aurelbzh
aurelbzh

Reputation: 23

problem trying to make a reverse proxy on a flask gunicorn application with nginx and docker

i'm trying to make a reverse proxy and dockerize it for my flask application with nginx, gunicorn, docker and docker-compose . Before that the nginx part was in the same container than the web app, i'm trying to separe it. My docker_compose yaml file is :

version: '3.6'

services:
  nginx:
    restart: always
    build: ./nginx/
    ports:
      - 8008:8008
    networks:  
      - web_net

  flask_min:
    build: .
    image: flask_min
    container_name: flask_min
    expose:
      - "8008"
    networks:  
      - web_net
    depends_on:
      - nginx

networks:
  web_net:
    driver: bridge

My dockerfile is :

    FROM python:3.6
    MAINTAINER aurelien beliard ([email protected])
    RUN apt update
    COPY . /usr/flask_min
    WORKDIR /usr/flask_min
    RUN useradd -r -u 20979 -ms /bin/bash aurelien.beliard
    RUN pip3 install -r requirements.txt
   CMD gunicorn -w 3 -b :8008 app:app

my nginx docker file is

FROM nginx
COPY ./flask_min /etc/nginx/sites-available/
RUN mkdir /etc/nginx/sites-enabled
RUN ln -s /etc/nginx/sites-available/flask_min /etc/nginx/sites-enabled/flask_min

my nginx config file in /etc/nginx sites-available and sites-enabled is named flask-min :

server {
    listen 8008; 
    server_name http://192.168.16.241/ ;
    charset utf-8; 
    location / { 
        proxy_pass http://flask_min:8008;
         } }

the requirements.txt file is :

Flask==0.12.2
grequests==0.3.0
gunicorn==19.7.1
Jinja2==2.10

The 2 containers are well created, gunicorn start well but i can't access to the application and there is nothing in the nginx access and error log .

If you have any idea it will be very appreciated.

ps sorry for the fault english is not my native language.

Upvotes: 1

Views: 604

Answers (2)

Michael Barany
Michael Barany

Reputation: 1669

As mentioned in Maxm's answer, flask is depending on nginx to startup first. One way to fix it is to reverse the dependency order, but I think there's a more clever solution that doesn't require the dependency.

Nginx tries to do some optimization by caching the dns results of proxy_pass, but you can make it more flexible by setting it to a variable. This allows you to freely restart flask without having to also restart nginx.

Here's an example:

resolver 127.0.0.11 ipv6=off;
set $upstream http://flask_min:8008;
proxy_pass $upstream;

Upvotes: 1

maxm
maxm

Reputation: 3667

server_name should just be the host. try localhost or just _.

you can also do multiple hosts: server_name 192.168.16.241 localhost;

The depends_on should be on nginx not flask_min. Remove it from flask and add:

depends_on:
  - flask_min

To nginx.

See if that works, let me know if you run into any more snags.

Upvotes: 0

Related Questions