Docker basics for Amazon ECS
Docker is a technology that provides the tools for you to build, run, test, and deploy distributed applications that are based on Linux containers. Amazon ECS uses Docker images in task definitions to launch containers as part of tasks in your clusters.
AWS and Docker have collaborated to make a simplified developer experience that enables
you to deploy and manage containers on Amazon ECS directly using Docker tools. You
can now build
and test your containers locally using Docker Desktop and Docker Compose, and then
deploy
them to Amazon ECS on Fargate. To get started with the Amazon ECS and Docker integration,
download
Docker Desktop and optionally sign up for a Docker ID. For more information, see Docker Desktop
Docker provides a walkthrough on deploying containers on Amazon ECS. For more information,
see
Deploying Docker
containers on Amazon ECS
The documentation in this guide assumes that readers possess a basic understanding
of what
Docker is and how it works. For more information about Docker, see What is Docker?
Installing Docker
If you already have Docker installed, skip to Create a Docker image.
Docker Desktop is an easy-to-install application for your Mac or Windows environment
that enables you to build and share containerized applications and microservices.
Docker
Desktop includes Docker Engine, the Docker CLI client, Docker Compose, and other tools
that are helpful when using Docker with Amazon ECS. For more information about how
to install
Docker Desktop on your preferred operating system, see Docker Desktop overview
If you don't need a local development environment and you prefer to use an Amazon EC2 instance to use Docker, we provide the following steps to launch an Amazon EC2 instance and install Docker Engine and the Docker CLI.
To install Docker on an Amazon EC2 instance
-
Launch an instance with the Amazon Linux 2 or Amazon Linux AMI. For more information, see Launching an instance in the Amazon EC2 User Guide for Linux Instances.
-
Connect to your instance. For more information, see Connect to your Linux instance in the Amazon EC2 User Guide for Linux Instances.
-
Update the installed packages and package cache on your instance.
sudo yum update -y -
Install the most recent Docker Engine package.
Amazon Linux 2
sudo amazon-linux-extras install dockerAmazon Linux.
sudo yum install docker -
Start the Docker service.
sudo service docker start -
Add the
ec2-userto thedockergroup so you can execute Docker commands without usingsudo.sudo usermod -a -G docker ec2-user -
Log out and log back in again to pick up the new
dockergroup permissions. You can accomplish this by closing your current SSH terminal window and reconnecting to your instance in a new one. Your new SSH session will have the appropriatedockergroup permissions. -
Verify that the
ec2-usercan run Docker commands withoutsudo.docker infoNote In some cases, you may need to reboot your instance to provide permissions for the
ec2-userto access the Docker daemon. Try rebooting your instance if you see the following error:Cannot connect to the Docker daemon. Is the docker daemon running on this host?
Create a Docker image
Amazon ECS task definitions use Docker images to launch containers on the container instances in your clusters. In this section, you create a Docker image of a simple web application, and test it on your local system or Amazon EC2 instance, and then push the image to a container registry (such as Amazon ECR or Docker Hub) so you can use it in an Amazon ECS task definition.
To create a Docker image of a simple web application
-
Create a file called
Dockerfile. A Dockerfile is a manifest that describes the base image to use for your Docker image and what you want installed and running on it. For more information about Dockerfiles, go to the Dockerfile Reference. touch Dockerfile -
Edit the
Dockerfileyou just created and add the following content.FROM ubuntu:18.04 # Install dependencies RUN apt-get update && \ apt-get -y install apache2 # Install apache and write hello world message RUN echo 'Hello World!' > /var/www/html/index.html # Configure apache RUN echo '. /etc/apache2/envvars' > /root/run_apache.sh && \ echo 'mkdir -p /var/run/apache2' >> /root/run_apache.sh && \ echo 'mkdir -p /var/lock/apache2' >> /root/run_apache.sh && \ echo '/usr/sbin/apache2 -D FOREGROUND' >> /root/run_apache.sh && \ chmod 755 /root/run_apache.sh EXPOSE 80 CMD /root/run_apache.shThis Dockerfile uses the Ubuntu 18.04 image. The
RUNinstructions update the package caches, install some software packages for the web server, and then write the "Hello World!" content to the web server's document root. TheEXPOSEinstruction exposes port 80 on the container, and theCMDinstruction starts the web server. -
Build the Docker image from your Dockerfile.
Note Some versions of Docker may require the full path to your Dockerfile in the following command, instead of the relative path shown below.
docker build -t hello-world . -
Run docker images to verify that the image was created correctly.
docker images --filter reference=hello-worldOutput:
REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest e9ffedc8c286 4 minutes ago 241MB
-
Run the newly built image. The
-p 80:80option maps the exposed port 80 on the container to port 80 on the host system. For more information about docker run, go to the Docker run reference. docker run -t -i -p 80:80 hello-worldNote Output from the Apache web server is displayed in the terminal window. You can ignore the "
Could not reliably determine the server's fully qualified domain name" message. -
Open a browser and point to the server that is running Docker and hosting your container.
-
If you are using an EC2 instance, this is the Public DNS value for the server, which is the same address you use to connect to the instance with SSH. Make sure that the security group for your instance allows inbound traffic on port 80.
-
If you are running Docker locally, point your browser to http://localhost/
. -
If you are using docker-machine on a Windows or Mac computer, find the IP address of the VirtualBox VM that is hosting Docker with the docker-machine ip command, substituting
machine-namewith the name of the docker machine you are using.docker-machine ipmachine-name
You should see a web page with your "Hello World!" statement.
-
-
Stop the Docker container by typing Ctrl + c.
Push your image to Amazon Elastic Container Registry
Amazon ECR is a managed AWS Docker registry service. You can use the Docker CLI to
push,
pull, and manage images in your Amazon ECR repositories. For Amazon ECR product details,
featured
customer case studies, and FAQs, see the Amazon Elastic Container Registry
product detail pages
This section requires the following:
-
You have the AWS CLI installed and configured. If you do not have the AWS CLI installed on your system, see Installing the AWS Command Line Interface in the AWS Command Line Interface User Guide.
-
Your user has the required IAM permissions to access the Amazon ECR service. For more information, see Amazon ECR managed policies.
To tag your image and push it to Amazon ECR
-
Create an Amazon ECR repository to store your
hello-worldimage. Note therepositoryUriin the output.aws ecr create-repository --repository-namehello-repository--regionregionOutput:
{ "repository": { "registryId": "
aws_account_id", "repositoryName": "hello-repository", "repositoryArn": "arn:aws:ecr:region:aws_account_id:repository/hello-repository", "createdAt": 1505337806.0, "repositoryUri": "aws_account_id.dkr.ecr.region.amazonaws.com/hello-repository" } } -
Tag the
hello-worldimage with therepositoryUrivalue from the previous step.docker tag hello-worldaws_account_id.dkr.ecr.region.amazonaws.com/hello-repository -
Run the aws ecr get-login-password command. Specify the registry URI you want to authenticate to. For more information, see Registry Authentication in the Amazon Elastic Container Registry User Guide.
aws ecr get-login-password | docker login --username AWS --password-stdin.dkr.ecr.aws_account_idregion.amazonaws.comOutput:
Login Succeeded
Important If you receive an error, install or upgrade to the latest version of the AWS CLI. For more information, see Installing the AWS Command Line Interface in the AWS Command Line Interface User Guide.
-
Push the image to Amazon ECR with the
repositoryUrivalue from the earlier step.docker pushaws_account_id.dkr.ecr.region.amazonaws.com/hello-repository
Clean up
When you are done experimenting with your Amazon ECR image, you can delete the repository so you are not charged for image storage.
aws ecr delete-repository --repository-namehello-repository--regionregion--force

