Image for post
Image for post
Kaique Rocha via

Installing Docker CE on an AWS EC2 instance running Ubuntu 16.04

Carlos Justiniano
Jul 18, 2018 · 4 min read

In this post we’ll look at creating an EC2 instance running Ubuntu 16.04 and Docker CE (community edition). You might use such an instance to learn more about Docker or for container testing and development efforts.

This post assumes you’re comfortable using the AWS dashboard. Also note that these instructions draw heavily from the How To Install and Use Docker on Ubuntu 16.04 post on Digital Ocean. This post is designed to be AWS specific and a faster read.

Go to the EC2 Dashboard on AWS and select an amazon machine image (AMI) that resembles the one shown below.

When creating an instance make sure to select a t2.small or better. The nano and micro instances are not recommended due to memory constraints. Also, make sure to change the default disk allocation from 8 GB to 30GB or more, as containers tend to use up disk space.

Once you ssh into your new instance and enter commands, you may notice the error message: unable to resolve host ip-172–30–0–158 To avoid that error edit the /etc/hosts file and add an entry similar to this one: ip-172–30–0–158

Naturally, use the machine’s IP instead of the one shown above.

The first step is to add the GPG key for the official Docker repository:

$ curl -fsSL | sudo apt-key add -

Then add the Docker repository to APT sources:

$ sudo add-apt-repository "deb [arch=amd64] $(lsb_release -cs) stable"

Next, we update the package database:

$ sudo apt-get update

Now we’re ready to install Docker CE:

$ sudo apt-get install -y docker-ce

Docker CE should now be installed. Check using the docker version command:

Congrats. You’re “almost” all set!

As you use docker command you’ll notice that some require you to prefix them with the sudo command. That can quickly become time consuming. Although, you can reduce the tedium to some extent using aliases as I describe in my Handy Docker Aliases post.

Still it would be advantageous to reduce the need to constantly use sudo. We can do that using the usermod command and add the current user to the docker group.

$ sudo usermod -aG docker ${USER}

Now just exist the ssh session and reenter to get the changes to take affect.

Once you reenter your EC2 instance you type the docker info command for good measure.

Lastly, we’ll create a Docker Swarm cluster to orchestrate containers:

$ docker swarm init

Great. We now have a single node swarm ready for testing.

The last thing we’ll do in this post is create a Docker Stack for organizing containers within our cluster.

To make this easier we’ll create three files. The first is what’s known as a compose file, which declares a network setup (for use within the swarm) and a list of service (containers) we’d like to include. For now, that will simply be an instance of Redis.

The next file we’ll create is a shell script which we’ll use to launch our test stack.

The last script is which tears down our test stack.

The idea here is that over time you’ll add additional containers to the test stack.

Once you’ve created the three files and updated the execute permissions on the shell scripts, you’re ready to launch the test stack!

Above we see that the script created a new test network and a service called test_redis. Using thedocker ps command we’re able to see the running container. Take note of the Container ID above — we’ll see that again shortly.

We can also use the docker service ls command to view the Redis container service, name, replicas (which indicates how many instances are loaded) and the port that Redis is using .

We can even access the Redis instance from outside of the swarm by using the docker exec command to execute the redis-cli command line client. We do need to supply the container instance ID we saw earlier.

That’s it. Hopefully that will help get you started using AWS and Docker and perhaps try a host of freely available containers at the Docker Store.

Thanks for reading! If you like what you read, hold the clap button below so that others may find this. You can also follow me on Twitter.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store