Log inSign up

Run multi-container stacks with Docker Compose

Docker Compose reads one compose.yaml file and starts every container, network and volume it lists with docker compose up -d. On a current Docker Engine install the Compose v2 plugin is already present, so you only write the file, keep passwords in an .env file, and update with docker compose pull followed by another up -d.

Topic: Containers and PaaS · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13, Rocky Linux 9, AlmaLinux 9 · Updated 2026-12-03

What do you need before writing a compose file?

You need a working Docker Engine with the Compose plugin and a normal sudo user. If Docker is not installed yet, follow the Docker Engine tutorial first, or deploy the Docker app image on Ubuntu 24.04 from the images page, which ships with both pieces ready. The official Docker repository for Ubuntu, Debian, Rocky Linux and AlmaLinux provides the package docker-compose-plugin, which adds the docker compose subcommand (with a space). The old Python tool called docker-compose (with a hyphen) is retired, so new projects should not depend on it.

docker version --format '{{.Server.Version}}'
docker compose version

The second command should print a version starting with v2. If it says the command is unknown, install the plugin from the Docker repository:

# Ubuntu and Debian
sudo apt update
sudo apt install docker-compose-plugin

# Rocky Linux 9 and AlmaLinux 9
sudo dnf install docker-compose-plugin

Add your user to the docker group only if you accept that membership equals root access on this host: sudo usermod -aG docker deploy, then log out and back in.

How should a project directory be laid out?

Give every stack its own directory, because Compose names the project after the folder and uses that name as a prefix for containers, networks and volumes. A tidy layout keeps the compose file, the env file and any config mounts side by side:

sudo mkdir -p /srv/notes
sudo chown deploy:deploy /srv/notes
cd /srv/notes
touch compose.yaml .env
chmod 600 .env

The preferred file name is compose.yaml. Compose still accepts docker-compose.yml, but if both exist the newer name wins, which can confuse you later. The top level version: key from older examples is obsolete and only produces a warning, so leave it out.

What does a small real stack look like?

The example below runs a web application behind a PostgreSQL database, with the app published only on the loopback address so a reverse proxy on the host can reach it while the internet cannot:

services:
  app:
    image: ghcr.io/example/notes:1.8
    restart: unless-stopped
    env_file: .env
    environment:
      DATABASE_URL: postgres://notes:${DB_PASSWORD}@db:5432/notes
    ports:
      - "127.0.0.1:8080:8080"
    depends_on:
      db:
        condition: service_healthy

  db:
    image: postgres:17
    restart: unless-stopped
    environment:
      POSTGRES_USER: notes
      POSTGRES_PASSWORD: ${DB_PASSWORD:?set DB_PASSWORD in .env}
      POSTGRES_DB: notes
    volumes:
      - dbdata:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U notes"]
      interval: 10s
      retries: 5

volumes:
  dbdata:

Services find each other by name on the default project network, so the app connects to db without any IP address. The database has no ports entry at all, which keeps it private to the stack. The depends_on condition waits for the health check to pass instead of only waiting for the container to start.

How do env files and variables work?

Compose reads .env in the project directory automatically and uses it to fill ${VAR} references inside compose.yaml. That is interpolation, done before any container starts. The separate env_file: key passes variables into the container environment at run time. The two are easy to mix up, so remember: .env shapes the file, env_file feeds the process.

# .env
DB_PASSWORD=change-this-long-random-string
COMPOSE_PROJECT_NAME=notes

Generate a strong value with openssl rand -base64 32. The syntax ${DB_PASSWORD:?message} stops Compose with an error when the variable is empty, and ${TAG:-latest} supplies a fallback. To use a different file, pass --env-file prod.env. Keep env files out of Git by adding them to .gitignore, and check the final result, with every variable filled in, before you start anything:

docker compose config

That command prints the merged configuration and fails loudly on YAML mistakes, which makes it a cheap first test after each edit.

How do profiles switch optional services on and off?

Profiles mark services that should only start when you ask for them, such as an admin tool or a one-off job. A service with a profiles list is skipped by a plain up:

  adminer:
    image: adminer:4
    profiles: ["debug"]
    ports:
      - "127.0.0.1:8081:8080"
docker compose --profile debug up -d
docker compose --profile debug down

You can also set COMPOSE_PROFILES=debug in .env. Services without a profile always start, so the core application never depends on remembering the flag.

Which restart policy should each service use?

Use unless-stopped for long running services on a VPS: the container comes back after a crash and after a reboot of the server, but stays down if you stopped it on purpose. The choices are no (the default), always, on-failure (optionally with a retry count such as on-failure:5) and unless-stopped. Restarts after a reboot only happen if the Docker service itself starts at boot, so confirm it is enabled:

sudo systemctl enable --now docker
systemctl is-enabled docker

For batch jobs that should run once and exit, leave the policy at no and call them with docker compose run --rm jobname.

How do you start, inspect and stop the stack?

Run every command from the project directory, or point at it with -f. The everyday set is short:

docker compose up -d          # create or update containers
docker compose ps             # state and health of each service
docker compose logs -f app    # follow one service
docker compose exec db psql -U notes
docker compose restart app
docker compose down           # remove containers and network, keep volumes

Test that the app answers on the loopback port with curl -I http://127.0.0.1:8080. To serve it on a domain, place a reverse proxy before the app, for example Caddy with automatic HTTPS. Avoid down -v unless you really want to delete the named volumes and the data in them.

How do you update images without surprises?

Pin image tags to a version you have tested, change the tag in compose.yaml when you want an upgrade, then pull and recreate. Compose only replaces containers whose image or settings changed:

cd /srv/notes
docker compose pull
docker compose up -d
docker compose ps
docker image prune -f

Take a copy of the database before major version jumps (see volumes and bind mounts), read the upstream release notes, and keep the previous tag written down so you can roll back by editing one line. Tools that update containers automatically exist, but on a server holding real data a manual pull keeps you in control. Old images pile up after updates, and the cleanup guide shows how to clear them safely.

Why does my published port ignore the firewall?

Docker writes its own packet filter rules for published ports, and those rules are evaluated before the ufw or firewalld chains that handle normal traffic. A mapping like "8080:8080" is therefore reachable from the internet even when ufw says the port is closed. The simplest protection is to bind to 127.0.0.1, as in the example, and expose only ports 80 and 443 through a proxy on the host. Check what is really listening with:

sudo ss -tlnp

Lines showing 0.0.0.0:8080 or [::]:8080 are public. More firewall background is in the firewall tutorial and the ufw guide.

Plans that fit this guide

Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.

All prices
PlanFits becauseSizeMonthlyOrder from
A2-4an app and a database with room for logs2 vCPU, 4 GB RAM, 100 GB NVMe$14.40/mo$43.20 for 3 months
A4-8several stacks or heavier databases4 vCPU, 8 GB RAM, 180 GB NVMe$28.80/mo$86.40 for 3 months

Questions about Docker Compose stacks

Is docker-compose with a hyphen still supported?

The standalone v1 tool no longer receives updates. Use the docker compose plugin; it reads the same files, and most old compose files work after you drop the version: line.

Where does Compose keep my data?

Named volumes live under /var/lib/docker/volumes on the server disk. Bind mounts stay wherever you point them, for example ./data inside the project folder.

Can one server run several stacks?

Yes. Give each stack its own folder and project name, and make sure no two stacks publish the same host port. Each stack gets its own default network.

How do I see the variables a container actually received?

Run docker compose exec app env, or docker compose config to see the values after interpolation. Avoid pasting that output anywhere public, since it contains secrets.

Does Compose start my stack after a reboot?

Containers with restart: unless-stopped or always come back when the Docker service starts at boot. Compose itself does not need to run; the policy is stored on each container.

What plan size suits a small Compose stack?

An app plus a database usually runs comfortably with 4 GB of RAM. Watch memory with docker stats and move to a bigger plan if you see swapping.