【问题标题】:Python app deployment using nginx and dockerize flask app使用 nginx 和 dockerize flask 应用程序部署 Python 应用程序
【发布时间】:2021-01-01 22:07:07
【问题描述】:

我想在我的 VPS 上部署烧瓶应用程序。我想出了如何在没有 docker 的情况下轻松完成,但现在我将应用程序 docker 化并使用 docker-composer.yml 运行它。

services:                                                                      │
  myapp:                                                           │
    build: ./myapp                                          │
    container_name: myapp                                         │
    restart: always                                                            │
    environment:                                                               │
      - APP_NAME = myapp                                          │
    expose:                                                                    │
      - 8081  

所以我改变了我的 nginx 配置文件 来自

   location / {
         include    uwsgi_params;
         uwsgi_pass unix:/home/username/path/to/socket/mysocker.sock;
     }

 location / {
     include    uwsgi_params;
     uwsgi_pass myapp:8081;
 }

应用程序正在使用 docker composer 运行,但是当我使用 nginx -t 在 nginx 中测试正确的设置时,我收到了这条消息

nginx:在上游“myapp”中找不到[emerg]主机 /etc/nginx/sites-enabled/myapp:22 │nginx:配置文件/etc/nginx/nginx.conf测试失败

我很确定这意味着 nginx 无法找到在 docker 中运行的 myapp 并且无法与之通信,但我暴露了端口并且据我所知容器名称是主机名,因此它应该可以工作。 有谁知道如何让他们交流?我没有在互联网上找到任何不会对我不想要的 nginx 进行 docker 化的教程。

感谢任何帮助

编辑: 这是我的 Dockerfile

FROM python:3.8.5-buster                                                     
                                                                             
WORKDIR /app                                                                 
                                                                             
ADD . /app                                                                   
                                                                             
RUN apt-get update -y && apt-get -y install curl && apt-get install libsasl2$
RUN pip3 install mysqlclient                                                 
RUN pip3 install blinker                                                     
RUN pip3 install pyOpenSSL                                                   
RUN pip3 install uwsgi                                                       
RUN pip3 install -r requirements.txt                                         
CMD ["uwsgi", "myproject.ini", "--enable-threads"]  

UWSGI

[uwsgi]                                                                                                                                                             
wsgi-file=wsgi.py                                                                                                                                                   
callable=app                                                                                                                                                        
socket=8081                                                                                                                                                         
module = wsgi:app                                                                                                                                                   
master = true                                                                                                                                                       
processes = 1                                                                                                                                                       
chmod-socket = 666                                                                                                                                                  
vacuum = true                                                                                                                                                       
harakiri = 120                                                                                                                                                      
die-on-term = true   

解决方案

  1. 将socket=8081改为socket=0.0.0.0:8081
  2. 改变nginx监听localhost:8081
  3. 将端口 8081:8081 添加到 docker-compose

【问题讨论】:

    标签: docker nginx flask


    【解决方案1】:

    Docker 有内部 DNS 服务器在容器内的 127.0.0.11 上工作。如果您的 nginx 不在容器中,则无法使用它来解析 myapp 名称。不过,您可以选择其中之一:

    1. 让您的容器监听主机端口:
    services:
      myapp:
        ports:  
          # host:container
          - 8081:8081
    

    然后在您的 nginx 配置中反映此更改:

     location / {
         include    uwsgi_params;
         uwsgi_pass localhost:8081;
     }
    
    1. 返回使用 unix 套接字,但这次将套接字放入主机目录。为此,首先将主机的/tmp 挂载到容器中:
    services:
      myapp:
        volumes:  
          # host:container
          - /tmp:/tmp
    

    然后配置您的应用程序以将套接字放入/tmp。套接字将出现在主机的/tmp 中,您可以配置 nginx 与之通信。如果你挂载的不是整个/tmp,而是其中的一个目录,你可能会稍微改进一下。 /tmp/myapp 例如。这样您就可以消除容器将主机文件弄乱的可能性。

    【讨论】:

    • 我使用了第一种方法,但现在网站给了我 502 Bad Gateway
    • 并且烧瓶应用程序没有显示任何请求都发送到它
    • 你能提供docker ps输出的结果吗?
    • 容器 ID 图像命令创建状态端口名称│ ebd43348ade2 ordelogy_monitoringhorndev "uwsgi myproject.ini..." 5 分钟前 5 分钟前 0.0.0.0:8081->8081/tcp monitoringhorndev
    • 不需要更改的是 http-socket 我只是将套接字编辑为 socket=0.0.0.0:8081 现在应用程序正在运行非常感谢你救了我的命
    猜你喜欢
    • 2014-06-04
    • 1970-01-01
    • 1970-01-01
    • 2021-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多