Mad World #Save_GAZA: Free Palestine  

Search

What Is Docker Compose? A Beginner's Guide with a Real Example

What Is Docker Compose? A Beginner's Guide with a Real Example

Docker Compose is a tool that runs an app made of several containers from one file. You write the containers, their ports, their storage and their settings in a file named compose.yaml. Then one command, docker compose up -d, creates and starts them all, and docker compose down stops and removes them.

An example: WordPress needs two containers, the website and its MySQL or MariaDB database. Without Compose you type 5 Docker commands with about 20 options, and you must remember them for the next time. With Compose, the same settings live in one file that you can keep, copy to another server and share. Below we build that WordPress example both ways and run every command. We tested everything on 14 November 2026 with Docker Compose v5.7.1 and Docker Engine 29.2.1. The outputs are the real ones.

Docker Compose in plain words

WordWhat it means
Compose fileThe YAML file that describes your app, usually compose.yaml.
ServiceOne part of the app, such as wordpress or db. Compose runs one container for it (or more, if you ask).
ProjectAll the services of one Compose file. By default its name is the folder's name, here wordpress-demo.
NetworkCompose creates one private network per project, and the services find each other by their service name.
VolumeStorage that outlives the container: your database and your uploads stay when a container is recreated.

Docker's documentation sums it up the same way: Compose is a tool for defining and running multi-container applications, with services, networks and volumes in a single YAML file and one command to start them.

Docker vs Docker Compose: the same app both ways

Docker runs containers one at a time with docker run. Docker Compose does not replace Docker: it reads your file and asks the same Docker Engine to create the containers, the network and the volumes for you.

Here is WordPress with plain Docker commands. You need a network so the two containers can talk, two volumes for the data, and one long docker run per container:

Terminal
docker network create demo-net

docker volume create demo-db-data && docker volume create demo-wp-data

docker run -d \
  --name demo-db \
  --network demo-net \
  --restart unless-stopped \
  -e MARIADB_DATABASE=wordpress \
  -e MARIADB_USER=wordpress \
  -e MARIADB_PASSWORD=change-me-wordpress \
  -e MARIADB_ROOT_PASSWORD=change-me-root \
  -v demo-db-data:/var/lib/mysql mariadb:11.8

docker run -d \
  --name demo-wordpress \
  --network demo-net \
  --restart unless-stopped \
  -p 8080:80 \
  -e WORDPRESS_DB_HOST=demo-db \
  -e WORDPRESS_DB_USER=wordpress \
  -e WORDPRESS_DB_PASSWORD=change-me-wordpress \
  -e WORDPRESS_DB_NAME=wordpress \
  -v demo-wp-data:/var/www/html wordpress:7.1

It works, but we had to wait by hand: 20 seconds between the two docker run commands so the database was ready, then 8 seconds more. Then WordPress answered with a redirect to its installer:

Terminal
curl -s -o /dev/null -w '%{http_code} %{redirect_url}\n' http://localhost:8080/
Code
302 http://localhost:8080/wp-admin/install.php

But the settings now live only in your shell history. To change one option, you stop, remove and type the whole command again. And nothing makes WordPress wait for the database: you do the waiting.

Here is the same app as a Compose file. Make a folder named wordpress-demo and save this as compose.yaml in it:

Code
services:
  db:
    image: mariadb:11.8
    restart: unless-stopped
    environment:
      MARIADB_DATABASE: wordpress
      MARIADB_USER: wordpress
      MARIADB_PASSWORD: ${DB_PASSWORD}
      MARIADB_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
    volumes:
      - db_data:/var/lib/mysql
    healthcheck:
      test: ["CMD", "healthcheck.sh", "--connect", "--innodb_initialized"]
      interval: 10s
      timeout: 5s
      retries: 5

  wordpress:
    image: wordpress:7.1
    restart: unless-stopped
    depends_on:
      db:
        condition: service_healthy
    ports:
      - "8080:80"
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: ${DB_PASSWORD}
      WORDPRESS_DB_NAME: wordpress
    volumes:
      - wp_data:/var/www/html

volumes:
  db_data:
  wp_data:

Put the passwords in a second file named .env, in the same folder. Compose reads it on its own and fills in ${DB_PASSWORD} and ${DB_ROOT_PASSWORD}. Change these example values:

.env
DB_PASSWORD=change-me-wordpress
DB_ROOT_PASSWORD=change-me-root

What each part does:

  • services: the two containers, db and wordpress.
  • image: the image and its version, mariadb:11.8 and wordpress:7.1.
  • environment: the settings each image reads at start, here the database name, user and passwords.
  • volumes: db_data keeps the database and wp_data keeps the WordPress files. The volumes: block at the end creates them.
  • healthcheck: a test that tells Docker when MariaDB is really ready, not only started.
  • depends_on with condition: service_healthy: WordPress starts only after the database passes its health check (startup order in the documentation).
  • ports: "8080:80" opens port 80 of the container on port 8080 of your computer.
  • WORDPRESS_DB_HOST: db: WordPress reaches the database by its service name. No IP address is needed.

Run it: docker compose up

First, let Compose check the file. --quiet prints nothing when the file is valid:

Terminal
docker compose config --quiet && echo 'compose.yaml is valid'
Code
compose.yaml is valid

docker compose config also shows the file as Compose understands it, with the values of .env filled in (variable interpolation). A quick way to check that your variables arrive:

Terminal
docker compose config | grep -A 5 'WORDPRESS_DB'
YAML
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_NAME: wordpress
      WORDPRESS_DB_PASSWORD: change-me-wordpress
      WORDPRESS_DB_USER: wordpress
    image: wordpress:7.1
    networks:
      default: null
    volumes:
      - type: volume

Now start the project. -d runs it in the background and gives you the prompt back:

Terminal
docker compose up -d
Code
 Network wordpress-demo_default Creating 
 Volume wordpress-demo_wp_data Creating 
 Volume wordpress-demo_db_data Creating 
 Volume wordpress-demo_wp_data Created 
 Volume wordpress-demo_db_data Created 
 Network wordpress-demo_default Created 
 Container wordpress-demo-db-1 Creating 
 Container wordpress-demo-db-1 Created 
 Container wordpress-demo-wordpress-1 Creating 
 Container wordpress-demo-wordpress-1 Created 
 Container wordpress-demo-db-1 Starting 
 Container wordpress-demo-db-1 Started 
 Container wordpress-demo-db-1 Waiting 
 Container wordpress-demo-db-1 Healthy 
 Container wordpress-demo-wordpress-1 Starting 
 Container wordpress-demo-wordpress-1 Started

Compose created the network and the two volumes, started the database, waited until it was healthy, and only then started WordPress. In a terminal, the same command shows a progress view with the time of each step:

Terminal: docker compose up -d creates the network and two volumes, waits for the MariaDB container to be healthy, then starts WordPress
Real terminal session, 14 November 2026: docker compose up -d with Docker Compose v5.7.1. The database needed about 21 seconds to become healthy before WordPress started.

Check the state of the project with docker compose ps:

Terminal
docker compose ps
Code
NAME                         IMAGE           COMMAND                  SERVICE     CREATED          STATUS                    PORTS
wordpress-demo-db-1          mariadb:11.8    "docker-entrypoint.s…"   db          24 seconds ago   Up 21 seconds (healthy)   3306/tcp
wordpress-demo-wordpress-1   wordpress:7.1   "docker-entrypoint.s…"   wordpress   21 seconds ago   Up Less than a second     0.0.0.0:8080->80/tcp, [::]:8080->80/tcp

Give WordPress a few seconds. Right after up -d, our first curl got no answer at all (000), because Apache inside the container was still starting. A few seconds later:

Terminal
curl -s -o /dev/null -w '%{http_code} %{redirect_url}\n' http://localhost:8080/
Code
302 http://localhost:8080/wp-admin/install.php

Open http://localhost:8080 in a browser (or http://<server-ip>:8080 from another computer) and the WordPress installer appears:

WordPress installer served by the Docker Compose project at localhost:8080, with the site information form and the Install WordPress button
The WordPress installer, served by the Compose project: (1) the site details to fill in, (2) the button that finishes the install. WordPress already reached its database through the service name db.

How the services find each other

Compose put both containers on a project network named wordpress-demo_default. On it, each service name works as a host name (Networking in Compose). From inside the WordPress container, db resolves to the database container:

Terminal
docker compose exec wordpress getent hosts db
Code
172.26.0.2      db
Terminal
docker network ls --filter name=wordpress-demo --format '{{.Name}} {{.Driver}}'
Code
wordpress-demo_default bridge

The IP address can change each time the container is recreated. The name db does not, which is why the Compose file uses names.

The Docker Compose commands you will use

Run them in the folder that holds compose.yaml. Compose finds the project from the folder.

CommandWhat it does
docker compose up -dCreate and start every service, in the background
docker compose psList the containers of the project and their state
docker compose logs -fFollow the logs of every service (Ctrl+C to stop)
docker compose stopStop the containers, keep them
docker compose startStart the stopped containers again
docker compose restart wordpressRestart one service
docker compose exec db shOpen a shell in a running service
docker compose pullDownload newer images
docker compose configCheck the file and show it with the variables filled in
docker compose downStop and remove containers and the network, keep the volumes
docker compose down -vSame, and delete the named volumes (your data)

The logs of one service, the last 2 lines. Each line starts with the service name:

Terminal
docker compose logs --tail 2 db
Code
db-1  | 2026-11-14 14:04:14 0 [Note] mariadbd: ready for connections.
db-1  | Version: '11.8.9-MariaDB-ubu2404'  socket: '/run/mysqld/mysqld.sock'  port: 3306  mariadb.org binary distribution

For -f, --since and the other options, see our guide to docker logs and docker compose logs.

Restart one service, then stop the whole project and start it again. start waits for the database to be healthy again, like up:

Terminal
docker compose restart wordpress
Code
 Container wordpress-demo-wordpress-1 Restarting 
 Container wordpress-demo-wordpress-1 Started
Terminal
docker compose stop
docker compose ps --all --format '{{.Service}} {{.State}}'
Code
 Container wordpress-demo-wordpress-1 Stopping 
 Container wordpress-demo-wordpress-1 Stopped 
 Container wordpress-demo-db-1 Stopping 
 Container wordpress-demo-db-1 Stopped
db exited
wordpress exited
Terminal
docker compose start
Code
 Container wordpress-demo-db-1 Starting 
 Container wordpress-demo-db-1 Started 
 Container wordpress-demo-db-1 Waiting 
 Container wordpress-demo-db-1 Healthy 
 Container wordpress-demo-wordpress-1 Starting 
 Container wordpress-demo-wordpress-1 Started

To apply a change to compose.yaml, such as a new port or a new image version, run docker compose up -d again: Compose recreates the services that changed. Our docker compose up guide covers that command in detail.

docker compose down: does it delete your data?

No, not by default. down removes the containers and the network, and keeps the named volumes. down -v also deletes the volumes (docker compose down reference). We tested both. First we created a database named notes inside MariaDB:

Terminal
docker compose exec db sh -c 'mariadb -uroot -p"$MARIADB_ROOT_PASSWORD" -e "CREATE DATABASE notes;"' && echo created
Code
created

Then docker compose down:

Terminal
docker compose down
Code
 Container wordpress-demo-wordpress-1 Stopping 
 Container wordpress-demo-wordpress-1 Stopped 
 Container wordpress-demo-wordpress-1 Removing 
 Container wordpress-demo-wordpress-1 Removed 
 Container wordpress-demo-db-1 Stopping 
 Container wordpress-demo-db-1 Stopped 
 Container wordpress-demo-db-1 Removing 
 Container wordpress-demo-db-1 Removed 
 Network wordpress-demo_default Removing 
 Network wordpress-demo_default Removed

The volumes are still there:

Terminal
docker volume ls --filter name=wordpress-demo --format '{{.Name}}'
Code
wordpress-demo_db_data
wordpress-demo_wp_data

After a new docker compose up -d, the notes database was still in MariaDB:

Terminal
docker compose exec db sh -c 'mariadb -uroot -p"$MARIADB_ROOT_PASSWORD" -e "SHOW DATABASES LIKE \"notes\";"'
Code
Database (notes)
notes

Then docker compose down -v, which deleted both volumes, and with them the database and the WordPress files:

Terminal
docker compose down -v
Code
 Container wordpress-demo-wordpress-1 Stopping 
 Container wordpress-demo-wordpress-1 Stopped 
 Container wordpress-demo-wordpress-1 Removing 
 Container wordpress-demo-wordpress-1 Removed 
 Container wordpress-demo-db-1 Stopping 
 Container wordpress-demo-db-1 Stopped 
 Container wordpress-demo-db-1 Removing 
 Container wordpress-demo-db-1 Removed 
 Volume wordpress-demo_wp_data Removing 
 Volume wordpress-demo_db_data Removing 
 Network wordpress-demo_default Removing 
 Volume wordpress-demo_wp_data Removed 
 Volume wordpress-demo_db_data Removed 
 Network wordpress-demo_default Removed
Terminal
docker volume ls --filter name=wordpress-demo --format '{{.Name}}' | wc -l
Code
0

So use docker compose down for everyday stops and updates, and down -v only to delete a test project for good.

Benefits of Docker Compose

  • One file describes the whole app. The compose.yaml above replaced 5 commands and their 20 options. It is also the documentation of how the app runs.
  • One command to start or stop everything. up -d and down, in the right order, every time.
  • Start order and health checks. WordPress waited 21 seconds for a healthy database instead of failing on its first try.
  • A private network with names. db reaches the database, whatever its IP address.
  • Data that survives. Named volumes outlive down and image updates.
  • Easy updates. docker compose pull then docker compose up -d downloads new images and recreates only what changed.
  • Easy to move. Copy the folder (with compose.yaml and .env) to another server and run up -d there.

Limits of Docker Compose

  • One server. A Compose project runs on one Docker host. To spread containers over several servers, look at Kubernetes or Docker Swarm.
  • The .env file is plain text. Keep it out of public Git repositories and readable only by you (chmod 600 .env).
  • No automatic updates. Compose updates an image only when you run pull and up -d.

docker-compose or docker compose? compose.yaml or docker-compose.yml?

docker-compose, with a hyphen, is the old Compose V1, a separate Python program that Docker no longer updates. Today Compose is a Docker plugin and you type docker compose, with a space. Check yours:

Terminal
docker compose version
Code
Docker Compose version v5.7.1

For the file name, Docker's documentation prefers compose.yaml, and still reads compose.yml, docker-compose.yaml and docker-compose.yml. If two of them are in the folder, Compose uses compose.yaml.

Many old examples start with a line such as version: "3.8". Compose ignores it today (version is obsolete) and prints a warning. We added that line to a copy of our file:

Code
time="2026-11-14T14:06:05Z" level=warning msg="/tmp/compose-lab-broken/compose.yaml: the attribute `version` is obsolete, it will be ignored, please remove it to avoid potential confusion"

Delete the version: line and the warning goes away. Nothing else changes.

The most common mistake: YAML indentation

YAML uses spaces to show what belongs to what. One space too few is enough to break the file. In a copy of our file, we moved image: of the wordpress service one space to the left:

YAML
  wordpress:
   image: wordpress:7.1
    restart: unless-stopped

Compose refused it:

Terminal
docker compose config --quiet
YAML
yaml: line 20: mapping values are not allowed in this context

The line number points at the first line Compose could not read, here line 20, restart:, just under the bad line. Use spaces, never tabs, and keep the same number of spaces for keys at the same level. Run docker compose config --quiet after each edit.

Docker Compose on a Synology NAS

On Synology, Container Manager calls a Compose project a Project. You create a project, choose a folder and paste your compose.yaml, and Container Manager runs the same up for you. Our step-by-step guides do exactly this: Immich on Synology and Paperless-ngx on Synology. You can also use the command line over SSH (see how to SSH into a Synology NAS) and put sudo in front of docker compose.

Frequently asked questions

What is Docker Compose in simple words?

A tool that starts an app made of several containers from one file, compose.yaml, with one command: docker compose up -d.

What is the difference between Docker and Docker Compose?

Docker runs containers, one docker run at a time. Docker Compose describes several containers, their network and their volumes in one file, and asks Docker to run them together.

Is Docker Compose free?

Yes. Docker Compose is open source, under the Apache 2.0 license.

Do I need to install Docker Compose separately?

Docker Desktop includes it. On Linux, install Docker's docker-compose-plugin package, as in our guide to install Docker Compose on Ubuntu. docker compose version tells you if it is there.

Does docker compose down delete my data?

No. It removes the containers and the network and keeps the named volumes. Only docker compose down -v deletes the volumes. We tested both above.

Should I name the file compose.yaml or docker-compose.yml?

Both work. compose.yaml is the name Docker recommends today.

Can I use Docker Compose in production?

Yes, on one server. Docker's documentation says Compose works in production, staging, development, testing and CI. For an app spread over several servers, use an orchestrator such as Kubernetes.

Yassine

Yassine

Yassine founded IGProd to educate the visitors with technological tutorials. These helpful guides are available for all sorts of professionals and novices, whether they work in technology or not!

Surf freely 💗

Leave a comment

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

Your experience on this site will be improved by allowing cookies Cookie Policy