DevOps Engineer. Solutions Architect

Blog 1: Getting Started with Docker for AWS

Imgur

Docker is a great virtualization tool used by developers to create and deliver distributed applications in packages called containers. As you create bigger and more robust applications you’ll find yourself seeking a production ready environment to host your Docker images. AWS ECR (Elastic Container Registry) is a commonly used solution for developers who want to store their images in a cloud-based infrastructure. Other used container registries are Docker Hub and Google Container Registry.

For those who are unfamiliar with Docker check out their website here to get started.

Installing Docker on an Amazon EC2 instance

If you don’t plan on running Docker on a local development system, I recommend installing it on an EC2 instance. For this tutorial I’ll be using the Amazon Linux 2 AMI to maintain free tier eligibility.

  1. After you provision and gain access to your instance, you’ll want to this command to update installed packages on your instance:
    sudo yum update -y
  2. In order to install the most recent edition of Docker run:
    sudo amazon-linux-extras install docker
  3. Finally, to start up Docker use:
    sudo service docker start

Play around with Docker and see if you can create your very first Docker image. Start by running the command touch Dockerfile to create a Dockerfile within the directory. Here is a snippet of an example Dockerfile:

Imgur

Pushing your Docker image onto Amazon ECR

Note: Make sure you have AWS CLI installed and configured before moving on.

Once you have Docker images ready to be pushed onto Amazon ECR, follow these steps:

  1. Create an ECR repository to store your Docker image using this command as an example:
    aws ecr create-repository --repository-name example-repository --region region
  2. You’ll notice a repositoryUri in the output. Tag the image with the repositoryUri value:
    docker tag example aws_account_id.dkr.ecr.region.amazonaws.com/example-repository
  3. Afterwards, you’ll need to authenticate yourself to gain access to the repository. Use this command as an example but replace the appropriate credentials:
    aws ecr get-login-password | docker login --username AWS --password-stdin aws_account_id.dkr.ecr.region.amazonaws.com
  4. Once you’ve authenticated yourself, push your Docker image to Amazon ECR with the repositoryUri value from step 2.
    docker push aws_account_id.dkr.ecr.region.amazonaws.com/example-repository

Remember to delete any unused repositories to minimize costs! I’ll cover deploying Docker containers onto Amazon ECS (Elastic Container Service) in a future post.