Docker lab/version note: use supported Linux-container tools and patched images. Check version compatibility before running the examples. Docker, Swarm and platform-specific deployment steps were not tested live.
What this guide covers
Docker Compose starts related containers from one YAML file. This guide runs a web server, a private PostgreSQL database and a separate database-check command, with a named volume for database files.
Before you start
Install the Compose plugin. In a new directory, copy server.mjs and Dockerfile from the Node.js guide. The sample web server is not a database client; the db-check service tests the database connection separately.
Step 1: Define the services
Save this as compose.yaml:
services:
web:
build: .
image: node-compose-lab:1.0
environment:
PORT: '3000'
APP_VERSION: compose-lab
ports:
- '127.0.0.1:8080:3000'
depends_on:
db:
condition: service_healthy
networks: [backend]
read_only: true
tmpfs: [/tmp]
cap_drop: [ALL]
security_opt: ['no-new-privileges:true']
db:
image: postgres:17-bookworm
environment:
POSTGRES_USER: app
POSTGRES_DB: app
POSTGRES_PASSWORD_FILE: /run/secrets/db_password
secrets: [db_password]
volumes:
- pgdata:/var/lib/postgresql/data
networks: [backend]
healthcheck:
test: ['CMD-SHELL', 'pg_isready -U app -d app']
interval: 5s
timeout: 3s
retries: 12
start_period: 10s
db-check:
image: postgres:17-bookworm
profiles: [tools]
environment:
PGHOST: db
PGUSER: app
PGDATABASE: app
secrets: [db_password]
networks: [backend]
entrypoint: ['/bin/sh', '-ec']
command:
- 'export PGPASSWORD="$$(cat /run/secrets/db_password)"; exec psql -c "SELECT current_database(), current_user;"'
networks:
backend:
volumes:
pgdata:
secrets:
db_password:
file: ./secrets/db_password.txt
The database is reached by its service name db from other containers. It has no public host port. pgdata keeps its data when the container is replaced. The password is supplied through a mounted file, not committed in YAML.
Step 2: Create the password and start
Keep secrets/, .env files and backups out of Git and the build context. Then run:
mkdir -p secrets
chmod 700 secrets
umask 077
openssl rand -hex 24 > secrets/db_password.txt
docker compose config --quiet
docker compose up -d --build
docker compose ps
curl --fail http://127.0.0.1:8080/healthz
docker compose run --rm db-check
docker compose logs --tail 60 db
The database health check controls startup ordering, but your real application still needs connection retries. On first startup only, PostgreSQL creates the configured user and database. Changing the password file later does not update an existing database role.
Step 3: Back up before deleting data
Create a logical database backup:
mkdir -p backups
chmod 700 backups
umask 077
docker compose exec -T db pg_dump -U app -d app -Fc > backups/app.dump
test -s backups/app.dump
docker compose exec -T db pg_restore --list < backups/app.dump
docker compose down
This uses PostgreSQL’s backup tool, not a copy of changing raw database files. Store the backup securely off the host and test restoration into a separate database.
Step 4: Stop without removing the volume
Use docker compose down to remove the test containers and network. Do not add --volumes unless you intentionally want to delete the database data. Use an application with a real database client before claiming end-to-end application/database integration.
Check the result
The web request should succeed and db-check should authenticate to the intended database. Confirm data survives ordinary container replacement, then test recovery independently.
Conversation
0 commentsNo comments yet. Start the conversation below.