Filip Bartoš
Filip Bartoš

Reputation: 301

Python app deployment using nginx and dockerize flask app

I would like to deploy flask app on my VPS. 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.

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

So I changed my nginx conf from

   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

nginx: [emerg] host not found in upstream "myapp" in /etc/nginx/sites-enabled/myapp:22 │nginx: configuration file /etc/nginx/nginx.conf test failed

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. 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.

Any help is appretiated

EDIT: This is my 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   

The solution

  1. Change the socket=8081 to socket=0.0.0.0:8081
  2. Change the nginx to listen on localhost:8081
  3. Add ports 8081:8081 to the docker-compose

Upvotes: 0

Views: 338

Answers (1)

anemyte
anemyte

Reputation: 20176

Docker has internal DNS server working on 127.0.0.11 inside container. If your nginx is not in container you cannot use it to resolve myapp name. 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:

 location / {
     include    uwsgi_params;
     uwsgi_pass localhost:8081;
 }
  1. Go back to using unix sockets but this time put socket into host's directory. To do that, first mount host's /tmp into container:
services:
  myapp:
    volumes:  
      # host:container
      - /tmp:/tmp

Then configure your application to put the socket into /tmp. The socket will appear in host's /tmp and you can configure nginx to communicate to it. You may slightly improve this if you mount not the whole /tmp but a single directory inside it. /tmp/myapp for example. That way you eliminate chances that your container will mess something with host's files.

Upvotes: 1

Related Questions