A compose.yaml for a real server

The compose file from part 6 works on a laptop. On a server it shows its password, lets logs fill the disk and stays stopped after a crash. A few lines fix that. An override file keeps the laptop setup easy to work with.

Part 8 of 9 · Containers4 min read
memory: 512m and no morepassword: a file,not in docker inspectcrashes? starts againrestart: unless-stoppedlogs cut at 10 MBZZ3 a.m.: nobody awakethe file keeps watch

The compose file from part 6 runs well on a laptop. Copy it to a server and three things go wrong. The database password is written in the file, and anyone who can run docker inspect can read it. Logs grow until the disk is full. When a container crashes at night, it stays stopped until someone notices.

The plan with the server’s lines added

Here is the same plan with the server’s lines added. Below it, we look at the new lines one at a time. Save it as compose.yaml in a folder named app:

services:
  db:
    image: postgres:${PG_TAG:-17-alpine}
    environment:
      POSTGRES_PASSWORD_FILE: /run/secrets/db_password
    secrets:
      - db_password
    volumes:
      - dbdata:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD", "pg_isready", "-U", "postgres"]
      interval: 2s
    restart: unless-stopped
    mem_limit: 512m
    logging:
      options:
        max-size: 10m
        max-file: "3"

  web:
    image: nginx:alpine
    ports:
      - "127.0.0.1:8080:80"
    depends_on:
      db:
        condition: service_healthy
    restart: unless-stopped
    mem_limit: 128m
    logging:
      options:
        max-size: 10m
        max-file: "3"

  adminer:
    image: adminer
    profiles: [tools]
    ports:
      - "127.0.0.1:8081:8080"

secrets:
  db_password:
    file: ./db_password.txt

volumes:
  dbdata:

Put two small files next to it. The first is .env, with the line PG_TAG=17.6-alpine. The second is db_password.txt, with a password of your choice on one line. For the laptop section at the end, also make a site folder with an index.html in it.

Values from outside the file

${PG_TAG:-17-alpine} is a variable with a default. Compose fills it from your shell or from a .env file in the same folder. So the same plan can run a pinned version on the server. Before you start anything, ask Compose what it will run:

Shell
docker compose config --images | sort  
nginx:alpine
postgres:17.6-alpine

Here is what each marked part does:

  1. 1--images prints only the image names, and not the whole plan.
  2. 2| sort sends that list to sort, which puts the lines in alphabetical order.

docker compose config prints the plan with every variable filled in and every file merged. It is the first command to run when a server does something the file does not seem to say.

compose.yamlimage: postgres:${PG_TAG:-17-alpine}.envPG_TAG=17.6-alpinedocker compose configimage: postgres:17.6-alpinewhat will run
compose.yaml says ${PG_TAG}, .env says 17.6-alpine, and docker compose config shows what will run.

The password as a file

In part 9 we will read a password directly from docker inspect, because an environment variable is written into the container’s settings. A secret travels differently. Compose mounts it as a file at /run/secrets/db_password. The official Postgres image reads the password from that file, because we set POSTGRES_PASSWORD_FILE.

Shell
docker compose up -d 
docker inspect app-db-1 -f '{{range .Config.Env}}{{println .}}{{end}}' | grep POSTGRES  
POSTGRES_PASSWORD_FILE=/run/secrets/db_password

Here is what each marked part does:

  1. 1up -d starts the services in the background, so the terminal is free again.
  2. 2-f '{{range ...}}' is a template. It prints each environment variable of the container on its own line.
  3. 3| grep POSTGRES keeps only the lines that contain the word POSTGRES.

We removed the lines that up prints. The settings now hold only where the password is, not the password itself. Many official images accept a _FILE variant like this. For your own app, read the file when the app starts. There is one limit. Postgres reads the file only when it creates its data directory, on the first start with an empty volume. If dbdata already exists from part 6, the old password stays. Change it inside Postgres with ALTER USER, or start from an empty volume.

environment:secrets: POSTGRES_PASSWORD=example /run/secrets/db_passworddocker inspect reads itinspect shows only the path
As an environment variable, the password is in the container's settings. As a secret, it is a file inside the container.

Coming back after a crash

restart: unless-stopped tells Docker to start a container again whenever its process exits, unless a person stopped it. Make web crash by killing its main process from inside:

After docker compose stop web, will Docker start it again?

Shell
docker compose exec web kill 1 
sleep 3
docker compose ps web --format '{{.Service}} {{.Status}}' 
docker inspect app-web-1 -f 'restarts={{.RestartCount}}'
docker compose stop web
docker compose ps -a web --format '{{.Service}} {{.Status}}' 
web Up 2 seconds
restarts=1
Container app-web-1  Stopping
Container app-web-1  Stopped
web Exited (0) 2 seconds ago

Here is what each marked part does:

  1. 1exec web kill 1 runs the command kill 1 inside the running web container. Process 1 is the main process of the container, so it ends.
  2. 2--format '...' prints only the fields that we name in the template: the service name and its status.
  3. 3ps -a also lists containers that have stopped. Without -a, a stopped container is not shown.

Docker restarted web once after the crash, and left it stopped after we stopped it by hand. A restart policy also starts containers again when the Docker daemon or the server restarts.

exits restart: unless-stopped docker compose stopstays down

The process in web exits: a crash, or the kill we sent.

Limits for every container

Two more lines per service stop one container from hurting the rest of the host. mem_limit: 512m is the cgroup memory limit from part 1, written into the plan. It means the same as the longer deploy.resources.limits.memory. The logging options rotate each container’s log at 10 MB and keep three files. Without these options, Docker’s default log driver keeps everything a container ever printed, until the disk is full. Check that both arrived:

Shell
docker inspect app-db-1 -f 'memory={{.HostConfig.Memory}} log={{.HostConfig.LogConfig.Type}} {{.HostConfig.LogConfig.Config}}'  
memory=536870912 log=json-file map[max-file:3 max-size:10m]

Here is what each marked part does:

  1. 1.HostConfig.Memory is the memory limit of the container, in bytes.
  2. 2.HostConfig.LogConfig holds the log driver and its options, such as the size and the number of files.

One file for the server, one for the laptop

On a laptop you want more: the web server should show the page you are editing. If compose.override.yaml is next to compose.yaml, Compose reads it automatically and merges the two files:

services:
  web:
    volumes:
      - ./site:/usr/share/nginx/html:ro

On the laptop, docker compose up uses both files, and curl localhost:8080 shows your local site/index.html. On the server, do not copy the override file, or name the file explicitly with docker compose -f compose.yaml up -d. Then the bind mount is gone.

The adminer service has a profile, profiles: [tools]. A plain docker compose up -d skips it. docker compose --profile tools up -d starts it when you need a database screen. The rest of the time, it does not run.

compose.override.yaml: ./site → htmlcompose.yaml: db, web, secrets, limitson the laptopon the server
compose.yaml is the server. Laid over it, compose.override.yaml adds what only the laptop needs.

Clean up with docker compose --profile tools down -v in the app folder.

Key points

  • docker compose config shows the plan as it will run, with .env variables filled in and override files merged.
  • Pass passwords as secrets:, read from /run/secrets/…, not as environment variables that docker inspect prints.
  • restart: unless-stopped starts crashed containers again and does not start the ones you stopped.
  • Give every service a memory limit and log rotation, so one container cannot take all the host’s memory or fill its disk.
  • Keep the server’s plan in compose.yaml and the laptop’s extras in compose.override.yaml.

Next, in the last part: the whole port in one picture, and where the picture stops being true.

New posts by email

One email when a post ships, about twice a month. Nothing else, and one click to leave.

Latest: Finding where a request breaks · RSS