Docker Compose Best Practices for Home Servers
Structure Compose stacks, volumes, networks, secrets, healthchecks, logging and updates so your home server stays clean, secure and easy to rebuild.
Why conventions matter
A home server can easily grow to 30+ containers. Without a consistent structure, you end up with mystery volumes, :latest tags that broke six months ago, and no idea how to rebuild after a disk failure. These practices keep your Docker homelab maintainable, secure and reproducible. If you're new to Compose, start with Docker basics.
1. One folder (and stack) per app group
Give each service or logically related group its own directory with its own compose.yaml:
~/docker/
nextcloud/compose.yaml
media/compose.yaml # jellyfin + arr stack together
monitoring/compose.yaml # grafana + prometheusThis isolates failures, makes updates independent, and lets you back up or move one app without touching the rest. Use the file name compose.yaml (the current convention) and omit the obsolete top-level version: line, which modern Compose ignores.
2. Keep everything in version control
Store your compose files (and non-secret config) in a Git repository. This gives you history, easy rollbacks, and a documented source of truth. Commit after every change; your future self will thank you when rebuilding. Keep secrets out of the repository (see section 5).
3. Always use named volumes or bind mounts
Never let important data live only inside a container — it vanishes when the container is recreated. Mount persistent data explicitly:
services:
app:
image: my/app:1.4.2
volumes:
- ./config:/config # bind mount, easy to back up
- app_data:/var/lib/app # named volume, managed by Docker
volumes:
app_data:Bind mounts (like ./config) are the easiest to back up because you can see them in the filesystem. Put bulk media on a separate path, mounted read-only (:ro) wherever the app doesn't need to write. Databases belong on local disks, not network shares.
4. Pin image tags — avoid :latest
:latest means "whatever is newest today", which makes builds unpredictable and upgrades surprising. Pin a specific major/minor version:
image: nextcloud:29-apache # not nextcloud:latestUpdate deliberately by bumping the tag, reading release notes, and testing — not by accident. For stricter reproducibility you can pin an image digest (image: name@sha256:...).
5. Handle secrets safely
Don't hard-code passwords in compose.yaml (which lives in Git). Use an .env file that is git-ignored and has tight permissions:
environment:
- DB_PASSWORD=${DB_PASSWORD}# .env (add to .gitignore, chmod 600)
DB_PASSWORD=super-secret-valueFor higher security, use Compose secrets: (files mounted into the container) for images that support _FILE variables, or a secrets manager. Rotate any password that was ever committed to a repository.
6. Set restart policies and healthchecks
Make services resilient across reboots and crashes:
restart: unless-stopped
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:80/health"]
interval: 30s
timeout: 5s
retries: 3
start_period: 30sA healthcheck lets Docker know a container is truly ready, not just running. Combine it with depends_on so dependent services wait for a healthy database:
depends_on:
db:
condition: service_healthy7. Use explicit networks and a reverse proxy
Create a shared network for web apps and put a reverse proxy in front instead of publishing a dozen ports:
networks:
proxy:
external: true # created once with: docker network create proxy
backend:
internal: true # no internet access, for databasesContainers reach each other by name on the same network; you expose only 80/443 to your LAN. Don't publish database ports. See Docker networking explained.
8. Limit privileges
Containers run as root by default. Reduce the blast radius when an app is compromised:
user: "1000:1000"
read_only: true
tmpfs:
- /tmp
cap_drop:
- ALL
security_opt:
- no-new-privileges:trueNot every image tolerates all of these options, so add them one at a time. Avoid privileged: true and don't mount the Docker socket unless the app truly needs it. See the security checklist.
9. Limit logs and resources
Container logs grow without bound by default. Cap them per service, or globally in /etc/docker/daemon.json:
logging:
driver: json-file
options:
max-size: "10m"
max-file: "3"Stop one runaway container from starving the host:
mem_limit: 1g
cpus: 1.510. Use profiles, anchors and overrides sparingly
Profiles let optional services live in the same file (profiles: ["debug"], started with --profile debug). YAML anchors reduce repetition for shared settings such as logging. Keep these simple; readability beats cleverness.
11. Plan updates and cleanup
- Update images intentionally:
docker compose pull && docker compose up -d. - Validate the file with
docker compose configbefore applying. - Read release notes for anything with a database.
- Prune periodically:
docker image prunereclaims space from old layers. - Back up volumes before major upgrades.
12. Make rebuilds boring
The end goal: if your server died today, you could restore from Git (compose files) plus your volume backups and be running again in under an hour. Test that assumption occasionally on a spare machine — a backup you've never restored is just a hope. Our backup strategy guide shows how.
A checklist for every new stack
- Own folder and
compose.yamlin Git - Image tag pinned
- Data in bind mounts or named volumes, and included in backups
- Secrets in
.env, not in the file - Healthcheck and restart policy
- On the right network; only necessary ports published
- Log rotation configured
- Monitored with Uptime Kuma
Next steps
Apply these patterns as you deploy services like Nextcloud, the *arr stack, and monitoring, and manage them visually with Portainer.
Artículos relacionados
Docker Networking Explained for Home Servers
Bridge, host, macvlan and internal Docker networks explained: container DNS, publishing ports, reverse proxy patterns, firewall pitfalls and a troubleshooting checklist.
Cómo instalar Docker en Ubuntu
Instala Docker Engine y el plugin Compose desde el repositorio oficial de Docker en Ubuntu Server, verifica la instalación, ejecuta tu primer contenedor y soluciona los errores habituales.
Fundamentos de Docker para autoalojamiento
Aprende imágenes, contenedores, volúmenes y Docker Compose para desplegar aplicaciones autoalojadas con confianza.