Installing Docker on Ubuntu for Running Local Servers as Containers

written by William Patton on August 16, 2016 in Tools of the Trade and Web Development with no comments

Docker on UbuntuGetting up and running with Docker on Ubuntu is a straight forward process that takes less than 10 minutes – under 2 minutes if you install it from the official repos! Once it’s installed you can get started running images for whatever kind of process or server that you want. My main reason for installing Docker on my development machine is so that I could run local servers to use as the development version for whatever latest website that I was working on – but you can use it for running any kind of process you want.

Why Use Docker for Development?

Docker makes building – then deploying – applications much more efficient, secure  and scalable. It does this by creating a standardized environment within the container that includes everything you need to run the application. Anything you need to install on the production server can be installed inside the container allowing you simply run it and start work.

Since containers run in an isolated environment, separate from other applications and the underlying system environment, each application is unable to interfere with another – or the hosting operating system.

To read more about why Docker is an option worth considering check out the What is Docker page.

Getting Started With Docker on Ubuntu

large_h-transDocker is a container running system that’s available for most of the common OS and support is always growing. As of Ubuntu 14.04 it’s available in the official repos as docker.io. Read this to the next bold text before issuing any of these commands. You can install Docker on Ubuntu quickly and easily by running sudo apt-get install docker.io.

Check if it’s installed and start it.

sudo service docker.io status
sudo service docker.io start

Since the official repo package version of Docker installed as ‘docker.io’ you’re going to want to symlink it to the directory that most scripts and tool expect it to be so that it can be controlled by the ‘docker’ command.

ln -sf /usr/bin/docker.io /usr/local/bin/docker

That’s you all set-up and good to go if you just need to run a container quickly.

Or if you want to install the latest version and keep up-to-date.

If you will be using Docker regularly it may not be the best of ideas to install the version in the official Ubuntu repo.

The reason why you would choose not to install Docker from the official Ubuntu repos is that they often contain versions that are behind – sometimes far behind – the latest current version of the software. If you plan to use Docker as part of your regular workflow – and want to take advantage of the latest features and optimizations – you’ll want to install Docker using the Docker maintained repos. The method is a bit longer but it’s still quick and not that much more complicated. Simply paste the commands and let it go.

The prerequisites and installing part of this guide is a modified version of what you’ll find in the Get Started with Docker pages at the docker.com. If you have any concerns or aren’t too clear on what’s happening then that’s a good place to start figuring it out.

Installing the Prerequisites for Docker

The first thing to do is make sure that your package manager is set-up to work with https, when https is available, and install the package to manage the certificates. In all likelihood these will already be installed and apt will be using them when necessary.

sudo apt-get install apt-transport-https ca-certificates

Since we’re installing Docker on Ubuntu from a non-Ubuntu repo over a secured https connection we need to add the corresponding GPG key for the those repos so we can decrypt the messages that are send to us.

sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D

Next thing is to add the source destination to a file that apt will check. Open the sources file for Docker – if this doesn’t exist create it. I use nano to edit files on the command line but use whatever editor you choose.

sudo nano /etc/apt/sources.list.d/docker.list

Add one of the following lines that points to the source for your distribution. I’m running Ubuntu 14.04 Trusty.

On Ubuntu Precise 12.04 (LTS)
deb https://apt.dockerproject.org/repo ubuntu-precise main
On Ubuntu Trusty 14.04 (LTS)
deb https://apt.dockerproject.org/repo ubuntu-trusty main
Ubuntu Wily 15.10
deb https://apt.dockerproject.org/repo ubuntu-wily main
Ubuntu Xenial 16.04 (LTS)
deb https://apt.dockerproject.org/repo ubuntu-xenial main

Once you add the source destination to the ‘docker.list’ file be sure to update your package definitions.

sudo apt-get update

If you have an older version of Docker installed now’s the best to remove it and be sure that apt caches the correct new reference. Purge any old version you have and have it cache the latest references.

sudo apt-get purge lxc-docker
apt-cache policy docker-engine

We also need apparmor installed if your on 14.04 or 12.04. It’s probably already installed but just in-case install it with apt.

sudo apt-get install apparmor

Installing Docker and Testing

Once you have all the prerequisites installed it’s time to install Docker and test it. Installing it only takes a single package – docker-engine – it’ll get any dependencies your missing while it installs.

sudo apt-get install docker-engine

To test Docker installed correctly we need to start it and download a simple container to run. The usual container to test is the ‘hello-world’ container – which simply echos text to your terminal. If the text appears it worked, if not go back and retrace your steps.

sudo service docker start
sudo docker run hello-world

The first time you run a container it’ll be downloaded to your local system. It only has to download once and then every time you run it in future it’ll run the local image. Since this will be the first test when you try to run the ‘hello-world’ image it’ll download image before it runs.

When you run it you should get output similar to what is below.

sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
c04b14da8d14: Pull complete 
Digest: sha256:0256e8a36e2070f7bf2d0b0763dbabdd67798512411de4cdcf9431a1feb60fd9
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker Hub account:
 https://hub.docker.com

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/

Seeing that text means that you’ve correctly installed Docker and all the components it needs to run. Enjoy working within a containerised environment!

Fixing A Common Daemon Not Running Error

I came across this error more than once after installing Docker on my machines.

Cannot connect to the Docker daemon. Is the docker daemon running on this host?

To check if this is going to be a problem for you do a reboot and attempt to run the ‘hello-world’ container again. If it runs again you don’t need to do anything but if you see the error indicating that docker daemon isn’t running (check in top that it is actually running) then add your username to the ‘docker’ group with the command below, do another restart and try again.

sudo usermod -aG docker 

What to Run with Docker

You can run all kinds of containers with Docker on Ubuntu. From entire operating systems to just small parts and services. Anything you can run on a computer you can generally get running inside a container. For my uses, as a local development tool, I generally run containers that power WordPress. That is a container running Apache with WordPress files in it and another hosting MySQL and the database.

You could build both containers from scratch yourself (or build both services inside a single container) but Docker has pre-made containers for both that are a good starting point. You’ll need to start them both manually (there’s an easy way to start both containers at the same time using docker-compose). Run the following 2 commands. It’s important to start the MySQL container first – as it’s needed when the WordPress container starts. The names ‘some-mysql’ and ‘some-wordpress’ are the names that are being assigned to these containers. You can call them whatever you choose.

docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:latest
docker run --name some-wordpress --link some-mysql:mysql -d wordpress

To access the WP install on the ‘some-wordpress’ container you’ll need to enter the IP address into the address bar of your browser (don’t worry – I’ll show you how to map domains here in a second).

Start by getting the container ID by running docker ps.

williampatton@williampatton-desktop:~$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
aa5801faa3f4        wordpress           "/entrypoint.sh apach"   16 minutes ago      Up 16 minutes       80/tcp              some-wordpress
3e298b5b2acb        mysql:latest        "docker-entrypoint.sh"   18 minutes ago      Up 18 minutes       3306/tcp            some-mysql
williampatton@williampatton-desktop:~$ 

My WP container is running with ID aa5801faa3f4 replace that in the following command with your own container ID.

docker inspect --format '{{ .NetworkSettings.IPAddress }}' aa5801faa3f4

It’ll give you an IP address that your container is running on. Enter that IP address into your browser and hit enter. It’ll load up a WordPress install screen from the running WordPress container.

WordPress install screen on container

Don’t run the installer yet – it’ll install with a home and site url containing the IP. Instead open up your hosts file for edit with sudo nano /etc/hosts/. Then enter your container IP address followed by the domain name of the site you’re working with. My container IP is 172.17.0.3 so replace that number with whatever IP your WP container is running on.

Enter these 2 lines (replacing my IP with your own) at the end of your hosts file. You only need the ‘www.’ entry if you plan to use that to access the site.

172.17.0.3 example.com
172.17.0.3 www.example.com

Then when you visit ‘example.com’ in your browser you’ll be directed to the container running on your local machine. Now run the installer and you should get a version installed and running with the site and home urls pointing to the correct domain name. When it’s time to go live with this just upload the database and files to the real webhost and remove the reference to the domain from your hosts file.

Perhaps a better way to work with this is by using a hostname like ‘dev.example.com’ – then when it goes live you just need to replace all instances of ‘dev.example.com’ in the database to ‘example.com’. You’ll need to do this if the site you’re working on is already live and you need to retain access to it in your browser as well as access your development version.

If you have any problems installing Docker on Ubuntu give me a shout down in the comments section and I’ll do my best to help however I can.