首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将容器部署到AWS存储库后,将UWSGI容器与NGINX容器[Docker]连接

将容器部署到AWS存储库后,将UWSGI容器与NGINX容器[Docker]连接
EN

Stack Overflow用户
提问于 2019-08-23 12:56:01
回答 1查看 1.6K关注 0票数 1

正在将两个容器uwsgi和nginx部署到AWS存储库。我正在使用fargate来部署和设置容器,但是在容器之间的连接和通信中出现了错误。

代码语言:javascript
运行
复制
error:No host not found in upstream "flask_app" in /etc/nginx/conf.d/nginx.conf.

Docker编写yml文件。

代码语言:javascript
运行
复制
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:8中上游"flask_app“中找不到主机

EN

回答 1

Stack Overflow用户

发布于 2019-11-06 14:09:08

在您的示例中,您依赖于Docker中设置的容器名称(flask_app)与另一个容器进行通信。这对于Docker在本地环境中很好,但在使用Fargate的ECS中却不是这样。

使用Fargate的部署使用awsvpc网络模式。这允许这些容器通过本地主机相互通信,只要它们属于相同的任务--请参阅AWS文档:https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-networking.html

如果您试图配置nginx反向代理侧服务器,那么这些容器将属于同一任务。我意识到下面的示例使用的是uwsgi原型,而不是http,但它确实演示了通过localhost进行通信(除非您必须使用http,否则可以轻松地修改您的配置以使用uwsgi)。下面是使用uwsgi协议作为Fargate部署的nginx和uwsgi的工作配置:

nginx.conf:

代码语言:javascript
运行
复制
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是这样的:

代码语言:javascript
运行
复制
[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和桥接网络,但是如果您在Linux上可以使用主机网络模拟它,可以在这里找到关于设置本地测试环境的说明:https://aws.amazon.com/blogs/compute/a-guide-to-locally-testing-containers-with-amazon-ecs-local-endpoints-and-docker-compose/

希望这能在某种程度上解决这个问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57626791

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档