One Shared Reverse Proxy Can Route Many Compose Projects by Host Name

In progress Docker Networking

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

  1. Create an external network once: docker network create webproxy.
  2. Run the proxy (nginxproxy/nginx-proxy) on ports 80 and 443, attached to webproxy, with read access to the Docker socket so it can watch containers.
  3. Run nginxproxy/acme-companion beside it, sharing the certificate volumes.
  4. In each project, attach the web service to webproxy and 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

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.

Flashcard acme-companion issues certificates, but the sites keep serving the old ones. What's missing?

The companion can't find the proxy to reload it, typically because the proxy has a custom container_name in another Compose project. Set NGINX_PROXY_CONTAINER on the companion.