Docker is an amazing tool, however, in order to take full advantage of its potential it would be better if every component of your application ran in its own container. For more advanced applications, those which have a lot of components, telling all the containers to start up and shut down at the same time could quickly become inconvenient.
The community of Docker created an excellent solution which quickly became popular; this solution is called Fig and allows you to use a single YAML file to orchestrate with your Docker containers and configurations.
This solution was so popular that, eventually, the Docker team made their own version based on the Fig source which is named Docker Compose. This basically deals with the orchestration procedure of the Docker containers like starting up, shutting down, as well as setting up intra-container linking and volumes.
At the end of this tutorial you should have Docker and Docker Compose installed, along with a basic understanding of how Docker Compose works.
Docker and Docker Compose Concepts
When using Docker Compose, it will need a combination of several different Docker concepts in one. Before beginning, we will take a minute to review the different concepts involved. If you are familiar with the docker concepts such as volume, links, and port forwarding then you will want to skip over to the next part of the tutorial.
Every Docker container is basically a local instance of a Docker image. You could think of a Docker image as a complete Linux Installation. Normally a minimal installation includes the bare minimum of packages which are required to run the image. Those images use the kernel of the host system, however, they are running within a Docker container and are only capable of seeing their own file system. It is very possible to run a distribution like CentOS on an Ubuntu host, this works both ways.
Many Docker images are given from the Docker Hub which is managed by the Docker team. Many popular open source projects require an equivalent image uploaded to the Docker Registry which you can use to deploy the software. Once possible it is important to obtain ‘official’ images which means they are guaranteed by the Docker team to follow Docker best practices.
For this tutorial you are required to have the following:
- Ubuntu 14.04 VPS
- A non-root user with sudo privileges, Initial Server Setup with Ubuntu 14.04explains how to set this up.
Step 1 — Installing Docker
Begin by installing Docker if you did not already. The fastest way to install Docker is to download and install their installation script, you should be asked for a sudo password.
wget -qO- https://get.docker.com/ | sh
This command above will download and run a small installation script made by the Docker team.
If the user is not configured correctly with Docker it can become a pain; make sure to add the user to the docker group using the following command.
sudo usermod -aG docker $(whoami)
Log out of your server before logging in again to enable your new groups.
Step 2 — Installing Docker Compose
Now that you have Docker installed, you can go ahead and install Docker Compose. First, install ‘python-pip’ as a prerequisite which you will need.
sudo apt-get -y install python-pip
Now you may install Docker Compose with the following.
sudo pip install docker-compose
Step 3 — Running a Container with Docker Compose
In the public Docker registry, Docker Hub, it has a basic ‘Hello World’ image. Since you now have Docker Compose installed, you can try it out with this basic example.
Begin by creating a directory for your YAML file.
Afterwards, change into the directory.
Now open the ‘YAML’ file with your favorite text editor. We will be using nano.
Place the following contents into the file before saving and exiting the text editor.
docker-compose.yml my-test: image: hello-world
Above, you will need to know that the first line is used as part of the container name. The second line makes sure what image to use to create the container. The image should be downloaded from the official Docker Hub repository.
As you are still in the ‘~/hello-world’ directory, use the following command to make the container.
docker-compose up This output should show up: Output of docker-compose up Creating helloworld_my-test_1... Attaching to helloworld_my-test_1 my-test_1 | my-test_1 | Hello from Docker. my-test_1 | This message shows that your installation appears to be working correctly. my-test_1 |
This output explains what Docker is doing:
- The Docker client has contacted the Docker daemon.
- The Docker daemon pulled the ‘hello-world’ image from the Docker Hub.
- The Docker daemon made a new container using that image as it runs the executable which provided the output you are currently reading.
- The Docker daemon directed that output to the Docker client, then it was sent to your terminal.
If case the process has not exited on its own, enter CTRL-X.
This easy test will not show one of the most essential benefits of Docker Compose: being capable of bringing a group of Docker containers up and down all simultaneously.
Step 4 — Learning Docker Compose Commands
We will now go over the commands the docker-compose tool supports.
Beginning with docker-compose, this command works on a per-directory basis. You may have several groups of Docker containers running on one machine so make sure to have one directory for each container and one ‘docker-compose.yml’ file for each container within its directory.
Thus far you have ran ‘docker-compose’ up on your own and then use CTRL-C in order to shut it down. This causes debug messages to be presented in the terminal window which might not be ideal. Once running in production, you will need to have docker-compose function more like a service. An easy way to do this is to simply add the ‘–d’ option once you ‘up’ your session, like in the command shown below.
docker-compose up –d
docker-compose should now fork to the background.
To reveal your group of Docker containers, even if it is stopped or already running, use the command shown below. As an example, we will show you in the following that the ‘helloworld_my-test_1’ container has stopped.
Output of `docker-compose ps` Name Command State Ports ----------------------------------------------- helloworld_my-test_1 /hello Exit 0
If it is a running container, it should show the ‘Up’ state.
Output of `docker-compose ps` Name Command State Ports --------------------------------------------------------------- nginx_nginx_1 nginx -g daemon off; Up 443/tcp, 80/tcp
To end all running Docker containers for an application group, use the below command within the same directory as the ‘docker-compose.yml’ file which was used to start the Docker group.
In certain cases, Docker containers are going to store their old information in an internal volume. If you would like to start from scratch, you may use the ‘rm’ command to completely remove all the containers which made up your container group.
If you attempt any of those commands from a directory different than the directory which has a Docker container and a ‘.yml’ file then it will not show you your containers like the below output.
Output from wrong directory Can't find a suitable configuration file in this directory or any parent. Are you in the right directory? Supported filenames: docker-compose.yml, docker-compose.yaml, fig.yml, fig.yaml
This should cover all of the basic concepts of Docker Compose and how you can get it installed and running.