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

Pattern A: Host Nginx → Container Port

App container maps port to localhost:

bash
docker run -d --name api -p 127.0.0.1:8080:8080 my-spring-api:latest

Host Nginx:

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:

bash
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:

nginx
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:

bash
docker run -d --name web -p 8080:80 nginx:alpine
curl http://127.0.0.1:8080/

Mount custom config:

bash
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:alpine

TLS with Docker

Common approaches:

ApproachNotes
Certbot on host NginxProxy to containers—simplest for one VPS
Certbot in containerNeeds volume mounts for certs and webroot
External CDN TLSTerminate at Cloudflare; origin HTTP

This track’s Certbot chapter assumes host-installed Nginx—recommended for learners.

Reload Config in Container

bash
docker exec web nginx -t
docker exec web nginx -s reload

Or recreate container when mounting configs from CI.

Logs

bash
docker logs web
docker exec web tail -f /var/log/nginx/access.log

Host Nginx logs stay in /var/log/nginx/ as usual.

Pitfalls

IssueCause
502 to containerWrong service name / not on same network
Connection refusedContainer not listening on expected port
Config not updatingVolume 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.

Linux chapter 14 covers docker run, docker compose basics.