Manage Multiple Containers with Docker Compose

Learn to orchestrate and manage multiple containers with a single Docker Compose configuration file.

🎯 Introduction

By the end of this article, you will understand why Docker Compose is useful and know how to write a docker-compose.yml file to start or stop a set of containers with one command. This is the final Docker article. After it, setting up environments for the MySQL and MongoDB courses should feel familiar.


🧱 Why Do We Need Docker Compose?

So far, starting each container has required a long docker run command. Recall the MySQL command:

docker run --name mysql8 -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 -v mysql-data:/var/lib/mysql -d mysql:8

One container is manageable, but real projects often combine an application container, a MySQL container, and a MongoDB container. With a long command for each, several problems arise:

  • Long commands are easy to lose or copy incorrectly when saved as notes.
  • On each new machine, you must find every command and run them in the right order.
  • Teams cannot easily ensure that everyone starts the same environment.

Docker Compose solves this multi-container orchestration problem: put all container configuration in one file, then start or stop the entire environment with one command.


🧱 What Is Docker Compose?

Docker Compose centers on a YAML configuration file, traditionally named docker-compose.yml. Think of a renovation checklist:

  • Giving verbal instructions about where to paint and where to lay flooring makes it easy to leave something out.
  • Writing every requirement on a checklist lets anyone follow the same instructions and get the same result.

docker-compose.yml is that checklist. It defines which containers the project needs (called services in Compose), their images, port mappings, and mounted volumes. Commit the file to Git so everyone on the team can use the same environment.

Recent versions of Docker Desktop include Compose. The command is docker compose, with a space. The older docker-compose command belongs to the separately installed legacy version; recognize it when reading older tutorials.

🛠 Write docker-compose.yml

Let's manage our earlier MySQL container with Compose. Create a directory and add docker-compose.yml:

docker-compose.yml
services:
    mysql:
        image: mysql:8
        container_name: mysql8
        ports:
            - '3306:3306'
        environment:
            - MYSQL_ROOT_PASSWORD=123456
        volumes:
            - mysql-data:/var/lib/mysql

volumes:
    mysql-data:

These settings correspond to the earlier docker run command:

  • services: List the services the project needs; here, just mysql.
  • image: mysql:8: The image name at the end of the command.
  • container_name: mysql8: Corresponds to --name.
  • ports: Corresponds to -p. Quote port mappings containing colons in YAML.
  • environment: Corresponds to -e, setting environment variables.
  • volumes: Corresponds to -v, mounting the named volume mysql-data at the container's data directory.
  • The final top-level volumes:: Declare the named volumes used by the project. Compose creates them automatically.

If you also need MongoDB, add another entry under services. One file manages them all.


🛠 Common Commands

Run these commands in the directory containing docker-compose.yml.

Start all services:

docker compose up -d

-d runs them in the background. The first run downloads images, creates volumes, and starts containers, all with one command.

Check service status:

docker compose ps

Stop and clean up:

docker compose down

This stops and removes the environment's containers, but preserves named volumes, keeping their data. The next docker compose up -d starts the services with that data intact.

View logs:

docker compose logs

Add a service name to view its logs alone, such as docker compose logs mysql.


💡 Prepare for the Next Courses

You have now covered the core Docker material. You will use these skills throughout the following courses:

  • MySQL course: Start MySQL with docker run and open its client with docker exec.
  • MongoDB course: Start MongoDB in the same way, using the same approach.

When you encounter commands such as docker run and docker exec in other courses, you will understand their arguments and be able to adjust ports, passwords, and volumes yourself.


❓ Questions and Answers

Q1: What formatting rules does YAML require?

YAML uses indentation to express hierarchy. Use spaces rather than tabs, and keep indentation consistent for settings at the same level. Incorrect indentation causes Compose parsing errors. This course uses four-space indentation.

Q2: What if I change the configuration after docker compose up?

Run docker compose up -d again. Compose detects configuration changes and recreates the affected containers.

Q3: Can services in a Compose file communicate with each other?

Yes. Services in the same Compose project join a shared internal network and can reach each other by service name. For example, an application container can use mysql as the database hostname instead of an IP address.

Q4: Does docker compose down remove volumes too?

Not by default. Add -v (docker compose down -v) to remove volumes as well, deleting their data. This is useful when you want to start over while learning.

Q5: Can a project have multiple Compose files?

Yes, but one docker-compose.yml is enough for learning. Complex projects may split configuration into files for environments such as development and production. You can explore that when you need it.


🧪 Practice

  1. Convert the MongoDB startup command into a Compose service in docker-compose.yml, then start it. Here is the original command:
docker run -d --name mongo-demo -p 27017:27017 -e MONGO_INITDB_ROOT_USERNAME=root -e MONGO_INITDB_ROOT_PASSWORD=123456 mongo:8
docker-compose.yml
# Hint: add a mongo service under services
# Write your configuration here
  1. Define both mysql and mongo services in docker-compose.yml. Run docker compose up -d, confirm both containers are running with docker compose ps, and clean up with docker compose down.

🎉 You now know how to use Docker Compose! This completes the Docker course. You can now set up and manage server environments with containers. Continue to the MySQL course to put these skills into practice; its very first command should already look familiar.