Docker is an amazing tool, but in order to take full advantage of its potential it’s better if every component of your application ran in it’s own container. For more advanced applications 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 had an excellent solution which quickly became popular, this solution is called Fig, this allows you to use a single YAML file to orchestrate with your Docker containers and configurations. This solution got so popular that eventually the Docker team made their own version based on the Fig source. Which is named Docker Compose. What this basically does is it deals with the orchestration procedure of the Docker containers. (like starting up, shutting down, as well as setting up intra-container linking and volumes) really simply.
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 needs a combination of several different Docker concepts in one, before we begin, we will take a minute to review the different concepts involved. In case 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 just 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’s 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, this is managed by the Docker team. Many popular open source projects require an equivalent image uploaded to the Docker Registry, which you may use to deply the software. Once possible it’s important to obtain “official” images, This 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 Droplet
A non-root user with sudo privileges (Initial Server Setup with Ubuntu 14.04 explains how to set this up.)
Step 1 — Installing Docker
Let’s begin by installing Docker in case you didn’t 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.
While working with Docker it can be a pain if the user is not configured correctly, make sure you add the user to the docker group using the following command:
sudo usermod -aG docker $(whoami)
Log out and log in from your server to enable your new groups.
Step 2 — Installing Docker Compose
Since you have Docker installed now, we can go ahead and install Docker Compose. But first, install python-pip as a prerequisite you’ll 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 we now have Docker Compose installed, we could try it out with this basic example.
Begin by creating a directory for our YAML file:
Afterwards, change into the directory:
Now make the YAML file with your favorite text editor (we’ll be using nano):
Place the following contents into the file, then save the file and exit 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’re 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 got sent to your terminal.
In case the process hasn’t 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 simultanouesly.
Step 4 — Learning Docker Compose Commands
We will now go over the commands the docker-compose tool supports.
Let’s begin with docker-compose, this command works on a per-directory basis. You may have several groups of Docker containers running on one machine, just ensure to have one directory for each container and one docker-compose.yml file for each container within its directory.
Thus far we have ran docker-compose up on our own and then using 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’ll need to have docker-compose function more like a service. One easy way to do this is to simply add the –d option once you ‘up’ your session, like in the following command:
docker-compose up –d
docker-compose should now fork to the background.
To reveal your group of Docker containers (Even if stopped or already running), use the following command:
As an example, we’ll 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’s 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 following 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’d 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, it will complain and 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
Excellent, so this should cover all of the basic concepts of Docker Compose and how you can get it installed and running.