Docker Compose 部署 Nginx 1.30.6 完整指南

前言

本文介绍如何使用 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 部署完成。

相关推荐
YYYing.8 小时前
【K8s系列 (一) 】Docker与容器(上篇)
docker·云原生·容器·kubernetes
csdn_aspnet9 小时前
使用 Docker 和 RedisInsight 设置 Redis 以进行本地开发
redis·docker·容器·redisinsight
余槐i14 小时前
MySQL 8.0 联合索引走错:为什么加了索引查询反而从 200ms 变成 8s
java·spring boot·mysql·docker·性能优化
csdn_aspnet14 小时前
如何在本地 Docker 容器中运行 Redis,并使用 Redis Insight 和 Redis CLI 进行管理
redis·docker·容器
且从容.14 小时前
Ubuntu 安装 Docker
linux·ubuntu·docker
todoitbo15 小时前
我在 Mac 上用 Docker 部署 Nexent:从拉取镜像到接入模型
macos·docker·大模型·ai智能体·nexent·快速入门与环境准备
Goboy16 小时前
夯爆了,我被一个一句话接入的免费可观测平台策反了,那一刻我决定放弃用了3年的Prometheus
后端·docker·kubernetes
吴声子夜歌17 小时前
Kubernetes权威指南——深入掌握Service(一)
云原生·容器·kubernetes
java_logo1 天前
Docker 部署 Liquibase:轻松搭建数据库变更管理
数据库·docker·容器·数据库管理·liquibase·轩辕镜像·数据库变更
IT利刃出鞘1 天前
Docker Compose--解决修改配置后不生效的问题
运维·docker·容器