简体   繁体   English

使用 nginx 和 dockerize Flask 应用程序部署 Python 应用程序

[英]Python app deployment using nginx and dockerize flask app

I would like to deploy flask app on my VPS.我想在我的 VPS 上部署 Flask 应用程序。 I figured out easy how to do it without docker, but now I dockerized the app and I am running it using docker-composer.yml.我想出了没有 docker 的简单方法,但现在我对应用程序进行了 dockerized,并且我正在使用 docker-composer.yml 运行它。

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

So I changed my nginx conf from所以我改变了我的 nginx conf

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

to

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

The app is running using docker composer but when I test the correct settings in nginx using nginx -t I get this message该应用程序正在使用 docker composer 运行,但是当我使用 nginx -t 在 nginx 中测试正确的设置时,我收到此消息

nginx: [emerg] host not found in upstream "myapp" in /etc/nginx/sites-enabled/myapp:22 │nginx: configuration file /etc/nginx/nginx.conf test failed nginx: [emerg] host not found in upstream "myapp" in /etc/nginx/sites-enabled/myapp:22 │nginx: 配置文件 /etc/nginx/nginx.conf 测试失败

I am pretty sure that means that nginx cannot find myapp running in docker and cannot communicate with it but I exposed the port and from what I understood the container name is host name so it should work.我很确定这意味着 nginx 找不到在 docker 中运行的 myapp 并且无法与之通信,但是我公开了端口,并且据我所知,容器名称是主机名,因此它应该可以工作。 Does anyone know how to make them communicate?有谁知道如何让他们沟通? I didnt find any tutorials on internet that wouldnt also dockerize the nginx I DONT WANT IT.我没有在互联网上找到任何不会对我不想要的 nginx 进行 dockerize 的教程。

Any help is appretiated任何帮助表示赞赏

EDIT: This is my Dockerfile编辑:这是我的 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

[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   

The solution解决方案

  1. Change the socket=8081 to socket=0.0.0.0:8081将 socket=8081 改为 socket=0.0.0.0:8081
  2. Change the nginx to listen on localhost:8081更改 nginx 监听 localhost:8081
  3. Add ports 8081:8081 to the docker-compose将端口 8081:8081 添加到 docker-compose

Docker has internal DNS server working on 127.0.0.11 inside container. Docker 有内部 DNS 服务器在容器内的127.0.0.11工作。 If your nginx is not in container you cannot use it to resolve myapp name.如果您的 nginx 不在容器中,则无法使用它来解析myapp名称。 Still, you can pick one of those:不过,您可以选择其中之一:

  1. Make your container listen on host's ports:让你的容器监听主机的端口:
services:
  myapp:
    ports:  
      # host:container
      - 8081:8081

Then reflect this change in your nginx configuration:然后在您的 nginx 配置中反映此更改:

 location / {
     include    uwsgi_params;
     uwsgi_pass localhost:8081;
 }
  1. Go back to using unix sockets but this time put socket into host's directory.回到使用 unix 套接字,但这次将套接字放入主机目录。 To do that, first mount host's /tmp into container:为此,首先将主机的/tmp挂载到容器中:
services:
  myapp:
    volumes:  
      # host:container
      - /tmp:/tmp

Then configure your application to put the socket into /tmp .然后配置您的应用程序以将套接字放入/tmp The socket will appear in host's /tmp and you can configure nginx to communicate to it.套接字将出现在主机的/tmp ,您可以配置 nginx 与它通信。 You may slightly improve this if you mount not the whole /tmp but a single directory inside it.如果您不是挂载整个/tmp而是挂载其中的单个目录,您可能会稍微改进这一点。 /tmp/myapp for example.例如/tmp/myapp That way you eliminate chances that your container will mess something with host's files.这样,您就可以消除容器将主机文件弄乱的可能性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM