Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

What the Command Does

Run it in the folder that holds your Compose file:

docker compose up -d

Compose reads the file, pulls any image that is missing, creates the networks and volumes the file names, and starts every service. With -d (detached) your prompt comes back as soon as the containers run. Without it, Compose stays in the foreground, prints the output of every container, and Ctrl+C stops them.

The default file name is compose.yaml, or compose.yml. Compose also accepts the older names docker-compose.yaml and docker-compose.yml, and prefers compose.yaml when both exist. Running the command again is safe: services whose configuration and image did not change keep running untouched.

docker compose or docker-compose?

docker compose, with a space, is the Compose plugin of the Docker command line and the one to use today. docker-compose, with a hyphen, is the older standalone tool (Compose v1), which Docker no longer supports. The subcommands and options are largely the same, so docker-compose up -d from an old tutorial becomes docker compose up -d. If Docker answers that compose is not a docker command, install the docker-compose-plugin package from Docker's repository, as shown in how to install Docker on Ubuntu.

A Small Complete Example

A web server and a database, with the database files in a named volume:

services:
  web:
    image: nginx:1.30
    ports:
      - "8080:80"
    depends_on:
      - db
    restart: unless-stopped
  db:
    image: mysql:9.7
    environment:
      MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
      MYSQL_DATABASE: app
    volumes:
      - db_data:/var/lib/mysql
    restart: unless-stopped

volumes:
  db_data:

The password stays out of the file. Put it in a file named .env next to compose.yaml; Compose reads it by itself:

DB_ROOT_PASSWORD=STRONG_PASSWORD

Restrict it with chmod 600 .env and keep it out of Git with .gitignore. The database publishes no port, so only the other services of the project can reach it.

Check What Is Running

CommandWhat it does
docker compose psLists the project's containers
docker compose ps -aIncludes stopped ones
docker compose logs -fFollows all output
docker compose logs -f webFollows one service
docker compose logs --tail 50 dbShows the last 50 lines

Rebuild and Recreate

  • --build: docker compose up -d --build builds the images from the build: sections before starting, for example after you change a Dockerfile.
  • --force-recreate: replaces the containers even when nothing changed, for example after editing a mounted file that the program reads only at start.
  • Newer images: docker compose pull followed by docker compose up -d replaces only the containers whose image changed. See how to update a Docker container.

Start or Restart One Service

docker compose up -d web
docker compose restart web
docker compose stop web

Naming a service with up also starts the services it depends on, unless they already run. Add --no-deps to start only the one you named.

Other Files and Environment Files

docker compose -f compose.prod.yaml up -d
docker compose --env-file prod.env up -d
docker compose -p shop up -d

-f chooses another Compose file. --env-file reads variables from another file instead of .env. -p sets the project name, which otherwise comes from the folder name. Pass the same options to ps, logs, and down, or Compose looks at a different project.

Stop and Remove the Project

docker compose down

This stops and removes the containers and the project's network. Named volumes stay, so the next up -d finds the database as you left it.

Be careful with -v: it also deletes the project's named volumes, and with them every row of the database in the example above. There is no undo. List the volumes first and back up the ones you need before you run it:

docker compose config --volumes
docker volume ls
docker compose down -v

Common Problems

  • No configuration file provided: you are in the wrong folder, or the file has another name; change folder or use -f.
  • Port is already allocated: another container or program uses the host port. Find it with lsof or change the left side of the mapping.
  • A container exits right after start: read docker compose logs db. A variable missing from .env is a frequent cause: Compose replaces an unset variable with an empty string. Write ${DB_ROOT_PASSWORD:?not set} to make Compose stop with an error instead.
  • A port is open to everyone: ports in ports: are opened by Docker's own firewall rules, past ufw. Write 127.0.0.1:8080:80 to keep one local.
Was this answer helpful?
Loading...