Scale customer reach and grow sales with AskHandle chatbot

How to Deploy a Docker App to AWS?

Imagine you've built a stunning application using Docker. Now, you want to share it with the world by deploying it to AWS (Amazon Web Services). Sounds intriguing, right? In this guide, we'll walk through the steps to get your Docker app up and running on AWS smoothly. You'll feel like a tech wizard in no time!

image-1
Written by
Published onJune 27, 2024
RSS Feed for BlogRSS Blog

How to Deploy a Docker App to AWS?

Imagine you've built a stunning application using Docker. Now, you want to share it with the world by deploying it to AWS (Amazon Web Services). Sounds intriguing, right? In this guide, we'll walk through the steps to get your Docker app up and running on AWS smoothly. You'll feel like a tech wizard in no time!

Prerequisites

Before we dive into the nuts and bolts, make sure you have these prerequisites in place:

  1. An AWS Account: If you don't have one, you can create an account at the AWS website.
  2. Docker Installed: Make sure Docker is installed on your local machine. If not, you can get it from the Docker website.
  3. AWS CLI Installed: The AWS Command Line Interface should be installed and configured on your system. You can find the installation instructions here.
  4. An Application to Deploy: Make sure you have a Dockerized app ready to go.

Step 1: Build Your Docker Image

The first task is to create your Docker image. Navigate to your project's root directory and locate your Dockerfile. Execute the following command to build the Docker image:

docker build -t your-image-name .

Make sure to replace your-image-name with a suitable name for your image.

Step 2: Tag Your Docker Image

To push the image to AWS, we need to tag it with the Amazon Elastic Container Registry (ECR) repository. Assume you're using the default region us-east-1 for this example:

docker tag your-image-name:latest 123456789012.dkr.ecr.us-east-1.amazonaws.com/your-repo-name:latest

Replace 123456789012 with your AWS account ID and your-repo-name with the ECR repository name.

Step 3: Log in to Amazon ECR

Log in to your ECR using the AWS CLI. This command authenticates your Docker CLI to access your ECR:

aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 123456789012.dkr.ecr.us-east-1.amazonaws.com

Replace 123456789012 with your AWS account ID.

Step 4: Push Your Docker Image to ECR

Now that you're authenticated, you can push your Docker image to ECR:

docker push 123456789012.dkr.ecr.us-east-1.amazonaws.com/your-repo-name:latest

Replace 123456789012 with your AWS account ID and your-repo-name with the repository name.

Step 5: Launch an EC2 Instance

To run a Docker app on AWS, we can use an EC2 instance. Here are the steps:

  1. Navigate to the EC2 Dashboard: Log in to your AWS Management Console and navigate to the EC2 dashboard.
  2. Launch Instance: Click on the "Launch Instance" button.
  3. Choose an Amazon Machine Image (AMI): Choose an AMI such as "Amazon Linux 2".
  4. Instance Type: Select a suitable instance type. For simple applications, a t2.micro is often enough.
  5. Configure Security Group: Make sure to open necessary ports in the security group (e.g., port 80 for HTTP).

Step 6: Connect to Your EC2 Instance

Once your EC2 instance is running, connect to it using SSH:

ssh -i your-key-file.pem ec2-user@your-ec2-public-dns

Replace your-key-file.pem with the path to your key file and your-ec2-public-dns with the DNS of your running instance.

Step 7: Install Docker on EC2

After logging into your EC2 instance, install Docker by running these commands:

sudo yum update -y
sudo amazon-linux-extras install docker
sudo service docker start
sudo usermod -a -G docker ec2-user

Log out and log back in to apply the Docker group changes:

exit

Then, SSH back into your instance using the same command as before.

Step 8: Pull the Docker Image from ECR

Now, you can pull the Docker image from ECR onto your EC2 instance:

aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 123456789012.dkr.ecr.us-east-1.amazonaws.com
docker pull 123456789012.dkr.ecr.us-east-1.amazonaws.com/your-repo-name:latest

Replace 123456789012 with your AWS account ID and your-repo-name with the repository name.

Step 9: Run Your Docker Container

To run your Docker container, you can use the following command:

docker run -d -p 80:80 123456789012.dkr.ecr.us-east-1.amazonaws.com/your-repo-name:latest

This command maps the container's port 80 to the EC2 instance's port 80, making your application accessible via the instance's public DNS.

Congratulations! You've successfully deployed your Docker app to AWS. People around the globe can now access and enjoy your creation. While AWS offers a multitude of services and configurations, these steps should give you a robust starting point for deploying Dockerized applications on AWS. Dive into the AWS documentation to explore additional features and optimizations for your workloads.

DockerContainerAWS
Create personalized AI for your customers

Get Started with AskHandle today and train your personalized AI for FREE

Featured posts

Join our newsletter

Receive the latest releases and tips, interesting stories, and best practices in your inbox.

Read about our privacy policy.

Be part of the future with AskHandle.

Join companies worldwide that are automating customer support with AskHandle. Embrace the future of customer support and sign up for free.

Latest posts

AskHandle Blog

Ideas, tips, guides, interviews, industry best practices, and news.

View all posts