Installing Docker and Docker Compose in AWS EC2 Instances

Amazon Web Services offers the Elastic Container Service which can be ideal for auto-scaling docker deployments with high availability, but there is a fair amount of setting up and it could get expensive. For simpler workloads, or just getting started you can install Docker inside of a single EC2 instance, and then use Docker Compose to manage the containers within. In this tutorial, we are going to walk through launching an EC2 instance with docker, docker-compose-cli, and git pre-installed, then we’ll connect to it over SSH to run some containers. We’ll conclude by navigating to the instance’s public DNS address in our browser, to see the output of the container that we have mapped to the host instance’s port 80.

To get started you will need an AWS account if you don’t have one you can sign-up here. If you do then go ahead and login to your console.

From there you want to head over to the EC2 Dashboard to create the EC2 instance.

Configuring the EC2 Instance

EC2 Dashboard

Step 1

From the EC2 Dashboard click on the Launch Instance button.

This will take you to the Launch Instance Wizard which walks us through the instance creation process. The first step is to choose an AMI,  an AMI is essentially the default configuration for an EC2 instance, some are just a barebones operating system, others include useful packages for a given purpose. We are going to use Amazon Linux because it will make it easier for us to install docker and because it would be a desirable choice down the road as you integrate other AWS services.

Step 2

choose Amazon Linux AMI

Choose Amazon Linux

Step 3

choose the t2.micro instance type

Choose Instance TypeEnsure that the t2.micro instance type is selected and then click Next: Configure Instance Details 

Step 4

Configure User Data

User data is data that is added by the user (us) at the time that an instance is first launched. We are going to supply a shell script that will install docker, docker-compose, and git when the instance is launched.

Scroll down and click Advanced Details to open the section:open advanced details

Paste the following script into the text area:

#! /bin/bash

yum update -y
yum install docker git -y
curl -L`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
service docker start
usermod -a -G docker ec2-user

Then click Next: Add Storage:Click Next Add Storage

This is a bash script that downloads and installs docker, git, and docker-compose, then starts docker and makes sure the cli is accessible by the default ec2-user. This way when we ssh into our instance all of these tools will be available to us.

Step 5

Add Storage

StorageFor this step, you will need to consider how much disk space you will need for your container(s). You will need some storage for docker to operate and store container images and volumes, etc., but also consider other storage options such as AWS S3 for media files, and AWS RDS or DynamoDB for your database needs. For this tutorial 8GiB should be plenty, so click Next: Add Tags.

Step 6

Add Tags

Add a Name tag, this will help to identify the EC2 instance inside of AWS. Click Add Tag, then for the Key enter Name and for the value enter dockerinstance. Then click Next: Configure Security Groups.

Add Tags

Step 7

Security Groups

Security Groups can be used to control which servers, computers, and IAM users, etc. can access the instance and how. For this tutorial we are going to open up all ports to all users, but once you know what your application needs you should lock this down.

Under Type change SSH to All trafffic 

Under Source change Custom to Anywhere

Configure Security GroupsNext click Review and Launch.

Step 8

Launch the instance

Launch InstanceThe Wizard gives us one last chance to review all of the options, then click Launch to launch the instance.

Step 9

Create a Key Pair

When you click Launch a modal window will pop up and ask you to select a key pair, choose the option to create a new key pair, and give the key pair a name like docker-key. Then click Download Key Pair. Make sure that the file downloads and you can find it on your computer, you need this file to access your instance.

Download Key Pair

Next click Launch Instances.

Step 10

Connect to the Instance

Navigate to the Instances page of the EC2 dashboard, select the instance and click the Connect button.

Click the Connect button

This will provide you with instructions to connect to the instance via SSH with your terminal application.

Connection Instructions

As the instructions state we need to locate our docker-key.pem file that we downloaded when creating the instance, and modify it’s permissions in order to use it. I like to move the file into my .ssh/ folder of my home directory (mac) when I am doing this step.

$ mv ~/Downloads/docker-key.pem ~/.ssh/
$ cd ~/.ssh
$ chmod 400 docker-key.pem

Now that the key is ready we can use it to connect by copying the ssh command  in the instructions:

ssh -i "docker-key.pem"

You will be greated with a security message because it is a new ssh host so type yes and press enter.

ssh prompt

After you are logged in you will see a screen like this:

EC2 Prompt

You can then type docker ps and press enter, this shows that docker is running and the cli is accessible, but no containers are currently running. This is exactly what we want.
Docker CLIdocker ps is used to list the running docker container processes.

Step 11

Start a Docker Container

Now we are going to start a docker container with the Apache HTTP server in order to serve a webpage from our EC2 instance. before we do that though we need a webpage to serve.

In the terminal that is connected to your EC2 instance make sure you are in the home directory:

Prompt in home directory

Create a new directory:

$ mkdir app

Then type $ vi ./appindex.html to enter the text editor VI, next you need to press i to enter INSERT mode then add the following content:

    <h1>Hello World!</h1>

When complete you need to press esc then :wq followed by enter to save and exit the file.

Now set the permissions for the app directory:

$ chmod -rf 775 ./app
$ cd ./app

Now we can use a docker command to launch the Apache container using this file:

$ docker run -dit --name my-apache-app -p 80:80 -v "$PWD":/usr/local/apache2/htdocs/ httpd:2.4

This will launch the container and attach Port 80 on the container to Port 80 on the EC2 instance, this way when someone enters the domain name for the instance in their web browser they will see our Hello World page.

If you use the $ docker ps command now you can see that there is a container running. So go back to the EC2 dashboard and find the Public DNS for the instance.

Public DNS

Copy this url into your browser to visit the page:

Hello World

This concludes our EC2-Docker tutorial, In future posts, will get to know the docker tools, and use the docker-compose and git resources that we installed to deploy a WordPress website on AWS EC2. Don’t forget to terminate the EC2 instance when you are done with it, if you are no-longer in the AWS free-tier you will be billed for the resources.






Leave a Reply

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