docker - 如何使用 docker 内的套接字将 nginx 套接字与 gunicorn 应用程序结合起来?

标签 docker ubuntu nginx flask gunicorn

我使用 flask / tensorflow 开发了一个小项目。它在应用服务器 - gunicorn 下运行。
我还必须将 nginx 包含到项目中以提供静态文件。没有 docker 应用程序运行良好。所有部分(gunicorn、nginx、flask)都按预期合作。现在是时候将这个项目移动到在线服务器了,我需要通过 docker 来完成。
Nginx gunicorn-> flask 应用程序通过 进行通信unix 套接字 .在我的本地主机环境中,我在应用程序根文件夹中使用了套接字 - myapp/app.sock,一切都很好。
现在的问题是我不太明白如何告诉 docker 内的 nginx 使用相同的套接字文件并告诉 gunicorn 听它。我收到以下错误:
上游:http://unix:/var/run/app.sock 连接到上游时失败(没有这样的文件或目录)
尝试使用不同的套接字文件路径,但没有运气 - 同样的错误。
docker 撰写文件:

version: '3'
services:
  nginx:
    image: nginx:latest
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./nginx/remote-app:/etc/nginx/sites-enabled/remote-app
      - /etc/nginx/proxy_params:/etc/nginx/proxy_params
    ports:
      - 8000:8000
    
    build: .
    command: gunicorn --bind unix:/var/run/app.sock wsgi:app --reload --workers 1 --timeout 60
    
    environment:
      - FLASK_APP=prediction_service.py
      - FLASK_DEBUG=1
      - PYTHONUNBUFFERED=True
    restart: on-failure
主 Dockerfile (对于主应用程序,它可以很好地构建应用程序,一切正常):
FROM python:3.8-slim

RUN pip install flask gunicorn flask_wtf boto3 tqdm
RUN pip install numpy==1.18.5
RUN pip install tensorflow==2.2.0 onnxruntime==1.4.0

COPY *.ipynb /temp/
COPY *.hdf5 /temp/
COPY *.onnx /temp/
COPY *.json /temp/
COPY *.py /temp/

WORKDIR /temp
nginx.conf 与默认值 99% 相同,只是增加了上传到 8M 的文件大小
proxy-params 只是用于发出 nginx 代理请求的配置参数的预设
远程应用程序是我的应用程序的配置(简单的一个):
server {
    listen 8000;
    server_name localhost;

   location / {
      include proxy_params;
      proxy_pass htpp://unix:/var/run/app.sock; //**tried /temp/app.sock here same issue**
}
}
因此,如果我打开 localhost(即使没有端口 8000),我也可以获得 nginx 答案。如果我尝试打开 localhost:8000 我得到那个套接字错误(上面粘贴了强文本)。

最佳答案

我会避免为此使用套接字,因为容器/服务之间存在 IP 通信,并且您确实应该为应用服务器提供单独的服务。
就像是:

version: '3'
services:

  nginx:
    image: nginx:latest
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./nginx/remote-app:/etc/nginx/sites-enabled/remote-app
      - /etc/nginx/proxy_params:/etc/nginx/proxy_params
    ports:
      - 80:80
      - 143:143

  app_server:
    build: .
    command: gunicorn --bind '0.0.0.0:5000' wsgi:app --reload --workers 1 --timeout 60
    
    environment:
      - FLASK_APP=prediction_service.py
      - FLASK_DEBUG=1
      - PYTHONUNBUFFERED=True
    restart: on-failure
注意,不是将 gunicorn 绑定(bind)到套接字,而是绑定(bind)到 app_server 的所有 IP 接口(interface)。 5000 端口上的容器。
使用单独的服务 app_server在您当前的 nginx 旁边服务,您可以简单地将这些值视为每个容器中的 DNS 别名。所以在 nginx 配置中:
proxy_pass http://app_server:5000/

So if i open localhost(even without port 8000) i can get nginx answer.


听起来您的意思是在端口 80 上连接到本地主机这可能是在主机上运行的 nginx 服务器。撰写文件中的这一行也建议这样做:/etc/nginx/proxy_params:/etc/nginx/proxy_params .
这是从主机上本地安装的 nginx 加载文件。您可能应该意识到这一点,因为在调试时运行该服务器也会使您感到困惑,并且在某处启动此撰写文件将意味着 /etc/nginx/proxy_params必须存在于主机上。
您可能应该将它存储在项目目录中,就像安装的其他文件一样,并像这样安装它:
  - ./nginx/proxy_params:/etc/nginx/proxy_params

关于docker - 如何使用 docker 内的套接字将 nginx 套接字与 gunicorn 应用程序结合起来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63974433/

相关文章:

docker - 在Docker中sha256是什么意思,图像层目录在哪里?

linux - 如何在 Docker 中使用容器的内部 IP 地址作为环境变量

ubuntu - Sublime Text : Permission denied when running Sublime REPL by non-root user under Ubuntu

python-2.7 - 无法在虚拟环境 Python 2.7 Ubuntu 17.10 中安装软件包

Python检查大量请求

ssl - NGINX HTTPS 服务器 - SSL_ERROR_BAD_CERT_DOMAIN

docker - 如何使用 secret .env var而不将其暴露在存储库中

docker - 尝试运行 docker-compose.yml 文件时收到拉取访问被拒绝错误

python - 在 Ubuntu/Ansible 上运行 playbook 时出现 ModuleNotFoundError

在 07/2016 中启用/禁用的 apache + nginx ssl 密码