Skip to content

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

Custom Content

bash
docker run -d --name nginx -p 80:80 -v $(pwd)/html:/usr/share/nginx/html:ro nginx:latest

Custom 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:latest

Reverse 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 downtime

Caddy

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

Using 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:latest

Automatic 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/Caddyfile

Apache (httpd)

Quick Start

bash
docker run -d --name apache -p 80:80 httpd:latest

Custom Content

bash
docker run -d --name apache -p 80:80 -v $(pwd)/html:/usr/local/apache2/htdocs/ httpd:latest

Custom 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:latest

Web Server Comparison

FeatureNginxCaddyApache
PerformanceHighHighMedium
Config complexityMediumLowHigh
Auto HTTPSRequires setupBuilt-inRequires setup
Memory usageLowLowMedium
Module ecosystemRichModerateVery rich
Best forReverse proxy, static filesQuick deploy, auto HTTPSTraditional 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.

Further Reading

Content is for learning and research only.