One Shared Reverse Proxy Can Route Many Compose Projects by Host Name
Only one process on a server can listen on ports 80 and 443. To host several Compose projects on one VPS, a single proxy container owns those ports and forwards each request to the right project by its host name. With nginx-proxy, projects don't publish any port at all: they join a shared Docker network and declare their domain in an environment variable, and the proxy reconfigures itself as containers start and stop. A companion container adds Let's Encrypt certificates the same way.
How the Pieces Connect
- Create an external network once:
docker network create webproxy. - Run the proxy (
nginxproxy/nginx-proxy) on ports 80 and 443, attached towebproxy, with read access to the Docker socket so it can watch containers. - Run
nginxproxy/acme-companionbeside it, sharing the certificate volumes. - In each project, attach the web service to
webproxyand set:
services:
web:
image: myapp:1.4.2
expose: ["8000"]
environment:
VIRTUAL_HOST: ${VIRTUAL_HOST}
VIRTUAL_PORT: "8000"
LETSENCRYPT_HOST: ${VIRTUAL_HOST}
networks: [default, webproxy]
restart: unless-stopped
networks:
webproxy:
external: true
The database stays on the project's default network only, unreachable from the proxy or other projects.
Keep Each Project Portable
Read domains and secrets from a .env file (VIRTUAL_HOST=${VIRTUAL_HOST}) rather than hard-coding them, so the same Compose file deploys to staging and production.
Behind the proxy, the app receives plain HTTP: it must trust the proxy's X-Forwarded-Proto header to know the original request was HTTPS, or it builds http:// links.
References
- Docker and Container Security MOC - hub for Docker notes.
- nginx-proxy on GitHub -
VIRTUAL_HOST,VIRTUAL_PORTand the shared network. - acme-companion on GitHub -
LETSENCRYPT_HOSTandNGINX_PROXY_CONTAINER.
Review cards
Flashcard Why does a single proxy container manage multiple Compose projects?
A single proxy container manages multiple Compose projects because only one process can listen on ports 80 and 443 on a server, and the proxy forwards requests to the correct project based on its host name.
Flashcard How does the proxy container know which project to route a request to?
The proxy container routes requests to the correct project based on the VIRTUAL_HOST environment variable set in each project's configuration.