
前言
本文介绍如何使用 Docker Compose 从零部署 Nginx 1.30.6,包括镜像拉取、目录创建、默认配置导出、Docker Compose 配置、网络配置、启动验证、容器间通信以及日志和配置检查。
1. 部署环境
本文使用:
text
Nginx:1.30.6
部署方式:Docker Compose
部署目录:/opt/docker-soft/nginx
建议固定使用具体版本:
text
nginx:1.30.6
而不要直接使用:
text
nginx:latest
这样可以避免以后重新拉取镜像时 Nginx 被自动升级到其他版本。
2. 拉取 Nginx 镜像
执行:
bash
docker pull nginx:1.30.6
查看版本:
bash
docker run --rm nginx:1.30.6 nginx -v
正常情况下应输出:
text
nginx version: nginx/1.30.6
3. 创建部署目录
进入统一的软件部署目录:
bash
cd /opt/docker-soft
创建 Nginx 目录:
bash
mkdir -p /opt/docker-soft/nginx/config/conf.d
mkdir -p /opt/docker-soft/nginx/html
mkdir -p /opt/docker-soft/nginx/cert
最终目录结构建议如下:
text
/opt/docker-soft/nginx/
├── docker-compose.yml
├── config/
│ ├── nginx.conf
│ └── conf.d/
│ └── default.conf
├── html/
│ ├── index.html
│ └── 50x.html
└── cert/
各目录用途:
text
config/nginx.conf Nginx 主配置文件
config/conf.d/ 网站、反向代理等 server 配置
html/ 静态网页目录
cert/ HTTPS 证书目录
docker-compose.yml Docker Compose 配置文件
4. 导出 Nginx 官方默认配置
为了避免直接使用旧版本 Nginx 配置,建议从 nginx:1.30.6 官方镜像中导出默认配置。
4.1 导出 nginx.conf
执行:
bash
docker run --rm \
--entrypoint=cat \
nginx:1.30.6 \
/etc/nginx/nginx.conf \
> /opt/docker-soft/nginx/config/nginx.conf
4.2 导出 default.conf
因为后面会把整个:
text
./config/conf.d
挂载到:
text
/etc/nginx/conf.d
所以容器原有的 default.conf 会被宿主机目录覆盖。
建议提前导出:
bash
docker run --rm \
--entrypoint=cat \
nginx:1.30.6 \
/etc/nginx/conf.d/default.conf \
> /opt/docker-soft/nginx/config/conf.d/default.conf
后续如果配置反向代理,也可以继续在该目录创建:
text
api.conf
web.conf
jenkins.conf
gitlab.conf
等配置文件。
5. 导出默认静态页面
如果准备将:
text
./html
挂载到:
text
/usr/share/nginx/html
建议把官方默认页面也导出来,否则空目录会覆盖镜像中原有的网页。
导出首页:
bash
docker run --rm \
--entrypoint=cat \
nginx:1.30.6 \
/usr/share/nginx/html/index.html \
> /opt/docker-soft/nginx/html/index.html
导出错误页面:
bash
docker run --rm \
--entrypoint=cat \
nginx:1.30.6 \
/usr/share/nginx/html/50x.html \
> /opt/docker-soft/nginx/html/50x.html
如果 Nginx 只用于反向代理,不需要提供静态网页,则可以不挂载 html 目录。
6. 证书目录
HTTPS 证书统一放在:
text
/opt/docker-soft/nginx/cert/
例如:
text
cert/
├── example.com.pem
└── example.com.key
Nginx 配置中可以使用:
nginx
ssl_certificate /etc/nginx/cert/example.com.pem;
ssl_certificate_key /etc/nginx/cert/example.com.key;
如果暂时不使用 HTTPS,cert 目录可以保持为空。
7. 检查宿主机端口
部署前建议先检查准备使用的端口是否已经被占用。
例如:
bash
ss -lntup 'sport = :9081'
ss -lntup 'sport = :9443'
ss -lntup 'sport = :9080'
没有任何输出说明端口当前没有程序监听。
本文示例使用:
text
宿主机 9081 → Nginx 容器 80
宿主机 9443 → Nginx 容器 443
宿主机 9080 → Nginx 容器 9080
需要注意:
text
9080:9080
只有在 Nginx 配置中存在:
nginx
listen 9080;
时才有意义。
如果没有使用 9080,可以直接删除该端口映射。
8. Docker 网络配置
Docker 网络有两种常见使用方式。
方案一:创建独立网络
创建:
bash
docker network create nginx-network
确认:
bash
docker network ls
Docker Compose 中使用:
yaml
networks:
nginx-network:
external: true
方案二:加入已有 Docker 网络
先查看网络:
bash
docker network ls
例如已经存在:
text
1panel-network
则无需再次创建。
查看网络详细信息:
bash
docker network inspect 1panel-network
Docker Compose 中直接使用:
yaml
networks:
1panel-network:
external: true
external: true 表示:
text
该网络已经存在
Compose 只负责加入
不会自动创建这个网络
9. 创建 docker-compose.yml
进入目录:
bash
cd /opt/docker-soft/nginx
创建:
bash
vim docker-compose.yml
参考配置如下:
yaml
services: # 定义 Compose 服务
nginx: # Nginx 服务名称
image: nginx:1.30.6 # 使用官方 Nginx 1.30.6 镜像
container_name: nginx # 容器名称固定为 nginx
restart: unless-stopped # 非手动停止情况下自动重启
ports: # 宿主机和容器端口映射
- "9081:80" # 宿主机 9081 → 容器 HTTP 80
- "9443:443" # 宿主机 9443 → 容器 HTTPS 443
- "9080:9080" # 宿主机 9080 → 容器 9080,可按实际需要删除
volumes: # 挂载配置文件及业务目录
- ./config/nginx.conf:/etc/nginx/nginx.conf:ro # Nginx 主配置文件
- ./config/conf.d:/etc/nginx/conf.d:ro # server、反向代理等配置目录
- ./html:/usr/share/nginx/html:ro # 静态网页目录
- ./cert:/etc/nginx/cert:ro # HTTPS 证书目录
networks: # Docker 网络
- 1panel-network # 加入已有 1panel-network 网络
networks: # 网络定义
1panel-network: # Docker 网络名称
external: true # 使用已有网络,不由 Compose 创建
如果使用自己创建的:
text
nginx-network
则改为:
yaml
services:
nginx:
image: nginx:1.30.6
container_name: nginx
restart: unless-stopped
ports:
- "9081:80"
- "9443:443"
volumes:
- ./config/nginx.conf:/etc/nginx/nginx.conf:ro
- ./config/conf.d:/etc/nginx/conf.d:ro
- ./html:/usr/share/nginx/html:ro
- ./cert:/etc/nginx/cert:ro
networks:
- nginx-network
networks:
nginx-network:
external: true
10. 检查 Docker Compose 配置
正式启动前执行:
bash
docker compose config
如果没有报错,说明 Compose 文件语法基本正常。
11. 启动 Nginx
执行:
bash
docker compose up -d
查看容器:
bash
docker ps
正常情况下可以看到类似:
text
nginx:1.30.6
以及:
text
0.0.0.0:9081->80/tcp
0.0.0.0:9443->443/tcp
12. 查看 Nginx 实际运行版本
执行:
bash
docker exec nginx nginx -v
正常:
text
nginx version: nginx/1.30.6
13. 检查 Nginx 配置
启动后建议立即执行:
bash
docker exec nginx nginx -t
正常情况下:
text
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
如果出现:
text
[emerg]
说明存在严重配置错误,需要先修复。
如果只是:
text
[warn]
通常服务仍可启动,但建议根据提示检查对应配置。
14. 查看 Nginx 日志
官方 Nginx Docker 镜像默认把日志输出到 Docker 标准输出。
查看最近日志:
bash
docker logs nginx
查看最后 100 行:
bash
docker logs nginx --tail 100
实时查看:
bash
docker logs -f nginx
也可以:
bash
docker compose logs -f nginx
通常不需要额外挂载:
text
/var/log/nginx
15. 本机访问测试
在服务器上执行:
bash
curl http://127.0.0.1:9081
如果使用默认页面,应看到类似:
html
<title>Welcome to nginx!</title>
也可以:
bash
curl -I http://127.0.0.1:9081
正常一般返回:
text
HTTP/1.1 200 OK
Server: nginx/1.30.6
16. 外网访问
假设服务器公网 IP 为:
text
133.141.30.45
本文 HTTP 映射:
text
9081:80
则访问:
text
http://133.141.30.45:9081
如果无法访问,需要检查:
bash
ss -lntp 'sport = :9081'
以及系统防火墙、云服务器安全组等是否允许 TCP 9081。
17. 检查 Nginx 是否成功加入 Docker 网络
执行:
bash
docker network inspect 1panel-network
在:
text
Containers
中应该可以看到:
text
nginx
例如:
text
Name: nginx
IPv4Address: 172.18.0.x/16
说明 Nginx 已成功加入该网络。
18. 同一 Docker 网络中的容器如何通信
同一 Docker 自定义网络中的容器,可以直接通过:
text
容器名:容器内部端口
通信。
例如网络中存在:
text
jenkins
mysql84
php74
在 Nginx 容器中测试 DNS:
bash
docker exec nginx getent hosts jenkins
例如返回:
text
172.18.0.2 jenkins
测试:
bash
docker exec nginx getent hosts mysql84
例如:
text
172.18.0.7 mysql84
说明 Docker DNS 工作正常。
需要注意:
bash
getent hosts mysql84
如果直接在宿主机运行,通常不会解析。
应该在同一 Docker 网络中的容器里执行:
bash
docker exec nginx getent hosts mysql84
19. Nginx 反向代理其他容器
假设 Jenkins:
text
容器名:jenkins
容器内部端口:8080
Nginx 可以直接:
nginx
server {
listen 80;
server_name jenkins.example.com;
location / {
proxy_pass http://jenkins:8080;
}
}
不建议写固定 Docker IP:
nginx
proxy_pass http://172.18.0.2:8080;
更推荐:
nginx
proxy_pass http://jenkins:8080;
因为容器重新创建后 IP 可能变化,而容器名可以由 Docker DNS 自动解析。
20. 修改 Nginx 配置后的操作
修改:
text
config/nginx.conf
或者:
text
config/conf.d/*.conf
之后,先检查配置:
bash
docker exec nginx nginx -t
确认正常后重新加载:
bash
docker exec nginx nginx -s reload
通常无需重启整个容器。
21. 常用维护命令
查看容器:
bash
docker ps
查看 Nginx 版本:
bash
docker exec nginx nginx -v
检查配置:
bash
docker exec nginx nginx -t
重新加载配置:
bash
docker exec nginx nginx -s reload
查看日志:
bash
docker logs -f nginx
查看 Compose 日志:
bash
docker compose logs -f nginx
停止:
bash
docker compose down
启动:
bash
docker compose up -d
重启:
bash
docker compose restart nginx
查看网络:
bash
docker network ls
查看指定网络:
bash
docker network inspect 1panel-network
查看 Nginx 网络:
bash
docker inspect nginx
22. 最终部署结构
完成后建议保持:
text
/opt/docker-soft/nginx/
├── docker-compose.yml
│
├── config/
│ ├── nginx.conf
│ └── conf.d/
│ ├── default.conf
│ └── 其他业务配置.conf
│
├── html/
│ ├── index.html
│ └── 50x.html
│
└── cert/
├── xxx.pem
└── xxx.key
23. 完整部署流程速查
如果只需要快速部署,可以按照以下顺序执行:
bash
# 1. 拉取镜像
docker pull nginx:1.30.6
# 2. 创建目录
mkdir -p /opt/docker-soft/nginx/config/conf.d
mkdir -p /opt/docker-soft/nginx/html
mkdir -p /opt/docker-soft/nginx/cert
# 3. 导出 nginx.conf
docker run --rm \
--entrypoint=cat \
nginx:1.30.6 \
/etc/nginx/nginx.conf \
> /opt/docker-soft/nginx/config/nginx.conf
# 4. 导出 default.conf
docker run --rm \
--entrypoint=cat \
nginx:1.30.6 \
/etc/nginx/conf.d/default.conf \
> /opt/docker-soft/nginx/config/conf.d/default.conf
# 5. 导出默认页面
docker run --rm \
--entrypoint=cat \
nginx:1.30.6 \
/usr/share/nginx/html/index.html \
> /opt/docker-soft/nginx/html/index.html
docker run --rm \
--entrypoint=cat \
nginx:1.30.6 \
/usr/share/nginx/html/50x.html \
> /opt/docker-soft/nginx/html/50x.html
# 6. 如需新建 Docker 网络
docker network create nginx-network
# 7. 检查 Compose
cd /opt/docker-soft/nginx
docker compose config
# 8. 启动
docker compose up -d
# 9. 查看状态
docker ps
# 10. 检查 Nginx
docker exec nginx nginx -v
docker exec nginx nginx -t
# 11. 查看日志
docker logs --tail 100 nginx
# 12. 本机访问测试
curl http://127.0.0.1:9081
如果以上步骤均正常,即表示 Nginx 1.30.6 Docker Compose 部署完成。