Nginx with Docker
Introduction
Docker packages apps in containers; Nginx often sits on the host proxying to published container ports—or runs inside a container as the front door. This chapter covers both patterns and links to the Linux Docker introduction for install basics.
Prerequisites
- Reverse Proxy Basics
- Linux Docker introduction
- Docker installed (
docker --version)
Pattern A: Host Nginx → Container Port
App container maps port to localhost:
docker run -d --name api -p 127.0.0.1:8080:8080 my-spring-api:latestHost Nginx:
location / {
proxy_pass http://127.0.0.1:8080;
include /etc/nginx/proxy_params;
}Code explanation:
-p 127.0.0.1:8080:8080— container 8080 only on host loopback, not public- Host Nginx on 80/443 remains the public entry—same security model as non-Docker deploy
Verify:
docker ps
curl http://127.0.0.1:8080/health
curl -H "Host: api.example.com" http://127.0.0.1/Pattern B: Nginx Container → App Container
Docker Compose network—services by name:
default.conf:
server {
listen 80;
location / {
proxy_pass http://api:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}Code explanation:
api:3000— Docker DNS resolves service name on compose network- Only Nginx publishes port 80 to host
Official nginx Image
Quick static test:
docker run -d --name web -p 8080:80 nginx:alpine
curl http://127.0.0.1:8080/Mount custom config:
docker run -d -p 8080:80 \
-v /path/to/site.conf:/etc/nginx/conf.d/default.conf:ro \
-v /path/to/html:/usr/share/nginx/html:ro \
nginx:alpineTLS with Docker
Common approaches:
| Approach | Notes |
|---|---|
| Certbot on host Nginx | Proxy to containers—simplest for one VPS |
| Certbot in container | Needs volume mounts for certs and webroot |
| External CDN TLS | Terminate at Cloudflare; origin HTTP |
This track’s Certbot chapter assumes host-installed Nginx—recommended for learners.
Reload Config in Container
docker exec web nginx -t
docker exec web nginx -s reloadOr recreate container when mounting configs from CI.
Logs
docker logs web
docker exec web tail -f /var/log/nginx/access.logHost Nginx logs stay in /var/log/nginx/ as usual.
Pitfalls
| Issue | Cause |
|---|---|
| 502 to container | Wrong service name / not on same network |
| Connection refused | Container not listening on expected port |
| Config not updating | Volume mount path wrong; forgot reload |
| Published 8080 publicly | Bind 127.0.0.1:8080:8080 when host Nginx fronts |
FAQ
Kubernetes replaces Compose?
K8s Ingress (often nginx-ingress) replaces manual config—concepts transfer.
One container for Nginx + app?
Possible with supervisord—anti-pattern; prefer separate containers or host Nginx.
SELinux and volumes?
May need :Z on volume flags on RHEL—host-specific.
Docker Nginx vs host Nginx performance?
Host Nginx slightly less network hop for localhost proxy—both fine at small scale.
Static files in container?
Serve from Nginx container volume or CDN; API in second container.
Link to hello_code Linux Docker?
Linux chapter 14 covers docker run, docker compose basics.