Installing Docker on Ubuntu for Running Local Servers as Containers
Docker is a container engine capable of encapuslating applications into a package capable of being deployed anywhere and have it operate in the expected way. Getting up and running with Docker on Ubuntu is a straightforward 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 containers for whatever kind of process you require and have them operational in seconds knowing that the very same container could be run in production, or as a service/utility anywhere, and it would work exactly the same.
Why Use Docker for Development?
Having Docker on my development machine allows me to run local versions of sites that I work on and have them running in the conditions that I know they are to operate under. It makes building – then testing and deploying – things much more efficient, secure and scalable. Once you know the basics it’s easy to use to create isolated working environments or utility containers capable of running various testing or build tools that can be used in a a CI cycle.
Most distributed applications are run in many different types of environments – with their contents being set sent to and from many more. Sometimes they are vastly different in all kinds of ways.
- Base configurations are different depending on the needs of the system or what the owner feels is an important focus (not always the same for different people).
- Installed softwares are often different depending on what the system is used for or what it’s been used for in the past.
- Sometimes there is need for very specific versioning of that software or other components used.
- Operating System choice is quite subjective and sometimes there are multiple popular variants of each OS.
- Transparent networking and interconnections between different servers, applications, clusters, services and so on is extremely common.
The issues noted above can be tackled quite cleanly with the use of various Virtual Machines or VPS.
Docker can also tackle them. It does this by creating a standardized environment within the container that includes everything you need to run the application. Anything you need to run on the production server can be installed inside the container allowing you simply start it and have it run. You can start it on production, staging, local or wherever and get the same results. The entire environment and any dependencies are all inside the single container, it can run isolated from the host and any other containers.
Since containers run in an isolated environment, separate from other applications and the underlying system environment, each container is generally unable to interfere with another’s operations – or the host OS.
To read more about why Docker is an option worth considering check out the What is Docker page.
Getting Started With Docker on Ubuntu
Docker 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.
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.