Use this as a starting template any time you're self-hosting a single service (n8n, a database, a small API) instead of writing raw docker run commands every time.
Steps
- Create a folder for the project, and inside it, a file named exactly
docker-compose.yml. - Define one service under
services:, using an existing image rather than building your own unless you actually need to. - Map a port so you can reach it from your browser:
"HOST_PORT:CONTAINER_PORT". - Pass configuration through
environment:instead of hardcoding it into the image. - Add a named volume if the app needs to persist data (workflows, database files) across restarts, otherwise a fresh container wipes everything.
- Set
restart: unless-stoppedso it comes back automatically after a crash or a full machine reboot. - Start it with
docker compose up -d, stop withdocker compose down.
Gotcha
Editing environment variables in the file does nothing until you actually restart the container. Run docker compose down && docker compose up -d after any change, a plain up -d while it's already running won't reapply new environment values.
Quick reference
version: '3.8'
services:
app:
image: some/image
restart: unless-stopped
ports:
- "5678:5678"
environment:
- SOME_VAR=value
volumes:
- app_data:/path/inside/container
volumes:
app_data: