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:
docker compose config --images | sort nginx:alpine postgres:17.6-alpine
Here is what each marked part does:
- 1
--imagesprints only the image names, and not the whole plan. - 2
| sortsends that list tosort, 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.
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.
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
up -dstarts the services in the background, so the terminal is free again. - 2
-f '{{range ...}}'is a template. It prints each environment variable of the container on its own line. - 3
| grep POSTGRESkeeps 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.
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?
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
exec web kill 1runs the commandkill 1inside the runningwebcontainer. Process 1 is the main process of the container, so it ends. - 2
--format '...'prints only the fields that we name in the template: the service name and its status. - 3
ps -aalso 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.
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:
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
.HostConfig.Memoryis the memory limit of the container, in bytes. - 2
.HostConfig.LogConfigholds 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.
Clean up with docker compose --profile tools down -v in the app folder.
Key points
docker compose configshows the plan as it will run, with.envvariables filled in and override files merged.- Pass passwords as
secrets:, read from/run/secrets/…, not as environment variables thatdocker inspectprints. restart: unless-stoppedstarts 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.yamland the laptop’s extras incompose.override.yaml.
Next, in the last part: the whole port in one picture, and where the picture stops being true.