Docker Compose Cheat Sheet

The most important Docker Compose keywords and commands explained briefly, for running multiple services from a single file.

Last updated: September 1, 2026

Basic file structure

services:Top level block, lists all services in the file
my-service:Freely chosen name of a single service
image:Ready made image from a registry to use
build:Builds a custom image from a Dockerfile instead
container_name:Fixed, custom name instead of an automatically assigned one

Network and ports

ports:Connects a container port with a host port
networks:Defines which Docker networks the service connects to
network_mode: hostUses the host's network directly, without a Docker network in between
expose:Opens a port only for other containers, not to the outside

Data and environment

volumes:Permanently connects a host folder with the container
environment:Sets environment variables directly in the file
env_file:Loads environment variables from a separate file instead

Behavior and dependencies

restart:Defines when a container restarts automatically
depends_on:Starts a service only after another one has started
healthcheck:Regularly checks whether a service is actually working
restart: unless-stoppedRestarts automatically unless deliberately stopped

Terminal commands

docker compose up -dStarts all services in the background
docker compose downStops and removes all services in the file
docker compose buildRebuilds the images defined under build:
docker compose logs -fShows all services' logs continuously

Checking and managing

docker compose psShows the status of all services in this file
docker compose restartRestarts all services in the file
docker compose configChecks the file for errors and shows the final configuration
docker compose pullDownloads newer versions of the images in use
Worth a look

A simple example

Here's what a very simple Compose file with a single service looks like.

docker-compose.yml
services:Start of the services block
  webserver:Name of the service, freely chosen here
    image: nginx:latestUses the ready made, official Nginx image
    ports:Start of the port mapping
      - "8080:80"Host port 8080 points to container port 80
    restart: unless-stoppedRestarts automatically unless deliberately stopped

Indentation in a YAML file, like the one Docker Compose uses, is crucial, it defines what belongs to which block. A single misplaced space can already cause an error.