Quick answer
What this guide helps you do
Understand Compose services, images, ports, volumes, networks and environment files, then validate and operate a small self-hosted stack safely.
What Compose does
Docker Compose describes one or more containers in a YAML file. It keeps services, images, networks, storage, ports and restart behaviour as rebuildable configuration.
A useful project layout is:
/opt/containers/example/
├── compose.yaml
├── .env
└── data/
The Compose file is the recipe; the data directory or named volumes are persistent state. Recovery usually requires both.
Read a small definition
services:
web:
image: nginx:alpine
ports:
- "127.0.0.1:8080:80"
volumes:
- ./site:/usr/share/nginx/html:ro
restart: unless-stopped
The host listens only on 127.0.0.1 port 8080, the local site directory is mounted read-only, and the service should return after Docker restarts unless deliberately stopped.
Validate before starting
sudo docker compose config
sudo docker compose pull
sudo docker compose up -d
sudo docker compose ps
sudo docker compose logs --tail=100
The config command catches YAML errors and renders variable substitutions. Its output may reveal secrets, so do not publish it without checking.
Understand lifecycle commands
stop preserves existing containers. start starts those stopped containers. up -d creates or recreates what the definition requires. down removes project containers and the default network. Avoid down -v unless deleting named volumes is intentional.
Handle secrets and versions
Keep passwords and tokens out of public repositories. Restrict environment files and provide a safe example containing names rather than real values.
sudo chmod 600 .env
sudo stat .env
Pin important image versions or documented release channels. A floating tag can introduce an unexpected change during a later pull.
Choose storage deliberately
Named volumes are managed by Docker. Bind mounts expose a chosen host path. Both require backups. Relative bind paths depend on the project location; inspect the effective mounts when troubleshooting.
Read Docker Volumes vs Bind Mounts before choosing.
Recovery checklist
- Compose validates cleanly.
- Published ports match the intended exposure.
- Persistent paths are documented.
- Secrets are protected.
- Image versions are deliberate.
- Application health and logs are checked.
- Services recover after reboot.
- An isolated restore has been tested.
This is a general operating pattern, not evidence that SmallGrid deployed the example service.
Next: Docker Container Cannot Access a Mounted Folder. Start with How to Install Docker on Ubuntu Server.
Official reference: Docker Compose.