Am将两个容器uwsgi和nginx部署到AWS ECS存储库。 我正在使用Fargate来部署和设置容器,但在容器之间的连接和通信中遇到错误。
error:No host not found in upstream "flask_app" in /etc/nginx/conf.d/nginx.conf.
Docker撰写yml文件。
version: "3"
services:
db:
container_name: db
image: postgres
restart: always
environment:
POSTGRES_DB: XXXXX
POSTGRES_USER: XXXX
POSTGRES_PASSWORD: XXXXX
ports:
- "54321:5432"
flask_app:
container_name: flask_app
image: XXXXXXXXXXXXXXX.dkr.ecr.us-east-2.amazonaws.com/YYYYY:flask
build:
context: ./
dockerfile: ./docker/Dockerfile-flask
volumes:
- .:/app
depends_on:
- db
ports:
- 5000:5000
links:
- db
nginx:
container_name: nginx
image: XXXXXXXXXXXXXXX.dkr.ecr.us-east-2.amazonaws.com/YYYYY:backend
env_file:
- ./docker/users.variables.env
build:
context: .
dockerfile: ./docker/Dockerfile-nginx
ports:
- 8080:80
depends_on:
- flask_app
links:
- flask_app
Nginx (nginx.conf):
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
location / {
resolver 169.254.169.253;
include uwsgi_params;
proxy_pass http://flask_app:5000/;
proxy_set_header Host "localhost";
}
}
UWSGI.ini:
[uwsgi]
protocol = http
; This is the name of our Python file
; minus the file extension
module = start
; This is the name of the variable
; in our script that will be called
callable = app
master = true
; Set uWSGI to start up 5 workers
processes = 5
; We use the port 5000 which we will
; then expose on our Dockerfile
socket = 0.0.0.0:5000
vacuum = true
die-on-term = trueS
错误2019/08/23 12:27:13 [emerg] 1#1:在/etc/nginx/conf.d/nginx.conf:8的上游“ flask_app”中找不到主机
答案 0 :(得分:0)
在您的示例中,您依赖于Docker compose中设置的容器名称(flask_app)与其他容器进行通信。这对于在本地环境中进行Docker撰写非常有效,但对于使用Fargate的ECS而言则无效。
使用Fargate进行的部署使用awsvpc网络模式。如果它们属于同一任务,则允许这些容器通过localhost相互通信-请参阅AWS文档:https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-networking.html
如果您尝试配置Nginx反向代理Sidecar,则这些容器将属于同一任务。我意识到下面的示例使用uwsgi协议而不是http,但是它确实演示了通过localhost的通信(除非必须使用http,否则您可以轻松地修改配置以使用uwsgi)。这是使用uwsgi协议作为Fargate部署的nginx和uwsgi的有效配置:
nginx.conf:
server {
listen 80;
server_name localhost 127.0.0.1;
root /usr/share/nginx/html;
location / {
include uwsgi_params;
uwsgi_pass localhost:5000;
}
和 uwsgi.ini 像这样:
[uwsgi]
module = wsgi:app
uid = www-data
gid = www-data
master = true
processes = 3
socket=localhost:5000
vacuum = true
die-on-term = true
(假设您使用示例中的端口5000) 此配置将不能在您使用Docker compose和网桥网络的本地环境中工作-但是,如果您在Linux上,则可以使用主机网络对其进行仿真-可以找到有关设置本地测试环境的说明这里: https://aws.amazon.com/blogs/compute/a-guide-to-locally-testing-containers-with-amazon-ecs-local-endpoints-and-docker-compose/
希望这可以解决问题。