Docker-compose/Nginx/Gunicorn - Nginx 不工作

Docker-compose/Nginx/Gunicorn - Nginx not working

我是 Docker 的新手,我正在尝试将我的 Flask-App 容器化。 app + gunicorn 的容器工作正常,我可以访问该站点。但是当我使用 docker-compose 来包含 Nginx 容器时,我无法再连接到该站点。

我的文件:

Docker文件(烧瓶):

FROM python:3.7-stretch
WORKDIR /app
ADD . /app
RUN pip install -r requirements.txt
EXPOSE 8080
CMD ["gunicorn", "-b", "127.0.0.1:8080", "app:app"]

Docker文件 (Nginx)

FROM nginx
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d/

nginx.conf

server {
    listen  8080;
    server_name localhost;

    location / {
        proxy_pass http://127.0.0.1:8080/;
    }

    location /static {
        alias /var/www-data;
    }
}

和docker-compose.yml

version: "3.7"

services:

  flask:
    build: ./flask
    container_name: flask_cookbook
    restart: always
    environment:
      - APP_NAME=Cookbook
    expose:
      - 8080

  nginx:
    build: ./nginx
    container_name: nginx_cookbook
    restart: always
    ports:
      - "80:8080"

当我 运行 使用 docker-compose up --build 的容器时,一切似乎都工作正常:

Starting nginx_cookbook ... done
Starting flask_cookbook ... done
Attaching to nginx_cookbook, flask_cookbook
flask_cookbook | [2019-07-18 15:19:37 +0000] [1] [INFO] Starting gunicorn 19.9.0
flask_cookbook | [2019-07-18 15:19:37 +0000] [1] [INFO] Listening at: http://127.0.0.1:8080 (1)
flask_cookbook | [2019-07-18 15:19:37 +0000] [1] [INFO] Using worker: sync
flask_cookbook | [2019-07-18 15:19:37 +0000] [8] [INFO] Booting worker with pid: 8

但是当我转到127.0.0.1:8080时,没有任何连接。

我真的找不到错误,我可能在某个地方弄错了...


附加信息: 我在 Windows 10

我的目录如下所示

Main Dir
├── docker-compose.yml
├── flask
│   ├── templates
│   ├── static
│   ├── app.py
│   ├── requirements.txt
│   └── Dockerfile
└── nginx
    ├── nginx.conf
    └── Dockerfile

感谢@ShawnC。 ,我解决了这个问题。引用他:

You have a few problems. A) you are proxy passing in nginx to localhost on the same port as nginx is listening to this is not going to work. You need to make requests to the flask container.

所以我将 nginx.conf 文件中的 listen-port 更改为 80(从 8080)并将 docker-compose.yml 的 nginx 部分中的端口更改为 80:80 (来自 80:8080)

B) from your machine making a request to 127.0.0.1:8080 will not work as no docker container is listening. Your config says nginx should use port 80 which would map to 8080 in the container. So you should just be making requests to 127.0.0.1:80

因此,我将 nginx.conf 中的代理传递更改为 flask_cookbook:8080。所以 container-name,以及 flask_app 的暴露端口。这样nginx容器就可以向flask容器发出请求了。

我还必须将 flask-Dockerfile 中的 gunicorn 绑定更改为 0.0.0.0:8080,这样我就可以使用 Localhost/127.0.0.1 连接到 nginx 容器。

现在可以使用了,我可以继续 localhost:80 连接到我的网站。