← Back to Notes
DockerJuly 2, 2026

How to Write a docker-compose.yml for Self-Hosting Any App

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

  1. Create a folder for the project, and inside it, a file named exactly docker-compose.yml.
  2. Define one service under services:, using an existing image rather than building your own unless you actually need to.
  3. Map a port so you can reach it from your browser: "HOST_PORT:CONTAINER_PORT".
  4. Pass configuration through environment: instead of hardcoding it into the image.
  5. Add a named volume if the app needs to persist data (workflows, database files) across restarts, otherwise a fresh container wipes everything.
  6. Set restart: unless-stopped so it comes back automatically after a crash or a full machine reboot.
  7. Start it with docker compose up -d, stop with docker 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: