gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 21:07:24 26 4
gpt4 key购买 nike

我对 Docker 相当陌生,我正在尝试将我的 Flask-App 容器化。应用程序 + gunicorn 的容器工作正常,我可以访问该站点。但是当我使用 docker-compose 包含一个 Nginx 容器时,我无法再连接到该站点。

我的文件:

Dockerfile( flask ):

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"]

Dockerfile (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 文件中的监听端口更改为 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。所以容器名称,以及flask_app的暴露端口。这样,nginx 容器就可以向 flask 容器发出请求。

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

现在它可以工作了,我可以在 localhost:80 上连接到我的网站。

关于python - Docker-compose/Nginx/Gunicorn - Nginx 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57098049/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com