Why use docker-compose and most its important commands πŸ‹

1. Overview

If we know how to create and utilize Dockerfiles to automatize the creation of custom docker images and use multiple containers in a docker network, we still have a lot of manual wiring to do.

Fortunately, the docker toolbox brings docker-compose that let us create setups on a higher level – just like with Dockerfile for images.

2. Installing docker-compose

When you are running docker on a Mac or Windows you will have docker-compose already installed with the standard package. For Linux we can use pip since docker-compose is python based.

pip install docker-compose

In order to verify our docker-compose installation we run

docker-compose version

docker-compose version 1.17.1, build unknown
docker-py version: 2.5.1
CPython version: 2.7.15rc1
OpenSSL version: OpenSSL 1.1.0g 2 Nov 2017

3. Setting up a multi-container configuration

It’s best to explain what to use docker-compose for by a practical example. We assume we have to containers running, a server and a client, both based on nodejs like was shown in our tutorial. The following diagram shows the gist of it.

If we assume that we already have images for the server and client container on DockerHub we still have to run a few commands to get our system up and running.

docker network create myveryfirstnetwork
docker run --net myveryfirstnetwork --name server pushcommit/myveryfirstimageserver
docker run --net myveryfirstnetwork --name client pushcommit/myveryfirstimageclient

We can do all of this (and more of course) with a docker-compose.yml file just like we have done with a Dockerfile to build images.

version: "2"
services:
 server:
  image: pushcommit/myveryfirstimageserver
 client:
  image: pushcommit/myveryfirstimageclient

The first line defines the docker-compose file version that goes with certain features from the docker main version. Next we define services, in particular our server and client that are named accordingly. For each we define the image hosted on DockerHub.

Now we run the file and docker-compose up takes over from there:

root@uservirt-VirtualBox:/home/uservirt# docker-compose up
Creating network “uservirt_default” with the default driver
Creating uservirt_server_1 …
Creating uservirt_client_1 …
Creating uservirt_server_1
Creating uservirt_server_1 … done
Attaching to uservirt_client_1, uservirt_server_1
server_1 | Server listening on 80
server_1 | /
client_1 |
client_1 | Hello World!
client_1 |
server_1 | /
client_1 |
client_1 | Hello World!
client_1 |
server_1 | /
client_1 |
client_1 | Hello World!
client_1 |
server_1 | /
client_1 |
client_1 | Hello World!
client_1 |
^CGracefully stopping… (press Ctrl+C again to force)
Stopping uservirt_client_1 … done
Stopping uservirt_server_1 … done
root@uservirt-VirtualBox:/home/uservirt#

We see our server and client containers were created as services and the output is display in our terminal. Behind the scenes a network was created and wired up our containers. When we pressed CTRL+C docker-compose tried to shut down our configuration.

4. Other useful compose functionality

Just like when you run a docker container with certain flags and arguments you can specify them in your docker-compose.yml file.

For example:

  • Run docker-compose ps to see running and exited docker-compose setups.
    Name Command State Ports
    ———————————————————————
    uservirt_client_1 /bin/sh -c node /data/clie … Exit 137
    uservirt_server_1 /bin/sh -c node /data/index.js Exit 137
  • Run test or other commands for you environment. The -d flag runs a detached environment.
    docker-compose up -d
    ./run_tests
    docker-compose down
  • Mount volumes
  • Expose ports to the host system
  • Run automatic build commands on Dockerfiles
  • Specify custom networks
  • Define log output files

5. Conclusion

Docker compose is really worth installing since it helps with automation and thus a more predictable testing setup.

However, it is rivaled by docker stack that comes with the standard installation and is intendend for more scaling.

Leave a Reply

Your email address will not be published. Required fields are marked *