Docker Install Web Servers
This chapter covers how to deploy Nginx, Caddy, and Apache web servers using Docker.
Nginx
Quick Start
bash
docker run -d --name nginx -p 80:80 nginx:latestCustom Content
bash
docker run -d --name nginx -p 80:80 -v $(pwd)/html:/usr/share/nginx/html:ro nginx:latestCustom Configuration
bash
docker run --rm nginx cat /etc/nginx/nginx.conf > nginx.conf
docker run -d --name nginx -p 80:80 \
-v $(pwd)/nginx.conf:/etc/nginx/nginx.conf:ro \
-v $(pwd)/conf.d:/etc/nginx/conf.d:ro \
-v $(pwd)/html:/usr/share/nginx/html:ro \
nginx:latestReverse Proxy Example
nginx
server {
listen 80;
location / {
proxy_pass http://app:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}Reload Configuration
bash
docker exec nginx nginx -t # Test config
docker exec nginx nginx -s reload # Reload without downtimeCaddy
Caddy is a modern web server with automatic HTTPS support.
Quick Start
bash
docker run -d --name caddy -p 80:80 -p 443:443 \
-v $(pwd)/html:/srv \
-v caddy-data:/data \
caddy:latestUsing Caddyfile
:80 {
root * /srv
file_server
}bash
docker run -d --name caddy -p 80:80 -p 443:443 \
-v $(pwd)/Caddyfile:/etc/caddy/Caddyfile \
-v $(pwd)/html:/srv \
-v caddy-data:/data \
caddy:latestAutomatic HTTPS
Just use a domain name and Caddy automatically obtains SSL certificates via Let's Encrypt:
example.com {
root * /srv
file_server
}Reload
bash
docker exec caddy caddy reload --config /etc/caddy/CaddyfileApache (httpd)
Quick Start
bash
docker run -d --name apache -p 80:80 httpd:latestCustom Content
bash
docker run -d --name apache -p 80:80 -v $(pwd)/html:/usr/local/apache2/htdocs/ httpd:latestCustom Configuration
bash
docker run --rm httpd cat /usr/local/apache2/conf/httpd.conf > httpd.conf
docker run -d --name apache -p 80:80 \
-v $(pwd)/httpd.conf:/usr/local/apache2/conf/httpd.conf \
-v $(pwd)/html:/usr/local/apache2/htdocs/ \
httpd:latestWeb Server Comparison
| Feature | Nginx | Caddy | Apache |
|---|---|---|---|
| Performance | High | High | Medium |
| Config complexity | Medium | Low | High |
| Auto HTTPS | Requires setup | Built-in | Requires setup |
| Memory usage | Low | Low | Medium |
| Module ecosystem | Rich | Moderate | Very rich |
| Best for | Reverse proxy, static files | Quick deploy, auto HTTPS | Traditional web apps |
Chapter Summary
Docker simplifies web server deployment. Nginx excels at reverse proxying, Caddy offers automatic HTTPS, and Apache provides the richest module ecosystem.