Host Multiple Docker Compose Projects behind a Shared nginx-proxy
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
- A Debian or Ubuntu server with Docker Engine and the Compose plugin (see Install Docker Engine and the Compose Plugin on Debian or Ubuntu).
- A DNS
Arecord for every domain and subdomain, pointing to the server's public IP. - Ports 80 and 443 open in the firewall and in the provider's security group.
Steps
- 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/ - Create the network that joins the proxy to the projects, once:
docker network create webproxy - Start the proxy stack in
~/nginx/docker-compose.yml:
Then runservices: 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}}docker compose up -din that folder. - 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:
Publish no host ports on the project's services: the proxy is the only entry point, and the database stays on the project's privateservices: 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: truedefaultnetwork. - 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-proxyshows a server block generated for eachVIRTUAL_HOST. -
docker logs nginx-proxy-acme(or the companion's container name) shows a certificate issued for eachLETSENCRYPT_HOST. -
https://project1.example.comloads with a valid certificate, andhttp://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 samehtmlvolume, mounted at/usr/share/nginx/htmlin 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_ALERTafter a reboot: the proxy came back but the project's container didn't, so the proxy has no backend for that name. Addrestart: unless-stoppedand 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'sX-Forwarded-Proto. For Uvicorn, add--proxy-headers --forwarded-allow-ipswith 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.venvwas copied into the image. Add.venvto.dockerignoreand 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
- One Shared Reverse Proxy Can Route Many Compose Projects by Host Name - why one proxy on 80/443 and an external network.
- Docker and Container Security MOC - hub for Docker notes.
- nginx-proxy documentation -
VIRTUAL_HOST,VIRTUAL_PORTand the socket mount. - acme-companion documentation -
LETSENCRYPT_HOSTand the shared volumes. - Let's Encrypt: Challenge Types - why HTTP-01 validation needs port 80.