Host Multiple Docker Compose Projects behind a Shared nginx-proxy

In progress Docker Networking

Goal

Run several independent Docker Compose projects on one server, each reachable on its own domain over HTTPS, behind a single reverse proxy that requests and renews Let's Encrypt certificates on its own.

Prerequisites

Steps

  1. Give the proxy and the projects their own folders, so that the proxy doesn't belong to any one project:
    ~/nginx/      # the shared proxy and its certificate companion
    ~/project1/   # one Compose project per folder
    ~/project2/
    
  2. Create the network that joins the proxy to the projects, once:
    docker network create webproxy
    
  3. Start the proxy stack in ~/nginx/docker-compose.yml:
    services:
      nginx-proxy:
        image: nginxproxy/nginx-proxy
        restart: always
        ports: ["80:80", "443:443"]
        volumes:
          - certs:/etc/nginx/certs:ro
          - vhost:/etc/nginx/vhost.d
          - html:/usr/share/nginx/html
          - /var/run/docker.sock:/tmp/docker.sock:ro
        networks: [webproxy]
      acme-companion:
        image: nginxproxy/acme-companion
        restart: always
        environment:
          - DEFAULT_EMAIL=you@example.com
          - NGINX_PROXY_CONTAINER=nginx-proxy
        volumes:
          - certs:/etc/nginx/certs:rw
          - vhost:/etc/nginx/vhost.d:rw
          - html:/usr/share/nginx/html:rw
          - acme:/etc/acme.sh
          - /var/run/docker.sock:/var/run/docker.sock:ro
        networks: [webproxy]
    volumes: {certs: {}, vhost: {}, html: {}, acme: {}}
    networks: {webproxy: {external: true}}
    
    Then run docker compose up -d in that folder.
  4. Declare each project's domain on its web service, and attach the service to both its own network and webproxy. The variables come from the project's .env:
    services:
      web:
        build: .
        restart: unless-stopped
        environment:
          - VIRTUAL_HOST=${VIRTUAL_HOST}          # project1.example.com
          - VIRTUAL_PORT=${VIRTUAL_PORT}          # the port the app listens on, e.g. 8000
          - LETSENCRYPT_HOST=${LETSENCRYPT_HOST}
          - LETSENCRYPT_EMAIL=${LETSENCRYPT_EMAIL}
        networks: [default, webproxy]
    networks:
      webproxy:
        external: true
    
    Publish no host ports on the project's services: the proxy is the only entry point, and the database stays on the project's private default network.
  5. Start the project with docker compose up -d. The proxy picks up the new container through the Docker socket, and the companion requests its certificate.

Verify

  • docker logs nginx-proxy shows a server block generated for each VIRTUAL_HOST.
  • docker logs nginx-proxy-acme (or the companion's container name) shows a certificate issued for each LETSENCRYPT_HOST.
  • https://project1.example.com loads with a valid certificate, and http:// redirects to it.
  • After sudo reboot, every site comes back without manual action.

Troubleshooting

  • Certificate validation returns 404: Let's Encrypt fetches /.well-known/acme-challenge/<token> over HTTP. The proxy and the companion must share the same html volume, mounted at /usr/share/nginx/html in both.
  • "Timeout during connect (likely firewall problem)": port 80 is blocked. Validation always starts on port 80, even for an HTTPS-only site: sudo ufw allow 80/tcp && sudo ufw allow 443/tcp, and check the provider's firewall too.
  • "DNS problem: NXDOMAIN": the record for that exact name (subdomains included) is missing or still points to an old server. Check with dig +short <name> before retrying.
  • HTTP 503 or ERR_SSL_UNRECOGNIZED_NAME_ALERT after a reboot: the proxy came back but the project's container didn't, so the proxy has no backend for that name. Add restart: unless-stopped and recreate the container (see Containers Without a Restart Policy Stay Down After a Host Reboot).
  • Pages load unstyled over HTTPS, with mixed-content errors: the app builds http:// URLs because it doesn't trust the proxy's X-Forwarded-Proto. For Uvicorn, add --proxy-headers --forwarded-allow-ips with the proxy's address (see An App Behind a TLS-Terminating Proxy Builds http URLs Unless It Trusts Forwarded Headers).
  • The container exits with exec /app/.venv/bin/uvicorn: no such file or directory: the local .venv was copied into the image. Add .venv to .dockerignore and rebuild with --no-cache (see Docker Build Context Isolation and Bind-Mount Masking and uv in a Multi-Stage Build Must Use the Image's Python or the Copied venv Breaks).

References