Skip to content

Docker 安装 Web 服务

本章将介绍如何使用 Docker 部署 Nginx、Caddy 和 Apache 三种常见的 Web 服务器。

Nginx

快速启动

bash
docker run -d \
  --name nginx \
  -p 80:80 \
  nginx:latest

访问 http://localhost 即可看到 Nginx 欢迎页面。

挂载自定义网页

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

自定义配置

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

反向代理配置示例

创建 conf.d/default.conf

nginx
server {
    listen 80;
    server_name localhost;

    location / {
        proxy_pass http://app:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

配置 SSL/HTTPS

bash
docker run -d \
  --name nginx-ssl \
  -p 80:80 \
  -p 443:443 \
  -v $(pwd)/ssl:/etc/nginx/ssl:ro \
  -v $(pwd)/conf.d:/etc/nginx/conf.d:ro \
  nginx:latest

SSL 配置文件示例:

nginx
server {
    listen 443 ssl;
    server_name example.com;

    ssl_certificate /etc/nginx/ssl/cert.pem;
    ssl_certificate_key /etc/nginx/ssl/key.pem;

    location / {
        root /usr/share/nginx/html;
        index index.html;
    }
}

重新加载配置

bash
# 测试配置文件语法
docker exec nginx nginx -t

# 重新加载配置(不停机)
docker exec nginx nginx -s reload

Caddy

Caddy 是一个现代化的 Web 服务器,自动支持 HTTPS。

快速启动

bash
docker run -d \
  --name caddy \
  -p 80:80 \
  -p 443:443 \
  -v $(pwd)/html:/srv \
  -v caddy-data:/data \
  -v caddy-config:/config \
  caddy:latest

使用 Caddyfile 配置

创建 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

反向代理

example.com {
    reverse_proxy app:3000
}

api.example.com {
    reverse_proxy api:8080
}

Caddy 自动 HTTPS

Caddy 的一大特色是自动申请和续期 SSL 证书:

example.com {
    root * /srv
    file_server
}

只需使用域名,Caddy 会自动通过 Let's Encrypt 获取证书。

重新加载配置

bash
docker exec caddy caddy reload --config /etc/caddy/Caddyfile

Apache (httpd)

快速启动

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

挂载自定义网页

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

自定义配置

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

启用模块

编辑 httpd.conf,取消注释需要的模块:

apache
# 启用 rewrite 模块
LoadModule rewrite_module modules/mod_rewrite.so

# 启用 proxy 模块
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so

反向代理配置

apache
<VirtualHost *:80>
    ServerName example.com
    ProxyPreserveHost On
    ProxyPass / http://app:3000/
    ProxyPassReverse / http://app:3000/
</VirtualHost>

Docker Compose 部署示例

Nginx + Node.js 应用

yaml
services:
  nginx:
    image: nginx:latest
    ports:
      - "80:80"
    volumes:
      - ./nginx.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - app

  app:
    image: node:20-alpine
    working_dir: /app
    volumes:
      - ./app:/app
    command: node server.js
    expose:
      - "3000"

Caddy + 多服务

yaml
services:
  caddy:
    image: caddy:latest
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile
      - caddy-data:/data
    depends_on:
      - web
      - api

  web:
    image: nginx:alpine
    volumes:
      - ./frontend/dist:/usr/share/nginx/html

  api:
    image: node:20-alpine
    working_dir: /app
    volumes:
      - ./backend:/app
    command: node server.js

volumes:
  caddy-data:

Web 服务器对比

特性NginxCaddyApache
性能
配置复杂度
自动 HTTPS需配置内置需配置
内存占用
模块生态丰富适中非常丰富
适用场景反向代理、静态文件快速部署、自动 HTTPS传统 Web 应用

本章小结

Docker 让 Web 服务器的部署变得非常简单。Nginx 适合高性能反向代理场景,Caddy 适合需要自动 HTTPS 的快速部署,Apache 适合需要丰富模块支持的传统应用。

延伸阅读