Docker Compose on a VPS: a production setup you can maintain
In this article13 sections
Docker Compose makes it easy to run an application and its database on one server: write a file, run one command, and everything starts. That ease is also a trap. A setup copied from a tutorial often works on day one and then slowly goes wrong: images that change under you because they were pulled as latest, a database port open to the internet, logs that fill the disk, no backups of the data in the volumes, and updates that nobody dares to run.
This guide turns a tutorial setup into something you can maintain. It assumes an Ubuntu VPS with Docker and the Compose plugin already installed (see the SiteHarbour guide to installing Docker on Ubuntu 24.04 safely) and covers the choices that decide whether the setup survives its first year.
When Compose is the right tool
Compose suits a single server running a handful of services: a web app and its database, a self-hosted tool like a wiki or an automation platform, a small internal system. It is not a cluster manager. If you need several servers, automatic failover or rolling updates across machines, you are looking at a different tool. For one VPS, Compose is simple and sufficient, if you set it up with care.
1. A layout you can read in a year
Keep each application in its own folder, with everything that defines it in one place:
/opt/myapp/
compose.yaml the services
.env settings and secrets (permissions 600, not in git)
data/ bind-mounted data, or leave it to named volumes
backup.sh the backup script
README.md what this is, how to update, how to restore
Keep compose.yaml and the README in version control and keep .env out of it. A README.md that says how to start, update, back up and restore saves hours later.
sudo mkdir -p /opt/myapp && cd /opt/myapp
sudo chown $USER: /opt/myapp
chmod 600 .env
2. Pin your images
image: postgres:latest means “whatever is current on the day you pull”. After a restart or an update, you may suddenly be running a different major version, and databases do not accept that quietly. Pin a specific version, and read the release notes when you change it:
services:
db:
image: postgres:16.4
app:
image: ghcr.io/example/app:2.8.1
For critical services you can also pin by digest (image: name@sha256:...) so that the image cannot change even if the tag is moved. Prefer images from the project’s own organisation, and avoid unknown images from random accounts: you are giving them a place on your server.
3. Publish as little as possible
Every ports: line opens a door on the server. Only one thing should face the internet: a reverse proxy that terminates HTTPS. Everything else talks over Compose’s internal network and publishes nothing, or binds to the local machine only:
services:
app:
image: ghcr.io/example/app:2.8.1
ports:
- "127.0.0.1:8080:8080" # reachable only from this server, for the proxy
db:
image: postgres:16.4
# no "ports": the app reaches it by the name "db" on the internal network
An important trap: Docker adds its own firewall rules, and ports published to all addresses can be reachable from the internet even when UFW says they are blocked. Do not rely on UFW for published container ports. Bind to 127.0.0.1 and put a proxy in front, and check from outside:
sudo ss -tulpn | grep -E 'docker|:8080|:5432'
nmap -Pn -p 1-10000 your-server-ip
Run the nmap from another machine. The only open ports should be the ones you intend (typically 22, 80 and 443).
The proxy can be NGINX on the host (proxying to 127.0.0.1:8080) or another container such as Caddy or Traefik that handles certificates for you. Pick one approach and keep it consistent across applications.
4. Secrets and settings
Passwords belong in .env, referenced from compose.yaml, not typed into the file that goes in git:
services:
db:
image: postgres:16.4
environment:
POSTGRES_PASSWORD: ${DB_PASSWORD}
with .env containing DB_PASSWORD= and a long random value (openssl rand -base64 24). Compose reads .env from the same folder automatically. Never put credentials in an image or commit them. Check that .env is not world-readable (ls -l .env should show -rw-------).
5. Restart policy, health checks and limits
A server reboots; a container crashes. Tell Compose what to do:
app:
restart: unless-stopped
healthcheck:
test: ["CMD", "wget", "-qO-", "http://localhost:8080/health"]
interval: 30s
timeout: 5s
retries: 3
mem_limit: 512m
cpus: 1.0
restart: unless-stoppedbrings the service back after a crash or a reboot, as long as Docker itself starts at boot (sudo systemctl enable docker).- A health check lets you see whether a container that is “running” is actually working. Use a command that exists inside the image.
- Memory and CPU limits keep one misbehaving service from taking down the server. Without a memory limit, a leak can trigger the kernel’s out-of-memory killer, and the victim is often your database.
6. Run containers with fewer privileges
Where the image supports it, tighten the defaults:
app:
user: "1000:1000"
read_only: true
tmpfs:
- /tmp
cap_drop:
- ALL
security_opt:
- no-new-privileges:true
Not every image tolerates all of these, so add them one at a time and test. And two rules without exceptions: do not mount /var/run/docker.sock into a container unless you understand that it hands that container control of the whole host, and do not run containers as privileged.
7. Control the logs
By default, Docker keeps every container’s output in an ever-growing file, which is a classic cause of a full disk. Cap it, either in each service or for the whole host:
app:
logging:
driver: json-file
options:
max-size: "10m"
max-file: "3"
To set it for everything, put the same options in /etc/docker/daemon.json and restart Docker (this applies to containers created afterwards). See how to find what is eating your disk and check docker system df now and then.
8. Back up the data, not the containers
Containers are disposable; their data is not. Know where each service keeps its data (a named volume or a bind-mounted folder), and back up two things:
The databases, as proper dumps, because copying a running database’s files can produce a broken backup:
docker compose exec -T db pg_dump -U postgres appdb | gzip > /opt/backups/appdb-$(date +%F).sql.gz
docker compose exec -T mariadb mariadb-dump -u root -p"$DB_ROOT_PASSWORD" --single-transaction appdb | gzip > /opt/backups/appdb-$(date +%F).sql.gz
(Use the line that matches your database.) The application’s files and uploads, from the volume, for example with a temporary container:
docker run --rm -v myapp_uploads:/data -v /opt/backups:/backup alpine tar czf /backup/uploads-$(date +%F).tar.gz -C /data .
Copy the results off the server, keep several generations, and test a restore onto a clean machine. Back up compose.yaml and .env as well (the .env file encrypted or in a password manager), because you will need the exact configuration to bring the service back.
9. Update calmly
Updating is where setups usually die, because nobody remembers the procedure. Write it down and follow it:
cd /opt/myapp
# 1. back up first (database dump and volumes)
./backup.sh
# 2. read the release notes for the new version, then edit the image tag in compose.yaml
# 3. pull and recreate
docker compose pull
docker compose up -d
# 4. check
docker compose ps
docker compose logs --tail=50 app
# 5. later, remove old images
docker image prune -f
Update one service at a time, and the database last and with the most care (a major database version change often needs a dump and restore, not just a new tag). Keep the previous image tag in the file’s history so that you can go back; and remember that a database that has migrated its data may not go back without a restore from backup.
10. Watch it
docker compose psanddocker stats --no-streamshow state and resource use.- An external monitor on the site and a heartbeat on the backup job tell you when it breaks (see monitoring a small server).
- Subscribe to the security announcements of the images you use, and update on a schedule, not only when something is on fire.
A short checklist
- One folder per app, with a README and
.envat600. - Images pinned to versions.
- Only the proxy faces the internet; everything else bound to
127.0.0.1or internal; checked with a scan from outside. - Secrets in
.env, never in git. - Restart policy, health checks and memory limits.
- Reduced privileges where possible; no Docker socket mounts.
- Log size limits.
- Database dumps and volume backups, copied off the server and restored once.
- A written update procedure.
- External monitoring.
When it is worth getting help
Docker deployments are quick to start and easy to leave in a state nobody understands. If you want an application deployed on a VPS with Docker or Compose, secured and backed up, or you have inherited a server full of containers and want to know what is running and how safe it is, I can set it up or review it.
I deploy Docker, Node.js, MERN and LAMP/LEMP applications and many self-hosted tools. You can see my profile on Upwork, or read about software and app installation.