Skip to content
lanzai.co
Go back

Installing Docker Compose and Common Commands

Docker Compose defines and runs multi-container applications. Modern Docker ships the Compose v2 plugin built-in.

Install

With current Docker (docker-ce package includes the plugin), just verify:

docker compose version

If it’s missing, install the plugin separately:

sudo apt update
sudo apt install -y docker-compose-plugin

Note it’s docker compose (with a space). The old standalone docker-compose command is deprecated.

Basic syntax

The skeleton of compose.yaml (or docker-compose.yml):

services:
  web:
    image: nginx:alpine
    ports:
      - "8080:80"
    volumes:
      - ./html:/usr/share/nginx/html
  db:
    image: postgres:16
    environment:
      POSTGRES_PASSWORD: secret
    volumes:
      - pgdata:/var/lib/postgresql/data

volumes:
  pgdata:

Common fields: image, build, ports, volumes, environment, depends_on, restart, networks.

Common commands

docker compose up -d             # build and start in the background
docker compose up -d --build     # rebuild after editing a Dockerfile
docker compose down              # stop and remove containers and networks
docker compose down -v           # also delete volumes (data loss!)
docker compose ps                # service status
docker compose ls                # list compose projects
docker compose logs -f           # follow logs from all services
docker compose logs -f web       # logs from one service
docker compose exec web sh       # shell into a service container
docker compose restart web       # restart one service
docker compose pull              # update images
docker compose config            # validate and print the resolved config

Common dev combos

docker compose up -d && docker compose logs -f   # start and tail logs
docker compose down && docker compose up -d      # clean restart
docker compose exec db psql -U postgres          # enter the database

Environment variables

The .env file is loaded automatically, used with $VAR:

services:
  db:
    image: postgres:16
    environment:
      POSTGRES_PASSWORD: ${DB_PASSWORD}
cp .env.example .env

Upgrade and migration tips


Share this post:

Previous Post
Installing Docker on Ubuntu/Debian
Next Post
Docker Commands Cheat Sheet