Docker · Containers in practice
How do you put a reverse proxy in front of containers?
Short answer
Run a proxy such as nginx, Traefik or Caddy on a shared network, route by host or path to service names, terminate TLS there, and forward the correct client headers.
The proxy is the only service that publishes ports 80 and 443. Application containers stay on an internal network and are reached by service name. This centralises TLS, routing, compression and request limits.
server {
listen 443 ssl;
server_name api.example.com;
ssl_certificate /etc/letsencrypt/live/api/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/api/privkey.pem;
location / {
proxy_pass http://api:3000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 60s;
}
}Things that bite
- WebSocket upgrades need explicit Connection and Upgrade headers
- Large uploads need a body size limit and a longer timeout
- The app must trust forwarded headers only from the proxy
- TLS certificates need renewal and a reload strategy
- Health checks should target the proxy and the app separately
Traefik and Caddy can discover containers through labels or the Docker API and manage certificates automatically. That reduces configuration, but it also means the proxy needs access to the Docker socket or a discovery mechanism, which is a security consideration.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1Why should the application not publish a host port when a proxy is used?
- 2What headers must the proxy forward for correct client IPs?
- 3How do you support WebSockets through the proxy?
- 4What is the security concern with Docker label discovery?
Say it in the interview
I run a reverse proxy on a shared Docker network and let it own ports 80 and 443, while application containers stay internal and are reached by service name. The proxy terminates TLS and forwards Host, X-Forwarded-For and X-Forwarded-Proto. I also configure WebSocket headers, upload limits and certificate renewal, because those are the usual causes of production surprises.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Docker mock interview and get scored feedback on depth, clarity and confidence.