What Is Docker Compose, and How Do You Use It?

Docker Compose is a tool you can use to centrally manage the deployments of many different Docker containers. It’s an important tool for any application that needs multiple microservices, as it allows each service to easily be in a separately managed container.

What Does Docker Compose Do?

Docker containers are used for running applications in an isolated environment. It’s quite common nowadays to see application deployments done in Docker for the numerous benefits it brings. However, it’s often not as simple as just running a single container. Usually you may have many containers coming together to act as one cohesive service made up of many moving parts.

Managing all of these at deployment time is messy, so to clean it up, Docker provides Docker Compose, a configuration tool used for running multiple containers at once. You can define all of the configuration in one YAML file, and then start all the containers with one command.

Rather than having all your services in one big container, Docker Compose allows you to split them up into individually manageable containers. This is both better for building and deployment, as you can manage all of them in separate codebases, and don’t need to manually start each individual container.

Using Docker Compose is a three step process:

  • Build the component images using their Dockerfiles, or pull them from a registry.
  • Define all of the component services in a docker-compose.yml file.
  • Run all of them together using the docker-compose CLI.

Docker Compose isn’t another kind of Dockerfile. You will still need to build and publish your Docker containers using a Dockerfile. But, instead of running them directly, you can use Docker Compose to manage the configuration of a multi-container deployment.

How Do You Use Docker Compose?

The configuration for a docker compose file is done in docker-compose.yml. You don’t need to place this at the root of your project like a Dockerfile. In fact, it can go anywhere, as it doesn’t depend on any other code. However, if you’re building the images locally, it will need to go in a project folder with the code being built.

A Compose configuration file will look something like the following. This configuration runs a WordPress instance using the wordpress container off the Docker Hub. However, this depends on a MySQL database, which is also created by Compose.

version: '3'

services:
   db:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: rootpasswordchangeme
       MYSQL_DATABASE: wordpress
       MYSQL_USER: usernamechangeme
       MYSQL_PASSWORD: passwordchangeme

   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     ports:
       - "8000:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: usernamechangeme
       WORDPRESS_DB_PASSWORD: passwordchangeme
volumes:
    db_data:

Lets take a look at the format of this file. First, a version number, since the syntax can change depending on which version you’re using.

Next a list of Services. The first is named “db,” and uses the mysql:5.7 container, set to always restart, and with environment variables to configure the database with a user and password. To maintain data across restarts, this image is configured with a Docker volume mounted to the MySQL data directory.

The other service is “wordpress,” which depends on the database service, ensuring that Docker will make sure the database is started before running. It exposes port 80 as port 8000, and sets some environment variables so that it can connect to MySQL. Note that the host for the database is set to db:3306, which tells the WordPress container to connect to the “db” service.

Lastly, the volumes are defined for persistent storage. Optionally, you can also define custom networking for the containers. There are plenty of extended options you can configure, so if you’re looking to do something specific, you should check the documentation for Docker Compose.

Once configured, starting this service is easy. Simply run docker-compose up, which will pull all the required containers and start your services.

docker-compose up -d

And you should see the services running on the system with docker ps. In this case, you’ll see WordPress up and running properly.

Building With Docker Compose

Docker Compose can also be used within a Dockerfile project, and can be set up to build and run an image locally rather than pulling from the Docker Hub.

To do so, you simply need to add a build section on the service. You can set different context directories, as well as different Dockerfiles for different images.

version: "3.8"
services:
  webapp:
    build:
      context: ./dir
      dockerfile: Dockerfile-alternate
    image: imagename:tag

In this case, the image: variable is also set, but here it is used to tag the image that is built by Docker Compose.

Original Article