Docker Compose Nginx Reverse Proxy with Let's Encrypt SSL (Automated Certbot Setup)
Running web applications in Docker containers requires a secure gateway to handle TLS encryption, domain routing, and HTTP/2 acceleration.
Combining Nginx as a reverse proxy with Certbot in Docker Compose gives you automated certificate generation and renewal without installing software directly on the host operating system.
Here is a tested, production-grade configuration.
Directory Structure
Set up your project directory:
├── docker-compose.yml
├── nginx/
│ ├── Dockerfile
│ └── default.conf
└── certbot/
├── conf/
└── www/Create the required directories:
mkdir -p nginx certbot/conf certbot/www1. Docker Compose Configuration
Create docker-compose.yml:
services:
nginx:
image: nginx:stable-alpine
container_name: nginx-proxy
restart: always
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
- ./certbot/conf:/etc/letsencrypt:ro
- ./certbot/www:/var/www/certbot:ro
depends_on:
- app
certbot:
image: certbot/certbot:latest
container_name: certbot-ssl
restart: unless-stopped
volumes:
- ./certbot/conf:/etc/letsencrypt
- ./certbot/www:/var/www/certbot
entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'"
app:
image: node:20-alpine
container_name: backend-app
restart: always
command: node server.js
expose:
- "3000"2. Nginx Configuration (nginx/default.conf)
Configure Nginx to handle the ACME challenge on HTTP (Port 80) and route secure traffic on Port 443:
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
# Certbot ACME challenge verification path
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
# Redirect all other HTTP traffic to HTTPS
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name example.com www.example.com;
# SSL Certificates (managed by Certbot)
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
# Recommended Security Headers
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers off;
ssl_ciphers "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384";
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
location / {
proxy_pass http://app:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_cache_bypass $http_upgrade;
}
}3. First-Time Certificate Issuance
Before the certificate files exist, Nginx cannot start with ssl_certificate directives pointing to missing files. Use a dummy certificate or issue the certificate in webroot mode:
docker compose run --rm --entrypoint "\
certbot certonly --webroot -w /var/www/certbot \
--email your-email@example.com \
-d example.com -d www.example.com \
--rsa-key-size 4096 \
--agree-tos \
--force-renewal" certbotOnce certificates are issued:
docker compose exec nginx nginx -s reload4. Automated Certificate Renewal Verification
Certbot checks for renewal every 12 hours via the entrypoint loop defined in docker-compose.yml.
To test that renewal succeeds without waiting 90 days:
docker compose run --rm --entrypoint "certbot renew --dry-run" certbotIf you see Congratulations, all simulated renewals succeeded, your automated SSL pipeline is production ready.
Connect with KoderSolution 🚀
Master modern software engineering, scalable architectures, and advanced backend systems.
Read more architectural deep-dives, systems tutorials, and engineering guides on kodersolution.com.
Subscribe to @kodersolution for concise, animated system design breakdowns and technical shorts.
Follow our official page for daily development tips, architecture discussions, and community updates.
Henrik Lindqvist
Runs small, hardened Docker containers and Alpine builds. Likes automated backups, Terraform scripts, and keeping server configs reproducible.