In short: Docker Compose is a perfectly good way to run a small or medium application on one server, as long as you treat the compose file like production code. Pin image versions, add healthchecks, keep secrets out of Git, limit logs and resources, never publish your database port, and update with a repeatable command.
A compose file that works on your laptop is not ready for a server. The typical failures are boring and expensive: a latest image that changed overnight, a container that restarted before the database was ready, a log file that filled the disk, or a database port open to the whole internet. This guide lists ten practices that prevent these outages, then puts them together in one complete file. It builds on our beginner guide to installing PocketBase with Docker Compose.
Prerequisites
- A Linux server with Docker Engine and the Compose plugin (check with
docker compose version) - An application image you can pull from a registry, or one you build
- Basic familiarity with YAML and the command line
- A domain and a reverse proxy if the app is public (see our Caddy deployment guide)
1. Pin image versions
image: postgres:latest means your next deployment may install a different major version and break your data directory. Pin a specific tag that you have tested, for example postgres:17, and for your own application use an explicit version tag such as 1.4.2. For stricter reproducibility, pin the image digest. Upgrade on purpose, not by accident.
2. Set a restart policy
Servers reboot and processes crash. Use restart: unless-stopped so containers come back after a reboot or failure, but stay down if you stopped them deliberately.
3. Add healthchecks and wait for readiness
A running container is not necessarily a ready one. Define a healthcheck and make dependent services wait for it:
yamldb:
healthcheck:
test: ["CMD-SHELL", "pg_isready -U app -d app"]
interval: 10s
timeout: 5s
retries: 5
app:
depends_on:
db:
condition: service_healthy
This removes the classic race where your application starts before PostgreSQL accepts connections.
4. Keep secrets out of Git and out of the compose file
Never commit passwords. Put them in an .env file that is listed in .gitignore and referenced with env_file, or better, use file-based secrets. The official PostgreSQL image supports the _FILE convention, which reads a password from a file instead of an environment variable:
yamlservices:
db:
environment:
POSTGRES_PASSWORD_FILE: /run/secrets/db_password
secrets:
- db_password
secrets:
db_password:
file: ./secrets/db_password.txt
Restrict the file permissions (chmod 600) and back the secrets up in a password manager.
5. Use named volumes for data and plan backups
Store database data in a named volume so it survives container recreation. A volume is not a backup: schedule dumps or snapshots and test a restore. For PostgreSQL, a nightly pg_dump copied off the server is a solid baseline.
6. Do not publish ports you do not need
Containers on the same Compose network can already reach each other by service name. Never add ports: "5432:5432" to your database. If only a reverse proxy on the host needs to reach the app, bind it to the loopback interface:
yamlapp:
ports:
- "127.0.0.1:3000:3000"
7. Limit logs
By default, the json-file logging driver keeps growing. A chatty application can fill your disk in weeks. Set a size and a file count:
yamllogging:
driver: json-file
options:
max-size: "10m"
max-file: "3"
8. Limit memory and CPU
One runaway container should not take down the whole server. Set limits that match your hardware:
yamlmem_limit: 512m
cpus: 1.0
Watch real usage with docker stats for a few days before choosing numbers.
9. Run with least privilege
Where the image allows it, run as a non-root user, use a read-only filesystem with a temporary directory for scratch files, and drop capabilities you do not need:
yamluser: "1000:1000"
read_only: true
tmpfs:
- /tmp
cap_drop:
- ALL
security_opt:
- no-new-privileges:true
Test each option, because some applications need to write to specific paths.
10. Update with a repeatable command
Updating should be one command that you can also run in a pipeline:
bashdocker compose pull
docker compose up -d --remove-orphans
docker image prune -f
Compose recreates only the services whose image changed. Always take a database backup before a version change, and keep the previous image tag handy for a fast rollback.
A complete example
This file combines the practices above for an application with a PostgreSQL database. Adapt the names and versions:
yamlservices:
db:
image: postgres:17
restart: unless-stopped
environment:
POSTGRES_USER: app
POSTGRES_DB: app
POSTGRES_PASSWORD_FILE: /run/secrets/db_password
secrets:
- db_password
volumes:
- db_data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U app -d app"]
interval: 10s
timeout: 5s
retries: 5
mem_limit: 512m
logging:
driver: json-file
options:
max-size: "10m"
max-file: "3"
app:
image: ghcr.io/your-org/your-app:1.4.2
restart: unless-stopped
env_file: .env
depends_on:
db:
condition: service_healthy
ports:
- "127.0.0.1:3000:3000"
mem_limit: 512m
cpus: 1.0
user: "1000:1000"
read_only: true
tmpfs:
- /tmp
logging:
driver: json-file
options:
max-size: "10m"
max-file: "3"
volumes:
db_data:
secrets:
db_password:
file: ./secrets/db_password.txt
You do not need the old top-level version: key: current Compose releases ignore it. Validate your file before every deployment:
bashdocker compose config
A pre-deployment checklist
Before the first real deployment, and again after any large change, walk through this list:
- Every image has a pinned tag, and you know which version of each service is running
- Healthchecks exist for the database and for the application, and the dependent services wait for them
- No secret appears in the repository, in the compose file or in the shell history
- Only the reverse proxy is reachable from the internet, and the firewall allows only ports 22, 80 and 443
- Log rotation and resource limits are set on every service
- A backup ran last night, it is stored off the server, and you restored it at least once on another machine
- You can roll back: the previous image tag is known and the command is written down
- Someone other than you can find this checklist and run the update
Treat the list as a living document in your repository, next to the compose file. When an incident teaches you something, add a line.
Troubleshooting
- The app crashes at startup with a connection error: the database is not ready. Add the healthcheck and
condition: service_healthy. - The disk is full: check
docker system dfand add log rotation. - A container is killed unexpectedly: it hit
mem_limit. Look atdocker statsand raise the limit or fix the leak. - Changes to
.envare ignored: recreate the service withdocker compose up -d, because a restart does not reload environment variables. - Permission denied on a volume: align the container’s
userwith the file ownership on the host.
FAQ
Is Docker Compose good enough for production?
For a single server and a modest number of services, yes. If you need automatic scheduling across several machines or rolling updates with no downtime, look at an orchestrator such as Kubernetes or Nomad.
Should I use latest for my own application image?
No. Tag each build with a version or a commit hash so you can roll back and know what is running.
Where do I store the .env file?
On the server, outside the repository, with restricted permissions. Keep a copy of the values in a password manager.
How do I get zero-downtime updates?
Plain Compose recreates containers, which causes a short interruption. For many apps that is acceptable. If it is not, run two instances behind a reverse proxy and update them one at a time.
Next steps
Put the file in a pipeline with our guide to deploying to a VPS with GitHub Actions, and choose the right engine for your data with PostgreSQL vs SQLite vs MySQL. The official Docker Compose documentation covers every option mentioned here.