How To Install Docker on an AWS Instance of Ubuntu

Updated 12/26/18
Problem scenario

You want to install Docker.  You can make changes to the Ubuntu Linux repositories so you can use apt-get to install it.  How do you install Docker on an AWS EC-2 instance?

Solution
There is an alternative set of directions if you click on this link here (if you would prefer not to configure the apt-get repositories).  These directions below to install Docker on Ubuntu Linux include a script and how to run it (but the script below will make a change to the repositories that the Linux server uses).  The script was designed to install Docker 1.12 on an AWS instance of Ubuntu 14.04, 16.04 or 18.04.  These directions assume that the user that will run Docker commands is named "ubuntu"; the "usermod" stanza associates this user with the group named "docker".  Please note that these directions do work for installing Docker on an Azure instance of Ubuntu too!  For AWS this script requires that your Ubuntu Linux server is in a security group that has access to the Internet.  The script takes approximately three minutes to run in AWS.  It may or may not take longer in Azure.  Bandwidth and resources on your instance may vary.  (If you want a variety of different Docker installation directions, see this posting.)

Step #1  Log in and become root.
Step #2  Run these commands:

cd /tmp/
touch installer.sh
chmod u+x installer.sh   # 
this is unnecessary if you prefer to run the script with the "sudo bash installer.sh" command
vi installer.sh

Step #3  Enter this text into the file from "#!/bin/bash" (the first line) to the line '"echo "docker run -it ubuntu bash""' (the last line):

#!/bin/bash
apt-get update
apt-get install -y apt-transport-https ca-certificates
sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
echo 'deb https://apt.dockerproject.org/repo ubuntu-trusty main' > /etc/apt/sources.list.d/docker.list
echo 'deb http://cz.archive.ubuntu.com/ubuntu trusty main' >> /etc/apt/sources.list.d/docker.list
apt-get update
apt-cache policy docker-engine
apt-get --assume-yes install linux-image-extra-$(uname -r) linux-image-extra-virtual
apt-get --assume-yes install docker-engine
sudo service docker start
sudo groupadd docker
sudo usermod -aG docker ubuntu
docker run hello-world
echo "Try running this command below to enter a Docker container:"
echo "docker run -it ubuntu bash"

Step #4  Save the changes. 
Step #5  Run the script (./installer.sh).  (An alternative way is to use "sudo bash installer.sh".)
Step #6  If you ran the above script when you were logged in with the "ubuntu" username, you will need to log out and log back in.  Otherwise start a new terminal session with the "ubuntu" user. Then run this command with no quotes to enter the Docker container:  "docker run -it ubuntu bash"

Leave a comment

Your email address will not be published.