How to Deploy Docker Containers to Amazon Elastic Container Service (ECS)?

If you have an AWS account and want to move your Docker images and containerized applications, then you should consider the Amazon Elastic Container Service (ECS). With ECS, you can deploy Docker images and containers to AWS and forget about downtimes and scaling.

In this post, we’ll look at deploying a Docker image in the Amazon Elastic Container Registry (ECR) and running this container in ECS.

Requirements

For this post, you’ll need to have the following.

  • An AWS account.
  • A computer with AWS CLI installed. This post will demonstrate the examples using AWS CLI v2.8.7 on a Fedora 35 computer.
  • A Docker image that you will deploy to AWS. A simple NGINX image would do, which is what this post will use as an example.

Step 1: Create a New Repository in ECR

Before you can deploy a container in ECS, you must first have a repository for your Docker image. In this example, let’s create a sample public repository in AWS.

  1. Open your web browser and go to the ECR Console.
  2. Under Public repositories, click Create repository.
    deploy docker containers
  3. Under the General settings, ensure the Public option is selected.
    docker compose deploy
  4. Under the Detail section, enter your new repository name. In this example, the name is my-ecs-demo.
    docker compose ecs
  5. Scroll to the bottom of the page and click Create repository.
    aws docker compose
  6. Once the repository is created, click the View push commands button to view the commands you’ll need to run to authenticate and push images.
    deploy docker container to aws
  7. Copy all the push commands to a text editor or note for later use. Click Close.
    deploy docker to aws

Step 2: Build and Push the Docker Image to ECR

The repository is now ready, and all that is left is pushing the Docker image. In this step, we’ll build an image from scratch and push the resulting image to the repository.

Build the Docker Image

  1. Open a terminal on your computer.
  2. Create a new Dockerfile using your preferred editor. In this example, let’s create one with nano:
    nano Dockerfile
  3. Copy the below code into the editor. Save the file once you’ve updated the code:
    FROM nginx
    
    COPY index.html /usr/share/nginx/html

    aws ecs docker compose

  4. Next, create the homepage file (index.html) that will be copied to your container:
    nano index.html
  5. Once the file is open in the editor, copy the code below into the editor and save the file:
    <!DOCTYPE html> 
    
    <html> 
    
    <head> 
    
    <title>AWS ECS DEMO</title> 
    
    </head> 
    
    <body> 
    
    <h1>AWS ECS DEMO</h1> 
    
    <p>Docker Container Deployed in AWS ECS</p> 
    
    </body> 
    
    </html>

    deploy docker image to aws

  6. Now that you have the two files in place (Dockerfile and index.html), run the below command to build the image. This command is the second command in your repository’s list of push commands:
    docker build -t my-ecs-demo .

    deploy docker compose to aws

  7. Next, let’s tag the new Docker image with your repository. This step ensures you can later push the Docker image to your ECR:
    docker tag my-ecs-demo:latest public.ecr.aws/********/my-ecs-demo:latest
  8. Confirm that the Docker image exists:
    docker images

    As you can see, the result below confirms that the Docker image creation is successful.
    docker compose to ecs

Deploy Docker Image to ECR

You have created the Docker image, and now is the time for Docker deployment.

  1. Authenticate your Docker client to your repository. This command is the first push command in your list:
    aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws/********

    aws deploy docker container

  2. Next, run the fourth command in your commands list for Docker deployment to ECR:
    docker push public.ecr.aws/********/my-ecs-demo:latest

    Wait for the Docker deployment to complete.
    aws deploy docker compose

  3. Finally, list your current public repositories and confirm that the new image is not published:
    aws ecr-public describe-images --repository-name my-ecs-demo

    In the below result, there’s only one image, and it’s the one with the latest tag.
    aws deploy docker image

  4. And if you go back to your AWS ECR console, you’ll also see the image. Click on Copy URI and save it into a note for later use.
    deploying docker containers to aws

Step 3: Create an ECS Cluster

You’ve published the image, and right now, you can actually pull it from the repository. For example:

docker pull public.ecr.aws/********/my-ecs-demo

aws dockerfile

If that’s what you want, then great! You can run the container off of that image you pulled. But where’s the fun in that? Why not run the container in AWS instead? Let’s do that next.

  1. Go to the Amazon ECS console.
  2. Click ClustersCreate Cluster.
    amazon ecs docker compose
  3. Next, select the ECS Linux + Networking cluster template and click Next step.
    ecs docker image
  4. Enter the new cluster name.
    deploy docker compose to aws ecs
  5. Under the Instance configuration, choose On-Demand Instance as the provisioning model. Choose the EC2 instance type that you think will be sufficient for your application to run. In this example, let’s choose the t2.micro instance type, which is free-tier eligible.
    docker compose on ecs
  6. Under Networking:
    • Select the VPC where the EC2 instance will be deployed.
    • Select the Subnet in that VPC you wish to use.
    • Set the Auto assign public IP to Enabled.
    • Select the security group that allows the network traffic required by your container. For example, the published NGINX application will be exposed to port 8080, so the security group you choose must be configured to allow 8080.
      deploy docker container aws
  7. Scroll down to the bottom and click Create.
    deploy docker container to aws ec2
  8. Once the cluster is created, click the View Cluster button.
    deploying docker image to aws
  9. And you should see the cluster page, as shown below.
    aws ecs deploy new image

Step 4: Create the Container Task Definition

You’ve now created the cluster in which to run the container. But first, you must create a task definition defining how the containerized application will run. The task definition contains information, like which resources it will use and how many containers will be created.

  1. Back to the Amazon ECS console, click Task Definitions → Create new Task Definition.
    deploy using docker
  2. Choose EC2 as the launch compatibility type, and click Next step.
    docker compose deploy to aws
  3. Now, enter the task definition name.
    ec2 deploy docker image
  4. Scroll down to Task size and assign the amount of memory and CPU. In this example, let’s assign 100 MB of memory and 1 vCPU.
    deploy a docker container
  5. Click Add container under Container definitions. This is where you’ll specify which container this task will run.
    deploying containers
  6. Type in the new container name, like, my-ecs-demo-container. In the Image box, paste the image URL you copied earlier.
    deploy docker app to aws
  7. Under port mappings, enter the host port and container port. In this example, the container will be exposed to port 8080 and mapped to the container’s port 80. Click Add.
    deploy docker compose to ecs
  8. The container name should appear on the list, as shown below.
    amazon docker image
  9. Scroll to the bottom of the page and click Create.
    docker on ecs
  10. And now you’ve created the task definition.
    deploy docker compose aws

Step 5: Run the Container

You’re ready to run the container now that you’ve defined the task.

  1. Open the ECS cluster page, click the Tasks tab, and click Run new Task.
    deploy docker on ec2
  2. On the Run Task:
    • Select EC2 as the launch type.
    • Select the task definition you created.
    • Select the cluster in which to run the container.
      deploy a docker image
  3. Scroll to the bottom and click Run Task.
    deploy docker image to ecs
  4. Back to the Cluster page, click the ECS Instance tab and the ECS Instance link.
    aws docker compose deploy
  5. Copy either the public IPv4 address or the public IPv4 DNS on the instance page.
    deploy docker to ecs
  6. To test whether your container works, open a browser and paste the public IPv4 address in the address bar combined with port 8080.
    As you can see below, the web page loads, which confirms that the image and container were successfully deployed in AWS.
    docker compose ecs ec2

Conclusion

In the end, you’ve completed the Docker deployment to AWS ECR and ran the container in AWS ECS.

Running containers in AWS ECS is a surefire way to keep your applications running with little to no downtime. With it, you can focus on developing your application and not worry about hardware and resource scaling.

Cyril Kardashevsky

Leave a Reply

Your email address will not be published. Required fields are marked *