What is Docker?
Recently I had the opportunity to use Docker for a small project and I realized how cool it is!
But what is Docker? “Docker is the world’s leading software containerization platform” (Docker official site).
It allows you to pack your application into a standardized unit for software development. It define itself as:
- Lightweight: Containers running on a single machine share the same operating system kernel; they start instantly and use less RAM
- Open: Docker containers are based on open standards, enabling containers to run on all major Linux distributions and on Microsoft Windows
- Secure by default: Containers isolate applications from one another and the underlying infrastructure, while providing an added layer of protection for the application
Is the Docker approach similar to the Virtual Machine approach? Containers and virtual machines have similar resource isolation and allocation benefits but a different architectural approach allows containers to be more portable and efficient.
Virtual machine architecture: (note that an entire guest operating system is necessary)
Docker containers architecture (the kernel is shared between the containers)
So Docker allows you to host different application (shipped by containers) while sharing the same operating system kernel and keep the application isolation.
Docker comes with a lot of tools like Docker Engine, Machine, Kitematic and Docker Compose.
Compose is a tool for defining and running multi-container Docker applications. You can create a file (called docker-compose.yml) where you define the services that compose your application.
Using this approach you can build an application (composed by different services) and keep the services in separated containers (that can be deployed everywhere: on the same host or on more different hosts).
Now we are going to see how to install Docker. I am using Ubuntu 14.04.4 LTS (codename: trusty). If you want to use a different version of Ubuntu, your kernel must be 3.10 at minimum.
sudo apt-get update
sudo apt-get install apt-transport-https ca-certificates
sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
Now add the Docker repository. Edit the file /etc/apt/sources.list.d/docker.list (create it if it does not exist) and add the following line:
deb https://apt.dockerproject.org/repo ubuntu-trusty main
Now get the added repository and purge the eventually existing package:
sudo apt-get update
sudo apt-get purge lxc-docker
apt-cache policy docker-engine
And finally install the docker-engine:
sudo apt-get update
sudo apt-get install docker-engine
sudo service docker start
To check if Docker is correctly installed, you can check your docker version with
You should see the version of the Docker client and server.
If you see the error “Docker command can’t connect to docker daemon” while connecting to the docker server, you need to add your current user to docker group as follow (suppose you are using an account called ubuntu):
sudo usermod -aG docker ubuntu
To run a test imagine in a container you can use:
sudo docker run hello-world
Now you can download, create and run your own Docker image (to see the full list of Docker commands and images, I suggest you to look at the official Docker documentation).
Now that we have installed Docker, we can install Docker-Compose.
On the official site page you can find all the different installation modes, but I suggest you to use pip (Python Package Index) to install it.
If you do not have pip installed, you can get it with:
sudo apt-get install python-pip
Now you can easily install docker compose:
sudo pip install docker-compose
Tc check the Docker-Compose installation run:
Now that we have Docker-Compose, we can define a new configuration file to deploy the services of our application in different container and compose (link) them.
Here an example of docker-compose.yml file:
We defined an DB service (with mysql image) and a wordpress service (that depends from the DB service). These two container can be deployed on the same host or two different hosts. As you know, when you start a new application project is common to build everything on the same machine and often is impossible to split the services later, this approach will help up to split up the application when the application grows or the demand for the different parts changes.
So we moved from a single monolith application to a multi- containers (that can be deployed everywhere!) application.
In next post I will describe how to build a Python Django web application using Docker-Compose (web server and Postresql services splitted up in two containers).
Docker Compose file
Docker Installation – Ubuntu
Getting started with Docker