bash
[root@iZuf6hb2khjn9b2lx76d4tZ ~]# docker logs nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2025/12/20 07:45:07 [emerg] 1#1: host not found in upstream "fresh" in /etc/nginx/nginx.conf:29
nginx: [emerg] host not found in upstream "fresh" in /etc/nginx/nginx.conf:29
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: IPv6 listen already enabled
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2025/12/20 07:45:54 [emerg] 1#1: host not found in upstream "fresh" in /etc/nginx/nginx.conf:29
nginx: [emerg] host not found in upstream "fresh" in /etc/nginx/nginx.conf:29
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: IPv6 listen already enabled
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2025/12/20 07:47:19 [emerg] 1#1: host not found in upstream "fresh" in /etc/nginx/nginx.conf:29
nginx: [emerg] host not found in upstream "fresh" in /etc/nginx/nginx.conf:29
现在找到了 Docker Nginx 容器启动失败的精准原因 :Nginx 配置文件 /etc/nginx/nginx.conf 的第 29 行中,把 Docker 网络名 fresh 当成了上游服务(upstream)的主机名,但 fresh 是网络名称而非容器名称,导致 Nginx 解析不到这个"主机",触发 host not found in upstream 错误,容器启动失败。
一、错误原因详解
upstream "fresh" 的含义是 Nginx 要反向代理到名为 fresh 的上游服务器,但:
fresh是你创建的 Docker 桥接网络 名称,不是容器名称;- Nginx 启动时会尝试解析
fresh这个主机名,但在fresh网络内没有名为fresh的容器,解析失败,因此报host not found,进程直接退出(退出码 1)。
二、解决方案:修正 Nginx 配置文件
核心是把 nginx.conf 第 29 行的 fresh 替换成实际要代理的容器名称/IP (比如你要代理的 material 容器,或 mysql 容器)。
步骤 1:编辑挂载的 Nginx 配置文件
bash
# 编辑你挂载的配置文件(/root/nginx/nginx.conf)
vim /root/nginx/nginx.conf
步骤 2:定位并修正第 29 行的 upstream 配置
找到第 29 行类似以下的错误配置:
nginx
# 错误示例(把网络名 fresh 当成了容器名)
upstream fresh {
server fresh:8080; # 错误:fresh 是网络名,不是容器名
}
将其修正为实际的容器名称/IP (比如你要代理 material 容器,它的端口是 8080):
nginx
# 正确示例(替换为容器名 material)
upstream backend { # 建议重命名 upstream 为有意义的名称,比如 backend
server material:8080; # material 是容器名,fresh 网络内可直接通过容器名访问
}
补充:在 Docker 同一网络(fresh)内,容器可以通过容器名称 直接解析 IP,因此
material:8080能正确访问到该容器的 8080 端口。
步骤 3:保存配置文件并重启 Nginx 容器
bash
# 1. 删除启动失败的 nginx 容器
docker rm -f nginx
# 2. 重新启动 Nginx 容器(用之前修正的命令)
docker run -d \
--name nginx \
-p 18080:80 \
-v /root/nginx/html:/usr/share/nginx/html \
-v /root/nginx/nginx.conf:/etc/nginx/nginx.conf \
-v /root/nginx/conf.d:/etc/nginx/conf.d \
--network fresh \
nginx
# 3. 查看容器状态(确认 Up 状态)
docker ps
三、验证配置是否生效
bash
# 1. 查看 Nginx 容器日志(无报错则成功)
docker logs nginx
# 2. 测试访问 Nginx 代理(访问宿主机 18080 端口)
curl http://localhost:18080
# 正常会返回 material 容器的响应内容(而非 Nginx 默认页面)
四、关键补充说明
-
Docker 网络内的域名解析规则:
- 同一 Docker 网络内,容器之间可以通过 容器名称 或 容器 IP 访问,不能通过网络名称访问;
- 比如
fresh网络内,nginx容器可以直接访问material:8080、mysql:3306,但不能访问fresh:8080。
-
如果不知道要代理的容器名/IP :
执行以下命令查看
fresh网络内的容器信息:bashdocker network inspect fresh | grep -E "Name|IPv4Address"输出示例(可获取容器名和 IP):
"Name": "material", "IPv4Address": "172.18.0.3/16", "Name": "mysql", "IPv4Address": "172.18.0.2/16",
总结
- 核心错误:Nginx 配置文件中将 Docker 网络名
fresh当成了上游容器名,导致域名解析失败。 - 解决关键:修改
nginx.conf第 29 行的upstream配置,将fresh替换为实际的容器名(如material)。 - 重要规则:Docker 同一网络内,容器通过容器名称解析,而非网络名称。
修正配置后,Nginx 容器就能正常启动,且能正确代理到目标容器。如果仍有报错,可把 nginx.conf 第 29 行附近的配置发给我,我帮你精准修正。