Containers have taken the development environment by storm. And perhaps you took advantage of this technology to containerize your application. But the question is, how do you deploy these containers? Well, you’ve come to the right place. This AWS ECS tutorial is everything you require to get started in deploying Docker containers.
This tutorial will teach you how to deploy and ship your containers to AWS cloud infrastructure. So buckle up and start deploying containers like a pro!
This guide will provide a practical demonstration. Ensure you have the following necessary tools:
- Docker installed on your computer.
- An AWS account.
- Git installed on your computer.
- Node.js installed on your computer.
- Visual Studio Code (VS Code) installed.
- Ensure you have AWS CLI is installed and ready on your computer.
To be able to set up the Docker container in AWS, we need to set up some configurations that will allow us to run AWS commands directly from the computer.
- First, ensure you install the latest AWS CLI version. Then go ahead and run
aws --versionto confirm if the AWS CLI is successfully installed on your computer. If it’s your first time installing AWS CLI, you may be required to restart your PC. Then rerun
aws --versionfor installation confirmation.
- Create an AWS IAM user that has AmazonEC2ContainerRegistryFullAccess policies. Make user you copy your created Access Key ID and Secret Access Key. This key will be required in the coming step.
For PC to locate your AWS credentials, you need to configure the following:
- Open your terminal and run
- Provide your AWS account Access Key ID. You can get this by navigating to your created AWS IAM user Security credentials.
- Provide your AWS Secret Access Key. You can get this from created AWS user.
- Provide AWS Default region name, i.e., us-east-2. Check this on your Account settings.
- Provide AWS Default output format, i.e., json
Setting Up a Basic Application
Assuming your application is ready and running locally on your computer, the next step is to run this application using Docker. Docker will package and containerize the application within Docker containers. In this example, you’ll use a simple React-based application to run a web app. But if you have your own application to carry out this test, you can skip this step.
To set up this React project, navigate to where you want your application to live, then this command to set up React as follows:
npx create-react-app sample-react-app
This will create a new
sample-react-app folder containing your application. Change the directory and point to this newly created folder:
To test this application, run the following:
Now go ahead and access the endpoint
http://localhost:3000/ on your browser and check if the web app is working. You should be served with the following basic web app:
Containerizing the React Application with Docker
You’ve just verified the application works correctly, and that’s great. But then again, the goal is to run the application inside a Docker container, so you’ll need a Dockerfile.
A Dockerfile (a text document) consists of instructions that define Docker commands to create a container image for your application. Let’s create one for the above application. First, you need to create a production build of this application to optimize it to run a light Docker image. A production-ready application creates images of smaller sizes
react-app directory, run:
npm run build
Create a file called
Dockerfile at the root of the project directory, and populate the code below, which creates a Docker Hub image to run Node.js.
# pull an nginx image and use it here FROM nginx:stable-alpine # Copy the build output this a new folder on Docker COPY build/ /usr/share/nginx/html
The Dockerfile commands may differ based on the application you want to containerize. If you are running a different project, make sure the Dockerfile has the right commands needed to dockerize the specific application.
Next, run the docker build command to execute the
Dockerfile in the working directory (.) to build an image called
my-docker-app. Ensure to specify the Dockerfile’s path. This command executes the Dockerfile to run all Docker commands and creates a
my-docker-app image that contains your project files and folders.
docker build -f Dockerfile -t my-docker-app .
Now, open your Docker Desktop, navigate to the Images page, and confirm if the image has been successfully deployed.
Once deployed, execute the following docker run command to map the
my-docker-app image to a Docker container and run the application on port
docker run -p 3000:3000 my-docker-app
Navigate to the Containers page in your Docker Desktop to confirm if the container has been successfully created.
Finally, navigate to the application’s URL (http://localhost:3000) on your web browser to access the containerized application. The same application server locally will be served by the Docker container, as shown below.
Connecting an Elastic Container Registry (ECR) on AWS ECS
You’ve seen your application is correctly working with Docker, locally, at least. This time, get into the cloud and deploy your Dockerized application to AWS.
But before you can deploy your application’s Docker container to AWS ECS, you need an ECR setup. ECR is an AWS-dedicated service for sharing and deploying container applications. This service offers a fully managed container registry that makes the process of storing, managing, sharing, and deploying your containers easier and faster.
To set up an ECR, you’ll need to create an ECS repository:
- Open the ECR service on your AWS account.
- On your AWS ECR page, click Get Started to initiate creating a new repository.
- Configure the repository’s General settings with the following:
- Choose the Private option to set the repository’s visibility to private.
- Provide a Repository name that matches your application use case.
- Make sure the name follows the AWS convectional naming rules as indicated below the repository name field.
- The named repository will set up the repository address you’ll use to access this repository.
- Now, scroll down at the bottom, and click Create repository to finalize creating the ECR repository.
- Finally, click the newly-created repository’s name (basic_react_app) from the list under the Private tab to access the repository’s information overview.
Note down the image URI as you’ll need it to configure a task later.
Pushing the Docker Image to the ECR Repository
Now that you have an ECR repository at your disposal, it’s time to push your Docker image to the newly-created repository (basic_react_app).
You need some commands that authenticate the Docker image to the registry and move the image to the created repository. AWS provides these commands out of the box.
To access these commands, navigate to your ECR repository, and click the View push commands button, as shown below.
A pop-up containing the Push commands for the repository will be launched.
Next, execute the following command to run an authentication token that authenticates and connects the Docker client to your registry (ECR) repository.
aws ecr get-the-password --the REGION | for login --yourusername AWS --awspassword ID.dkr.ecr.REGION.example.com
Copy your authentication token command and run the application where you wrote your Dockerfile. A Login Succeeded message should be logged in your terminal if the authentication is successful.
Now, run a command to build docker. This will build the container from the local/working project directory (.) as follows:
docker build -t basic_react_app .
After building, copy and run the docker tag command to tag the image. Doing so gives a tag to the image you’ll use to push the image to the repository.
docker tag basic_react_app:latest ID.dkr/basic_react_app:latest
Once tagged, copy and run the docker push command to push and publish the image to the ECR repository.
docker push ID.dkr/basic_react_app:latest
Ultimately, refresh the repository’s page to verify you’ve successfully pushed the image to the AWS ECR repository.
Creating a Task to Run the Docker Image
The image is now hosted in the ECR, but to run the image, you need to launch it onto an ECS container.
To deploy the image to ECS, you first need to create a task. A task tells ECS how you want to spin up your Docker container, including port mappings, CPU and RAM resources, etc.
Launch AWS ECS, and create task specifications on how to launch the container. Select EC2 since you’ll run the task using Amazon EC2 instances based on where you want to launch your task and click Next step.
Configure the new task with the following:
- Provide a unique Task definition name. This chois is MyReactAppTask, as shown below.
- Set the Task role to None. But you can optionally select the IAM role if you have one already set.
- Set the Network mode as to run your Docker container using the default networking mode.
Scroll down to Task size section, and assign Task memory allocation (MiB) and Task CPU (unit) based on the resources you want to assign to the container. For example, 128mb and 1 vCPU, respectively.
Now, click Add container to initiate adding new container definitions.
Configure the new container definition with the following:
- Provide a unique Container name, but this tutorial uses the name ReactApp.
- Set the Image URI you noted in the last step of the “Connecting an Elastic Container Registry (ECR) on AWS ECS” section.
- Specify the Memory Limits (128mb will suffice).
- Set the Port mappings for the host (4000), and the container (3000). The Host port can be any port number you want to run the container on ECS, while the Container port is the port that runs your container on Docker.
- Leave other settings as the default unless you want to customize them further.
Next, click the Add button (bottom-right) to set the container after configuring the container definition.
Finally, click Create (bottom-right) to create the task.
Creating a Cluster as a Target to Run the Task
You’ve just created a task, and that’s a big step to deploying containers to AWS ECS. But whenever you want to launch a task or run an application container, you’ll need a target.
What target? A cluster acts as the target, which takes a task into the cluster configuration and runs that task within the cluster. In this case, an ECS agent gets installed on your task ES2 instance. The ECS agent communicates with the ECS cluster and receives requests to launch your container.
To create a cluster where you’ll run your task, launch AWS ECS, navigate to Clusters, and click on the Create Cluster button to initiate creating a cluster.
Next, select EC2 Linux + Networking as the cluster template, and click Next step.
Configure the cluster with the following:
- Provide the unique and descriptive Cluster name. This tutorial’s choice is MyReactApp.
- Keep the On-Demand Instance selected as the Provisioning Model, so you only pay for compute capacity by the hour.
- Select the EC2 instance type. You can use t2.micro or t3a.micro for the free tier, as shown below.
- Add the Number of instances. In this case, use one. But you can add more instances based on your container needs.
Now, configure the VPC for your container instance as follows:
- Pick the default VPC.
- Choose 1a in the Subnets section.
- Select the Auto public IP to Enabled as shown below
- Finally pick the default Security group. These are should in the below image
Click Create (bottom-right) Once you’re happy with the cluster configuration to create the cluster.
Lastly, navigate to the Clusters page as you did in step one, and you’ll see the newly-created cluster (MyReactApp). Below, you can see the MyReactApp cluster’s status is ACTIVE.
Running a Task Within a Cluster
You now have an active task and a cluster. So what’s next? Remember that the cluster is the target when you run a task, so you’ll have to connect the cluster with the task.
To run a task (MyReactAppTask) on your cluster (MyReactApp): On your cluster’s page, navigate to the Tasks tab. Here, go ahead and click on the Run New Task section. This action redirects your browser to a page where you can specify which task to run (step two).
Configure the run task with the following:
- Set the Launch type as EC2.
- Select your task (MyReactAppTask) as the Task Definition.
- Select your cluster (MyReactApp).
Run the task which pushes your container recreated to your instance. Once the task shows as RUNNING, everything is fine and well set up. The status may take time to change to RUNNING, but you can refresh the list of tasks by clicking on the refresh icon.
Testing the Deployed Docker Application
After going through all the steps of creating a task and a cluster, how do you know your deployed application is working? You first have to verify your instance is running correctly, and create an inbound rule. This will be used to control all the application incoming traffic accessing your instance.
Assuming your task is in the RUNNING state, go ahead and click on your container instance. This will allows you to view the resources being used by the container.
Below, you can see the resources your container uses.
Next, navigate to your cluster again, click on the EC2 Instances tab and select the instance running your ECS instance to verify if the deployed ECS container is working.
Click the Instance ID to access the created EC2 machine.
Now, copy the instance’s Public IPv4 DNS and paste it into your browser. Ensure you add the host port running the ECS container, in this case, port 4000. For example: ec2-compute-1.amazonaws.com:4000
At this point, the application cannot be accessed on your browser. You need to expose the Host port 4000 on your EC2 machine. Head to Security Groups and click on the Inbound rules tab to see the list of available inbound rules.
Tick the box on the security group rule you see in the list, and click on Edit inbound rules to initiate editing the selected inbound rule.
Configure the inbound rules as follows:
Click Save rules to add the rules you configured. Ultimately, refresh your page, and you should get the deployed application similar to the application you previously tested locally.
With Docker, teams can develop, ship, and run applications separately from their infrastructure. In this tutorial, you’ve learned how to use AWS to confidently deploy a Docker container to AWS ECS using tasks and clusters accordingly.
Why not let your team build an app as a collection of microservices, allowing them to create independently deployable services to deliver software faster? AWS ECS is an excellent choice once your team has developed the application model to deploy to production so the end-users can access the application.