Docker compose: what is it? How can you use it?

Docker Logo

Docker compose (also called docker-compose) is a tool for designing and running multi-container applications. Following Docker‘s one application per container it is difficult to create or to containerize applications that have multiple components or servers. Thanks to docker-compose you can simply define multiple serviceslink them together and run them.

Imagine a WordPress installation, chances are you will need a web server such as Apache or Nginx. Then you will surely need a database such as MySQL or MariaDB. And optionally you might want a cache server such as Redis. Using Docker alone you can containerize each one of the components needed for a WordPress installation, but you can’t bundle them together in one container without breaking Docker’s philosophy.

docker-compose.yml and multi-container applications

Docker-compose is essentially a tool to design, link and run multiple containers together. This is accomplished thanks to the docker-compose.yml (or .yaml) file. This file, written in the YAML language, contains all the information needed to run your multi-container application. It is important to understand that docker-compose is just a tool and it leverages Docker to do all the hard work. Let’s take a look at an example docker-compose.yml file with only two containers:

version: '3'
services:
  web:
    build: .
    ports:
    - "5000:5000"
    volumes:
    - .:/code
    - logvolume01:/var/log
    links:
    - redis
  redis:
    image: redis
volumes:
  logvolume01: {}

As you can see the docker-compose file starts with a version statement. Each version has its own set of supported parameters and its differences. To know more about docker-compose files versions, you can check here.

After the version statement comes the services section, here all the containers of your multi-container application are specified. You might even want to specify only one container in the services section, but that kind of defeats the purpose of docker-compose.

You could, of course, run each container individually along with its complicated line of commands, but docker-compose allow you to break it and write it down in an easier to use and modify format.

Also notice, nearing the end of the file the volumes section, this section allow the user to define Docker Volumes to ensure data persistency.

The file format has way too many parameters to accomplish many complicate setups, if you’re interested, you can take a look at the latest docker-compose file reference.

Running a Docker compose file

Now that you’ve got your docker-compose file, you might wonder: “How do I start the containers?“.

docker-compose up

Is that all? Absolutely. This is how easy it is to start a multi-container application once its docker-compose file has been designed. Although you may want to use the -d flag in order to run the containers in background.

Installing Docker compose

Although each operating system has its own way to get Docker compose, there is a standard way on Linux operating systems regardless of the distribution.

sudo curl -L https://github.com/docker/compose/releases/download/1.16.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

Some operating systems might get Docker composer through their package manager, please refer to your distribution’s wiki/documentation in order to install using the package manager.

In the case you’re not running Linux or you need further help installing Docker compose, please take a look here.

Everything you do in Docker compose you can do in Docker

One important thing to understand that many beginners overlook is the fact that Docker compose is just a tool, the real player is Docker. Everything you’re able to do using docker-compose can be done using Docker alone. Take whatever docker-compose file, with a bit of effort you will be able to accomplish the same end result without Docker compose, using another tool or even Docker alone.

Although Docker compose has orchestration and healing capabilities, it might not the best tool to use when designing complex applications. Docker-compose was designed as a quick and primitive form of orchestration, used to design and run multi-container applications and only recently acquired the capability to interact with Docker Swarm. For the orchestration of complex applications Kubernetes is the de facto standard.

Now this might not be immediate, designing a docker-compose file might seem like a simple task, and it may even be a simpler task than managing containers using the command line only. Now imagine designing and maintaining hundreds, thousands of docker-compose files, each one with just a few different lines. It soon becomes a nightmare, hence the use of mature orchestration tools like Kubernetes is highly suggested for large-scale deployments.

Are Docker and Docker compose the same?

Absolutely not. If you’re asking this you’re probably new to the Docker world (I can’t blame you for the misunderstanding, I did the same), please do take a look at these articles in order to learn about containers and Docker:

I need orchestration, is Docker compose what I’m looking for?

It depends, but usually the answer is negative.

  • If you have a few containers, a few applications, and don’t need advanced healing capabilities and monitoring, you can use Docker compose.
  • For everything else use a proper orchestration tool like Kubernetes.
Image courtesy of mark | marksei
mark

You may also like...

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.