[英]host not found in upstream in /etc/nginx/conf.d/nginx.conf when running standalone docker containers
[英]AWS ECR, ECS based deployment: nginx: [emerg] host not found in upstream "app:9000" in /etc/nginx/conf.d/default.conf:2
我正在努力配置 AWS ECS 任务定义以运行简单的基于 PHP-FPM 和 Nginx 的容器。
我的“应用程序”容器运行在app:9000
端口,而 Nginx 运行在80
端口。 Nginx 正在通过fastcgi_pass app-upstream;
将请求转发到应用容器fastcgi_pass app-upstream;
所有这些都在本地完美运行。 相同的配置在 DigitalOcean Docker 实例中完美运行,但在 AWS ECS 中失败。
我在猜测一些任务定义配置问题,但我找不到它。
错误日志:
我从Nginx 容器获取此日志
nginx: [emerg] host not found in upstream "app:9000" in /etc/nginx/conf.d/default.conf:2
以及来自App (PHP-FPM)容器的日志
电子控制系统
我使用 T2 Small 实例创建了一个简单的集群,该集群运行良好。
文件
在这个 Github Repo 中,我添加了 App 的Dockerfile
、Nginx 镜像、 docker-compose.yml
文件和task-defination.json
文件,以供参考,如果docker-compose.yml
文件中有任何错误。
Github repo 中的源代码: https : //github.com/arifulhb/docker-ecr-sample
您的问题与 nginx 配置中提到的错误上游路径有关。
做以下事情来调查,如果特别不需要,也避免在 docker-compose 文件中使用自定义容器名称:-
快速解决方案将是:-
构建 docker-compose 文件的正确和推荐方法:-
这对您有什么帮助? 您可以检查您创建的这个网络,确定您的容器是否正确连接并识别用于连接的名称。
Command : docker network inspect <network_name>
NOTE : Docker treats container names as host names by default for internal connections.
当使用多个容器时,容器名称对于提供内部连接非常重要。
当我看到您的 Docker 撰写文件时,容器名称应与 nginx conf 中使用的名称相匹配。
version: '3'
services:
test_app:
container_name: app # not test_app_1
volumes:
- test-app-data:/var/www/app
test_nginx:
image: "xxxxxx.dkr.ecr.xx-xx-1.amazonaws.com/test-nginx"
build:
context: ./docker/nginx
dockerfile: Dockerfile
container_name: nginx
ports:
- "80:80"
- "443:443"
volumes:
- test-app-data:/var/www/app
- test-nginx-log:/var/log/nginx
external_links:
- app # not test_app_1
depends_on:
- test_app
volumes:
test-app-data:
test-nginx-log:
我也遇到了同样的问题。 我的“博客”容器运行在blog:8000
端口,“nginx”容器运行在80
端口。 “nginx”容器正在将请求转发到“blog”容器。
因为我根本没有在“网络设置”中为“nginx”容器设置“链接”。
因此,我将后端容器“blog”的名称放在前端容器“nginx”中(“nginx”容器的“NETWORK SETTINGS”中的“Links”)。
然后,正确运行“blog”和“nginx”容器就成功了。
因此,在您的情况下,将后端容器“app”的名称放在前端容器“nginx”中(“nginx”容器的“网络设置”中的“链接”)。 它会起作用。 我使用了Adiii 的解决方案。
不要忘记将“ CMD-SHELL, curl -f http://localhost:9000/ || exit 1
”放在“app”容器的“HEALTHCHECK”中的“Command”中。
此外,不要忘记将“app”放在“Container name”中,并在“nginx”容器的“STARTUP DEPENDENCY ORDERING”中的“Condition”中选择“HEALTHY”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.