Reference

Docker & Kubernetes

Read image builds, container commands, Compose files and Kubernetes workload configuration.

Scope and conventions

Docker CLI/Dockerfile, Docker Compose and Kubernetes native YAML/kubectl. These are complementary tools, not interchangeable editions. Commands assume your own disposable local environment; image names, cluster objects and paths are illustrative. Nothing runs from this guide.

Docker Compose

Read a compose.yaml

Docker & Kubernetes

A Compose file describes an application as services (each one becomes one or more containers from an image), plus the volumes, networks, configs and secrets they share. name sets the project name, which prefixes container, volume and network names. Compose looks for compose.yaml in the current directory, then parent directories.

Fragment

Docker & Kubernetes · Read a compose.yaml
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
name: shop
services:
  web:
    image: nginx:1.30
    ports:
      - "127.0.0.1:8080:80"
    restart: unless-stopped
  api:
    image: ghcr.io/example/shop-api:1.4.2
    environment:
      DATABASE_URL: postgres://shop:example@db:5432/shop
  db:
    image: postgres:18
    environment:
      POSTGRES_USER: shop
      POSTGRES_PASSWORD: example # the image refuses to start without one
      POSTGRES_DB: shop

The top-level version: key is obsolete; current Compose ignores it and warns. compose.yaml is the preferred file name; docker-compose.yml is still read.

Image tags, the example API image and the password are illustrative; see the secrets entry for keeping a real password out of the file. A bare "8080:80" publishes on every host interface; prefix 127.0.0.1: to keep a development port local.

Start, inspect and stop a Compose project

Docker & Kubernetes

up creates and starts every service (-d returns immediately; --wait also waits for health checks to pass). ps and logs inspect the running project, stop keeps containers, and down removes the containers and networks it created. Volumes survive down unless you add -v.

Fragment

Docker & Kubernetes · Start, inspect and stop a Compose project
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
docker compose up -d --wait      # start in the background, wait until healthy
docker compose ps                 # containers, state and published ports
docker compose logs -f --tail 50 api
docker compose stop               # stop, keep containers
docker compose down               # remove containers and networks
docker compose down -v            # ...and the named volumes: deletes data

down -v deletes named volumes declared in the file, such as a database volume. Treat it as a data reset.

Changing compose.yaml and running up again recreates only the services whose configuration changed. --remove-orphans removes containers of services you deleted from the file.

Build service images from source

Docker & Kubernetes

build replaces image when Compose should build the image itself. context is the directory sent to the builder, dockerfile is relative to it, args sets ARG values and target picks a stage of a multi-stage Dockerfile. With both build and image, the built image gets that name.

Fragment

Docker & Kubernetes · Build service images from source
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
services:
  api:
    build:
      context: ./api
      dockerfile: Dockerfile
      args:
        BUILD_MODE: release
      target: runtime
    image: shop-api:dev

up does not rebuild an image that already exists. Use docker compose up --build (or docker compose build) after changing source or the Dockerfile.

Start services in order and wait for health

Docker & Kubernetes

depends_on controls start order. On its own it only waits for the dependency container to start, not for the program inside to be ready. With condition: service_healthy, Compose waits until the dependency passes its healthcheck; restart: true also restarts the dependent service after Compose restarts or updates the dependency.

Fragment

Docker & Kubernetes · Start services in order and wait for health
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
services:
  api:
    image: ghcr.io/example/shop-api:1.4.2
    depends_on:
      db:
        condition: service_healthy
        restart: true
  db:
    image: postgres:18
    environment:
      POSTGRES_PASSWORD: example
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 5s
      timeout: 3s
      retries: 10
      start_period: 10s

Other conditions are service_started (the default) and service_completed_successfully, for a one-off migration or seed job that must exit 0 first.

Ordering covers startup only. The application should still retry connections, because a dependency can restart later.

Set environment and interpolate variables

Docker & Kubernetes

environment and env_file set variables inside the container. ${VAR} in the Compose file itself is interpolated from your shell and from a .env file next to compose.yaml before the file is used; ${VAR:-default} supplies a fallback and ${VAR:?message} fails if it is unset. docker compose config prints the file after interpolation and merging.

Fragment

Docker & Kubernetes · Set environment and interpolate variables
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
services:
  api:
    image: ghcr.io/example/shop-api:${API_TAG:-latest}
    env_file:
      - ./api.env
    environment:
      LOG_LEVEL: ${LOG_LEVEL:-info}
      PORT: "8080"

# .env (read by Compose for interpolation):  API_TAG=1.4.2
# docker compose config   # shows image: ghcr.io/example/shop-api:1.4.2

The .env file feeds interpolation; it is not passed into containers unless a service also lists it in env_file. Values in environment override values from env_file.

Shell variables override .env values. Quote numbers and booleans in environment (PORT: "8080") so YAML keeps them as strings.

Connect services, publish ports and keep data

Docker & Kubernetes

Every project gets a default network on which services reach each other by service name (the api container connects to db:5432). ports publishes a container port on the host; services talking to each other need no ports entry. A named volume keeps data across container re-creation, and a relative bind mount (./config) shares a host directory.

Fragment

Docker & Kubernetes · Connect services, publish ports and keep data
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
services:
  api:
    image: ghcr.io/example/shop-api:1.4.2
    ports:
      - "127.0.0.1:8080:8080"
    volumes:
      - ./config:/etc/shop:ro
    networks: [backend]
  db:
    image: postgres:18
    environment:
      POSTGRES_PASSWORD: example
    volumes:
      - db-data:/var/lib/postgresql
    networks: [backend]
volumes:
  db-data:
networks:
  backend:

Named volumes are listed under the top-level volumes: key; Compose prefixes them with the project name (shop_db-data).

From postgres 18 the image keeps data under /var/lib/postgresql/18/docker, so the volume is mounted at /var/lib/postgresql; earlier images used /var/lib/postgresql/data. Check the image documentation for its data directory.

Run commands: exec versus run

Docker & Kubernetes

exec runs a command inside an already running service container, like docker exec. run starts a new one-off container for the service, which suits tests, migrations and shells; add --rm so it is removed afterwards. run starts the services it depends on but does not publish its ports unless you pass --service-ports.

Fragment

Docker & Kubernetes · Run commands: exec versus run
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
docker compose exec db psql -U postgres     # inside the running db container
docker compose run --rm api npm test          # new one-off container
docker compose run --rm --no-deps api sh      # without starting dependencies
docker compose run --rm --service-ports api   # with the service's ports published

exec fails if the service is not running. A one-off run container gets a generated name and does not replace the service container.

Layer files and optional services

Docker & Kubernetes

Compose automatically merges compose.override.yaml over compose.yaml, which is the usual place for development-only settings. -f names the files explicitly, later files win, and passing -f turns off the automatic override. Services with profiles start only when that profile is active.

Fragment

Docker & Kubernetes · Layer files and optional services
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
# compose.override.yaml (merged automatically)
services:
  api:
    environment:
      LOG_LEVEL: debug
    ports:
      - "127.0.0.1:9229:9229"
  mailpit:
    image: axllent/mailpit:v1.33
    profiles: [debug]

# docker compose -f compose.yaml -f compose.prod.yaml up -d
# docker compose --profile debug up -d

Single values such as image or environment keys are replaced; lists such as ports are combined. docker compose config shows the merged result.

COMPOSE_PROFILES=debug in the environment is equivalent to --profile debug. Services without profiles always start.

Provide secrets as files

Docker & Kubernetes

A top-level secrets entry names a secret and its source (file or environment). A service that lists it gets it as a read-only file at /run/secrets/<name>, which keeps the value out of docker inspect output and the environment. Many official images accept *_FILE variables for this.

Fragment

Docker & Kubernetes · Provide secrets as files
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
services:
  db:
    image: postgres:18
    environment:
      POSTGRES_PASSWORD_FILE: /run/secrets/db_password
    secrets:
      - db_password
secrets:
  db_password:
    file: ./secrets/db_password.txt

Keep the secret file out of version control. Values in environment: are visible to anything that can inspect the container.

Sync or rebuild on file changes

Docker & Kubernetes

develop.watch rules make Compose react to edits: sync copies changed files into the running container, sync+restart also restarts it, and rebuild builds a new image and replaces the container. Start it with docker compose watch or docker compose up --watch.

Fragment

Docker & Kubernetes · Sync or rebuild on file changes
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
services:
  web:
    build: .
    develop:
      watch:
        - action: sync
          path: ./src
          target: /app/src
        - action: rebuild
          path: package.json

# docker compose up --watch

Watch applies to services built from a build: section. sync suits code that the app reloads itself; dependency manifests usually need rebuild.