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.
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.
- Open your web browser and go to the ECR Console.
- Under Public repositories, click Create repository.
- Under the General settings, ensure the Public option is selected.
- Under the Detail section, enter your new repository name. In this example, the name is my-ecs-demo.
- Scroll to the bottom of the page and click Create repository.
- 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.
- Copy all the push commands to a text editor or note for later use. Click Close.
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
- Open a terminal on your computer.
- Create a new Dockerfile using your preferred editor. In this example, let’s create one with nano:
- 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
- Next, create the homepage file (index.html) that will be copied to your container:
- 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>
- 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 .
- 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
- Confirm that the Docker image exists:
As you can see, the result below confirms that the Docker image creation is successful.
Deploy Docker Image to ECR
You have created the Docker image, and now is the time for Docker deployment.
- 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/********
- 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.
- 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.
- 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.
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
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.
- Go to the Amazon ECS console.
- Click Clusters → Create Cluster.
- Next, select the ECS Linux + Networking cluster template and click Next step.
- Enter the new cluster name.
- 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.
- 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.
- Scroll down to the bottom and click Create.
- Once the cluster is created, click the View Cluster button.
- And you should see the cluster page, as shown below.
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.
- Back to the Amazon ECS console, click Task Definitions → Create new Task Definition.
- Choose EC2 as the launch compatibility type, and click Next step.
- Now, enter the task definition name.
- 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.
- Click Add container under Container definitions. This is where you’ll specify which container this task will run.
- Type in the new container name, like, my-ecs-demo-container. In the Image box, paste the image URL you copied earlier.
- 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.
- The container name should appear on the list, as shown below.
- Scroll to the bottom of the page and click Create.
- And now you’ve created the task definition.
Step 5: Run the Container
You’re ready to run the container now that you’ve defined the task.
- Open the ECS cluster page, click the Tasks tab, and click Run new Task.
- 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.
- Scroll to the bottom and click Run Task.
- Back to the Cluster page, click the ECS Instance tab and the ECS Instance link.
- Copy either the public IPv4 address or the public IPv4 DNS on the instance page.
- 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.
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.