1

I have a Flask app that I want to run as a container, and be able to load it on url path of my server, something like http://example.com/flask. I managed to get a Flask container up and running on the root url http://example.com, but when setting on the path all the urls within the pages remain like <a href="/login"></a> instead of being <a href="/flask/login"></a>. My setup is:

Dockerfile

FROM ubuntu:latest

RUN apt-get -y update

RUN apt-get -y install build-essential python3-dev python3-pip python3-setuptools python3-wheel uwsgi-plugin-python3 python3-cffi libcairo2 libpango-1.0-0 libpangocairo-1.0-0 libgdk-pixbuf2.0-0 libffi-dev shared-mime-info libmysqlclient-dev

RUN mkdir -p /etc/logs/webapp

ENV FLASK_APP=main.py
ENV LC_ALL=C.UTF-8
ENV LANG=C.UTF-8

WORKDIR /app
ADD requirements.txt /app/requirements.txt
RUN python3 -m pip install -r requirements.txt
ADD . /app

ADD app.fcgi /app/app.fcgi
RUN chmod +x /app/app.fcgi
RUN python3 -m pip install flup

RUN apt-get -y install nginx
ADD nginx.conf /etc/nginx/conf.d/default.conf
RUN rm /etc/nginx/sites-enabled/*

ADD entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

ENTRYPOINT [ "/entrypoint.sh" ]

entrypoint.sh

#!/bin/sh
service nginx start
/app/app.fcgi > /app/logs/app.log

app.fcgi

#!/usr/bin/python3
from flup.server.fcgi import WSGIServer
from main import app

if __name__ == '__main__':
    WSGIServer(app, bindAddress='/var/run/app-fcgi.sock').run()

nginx.conf

server {
  listen 80 default;

  server_name _;

  location /css {
    root /app/static/;
  }
  location /js {
    root /app/static/;
  }
  location /assets {
    root /app/static/;
  }

  location / { try_files $uri @app; }
  location @app {
    include uwsgi_params;
    uwsgi_pass unix:/var/run/app-fcgi.sock;
  }
}

I'm using nginx to serve the static files directly and uwsgi to bind the app to a socket. I'm new to Flask and trying to run a project someone else did so I have no idea if this is a good approach, but it doesn't work

Guillaume
  • 2,912
  • 3
  • 35
  • 59
  • 1
    The answer is there. It has to do with monting the flask application in the wsgi server. Nothing special with doing it in a container. – Robert Moskal Jun 05 '18 at 15:33
  • For those having the same issue, I solved it by adding `fastcgi_split_path_info ^(/flask)(.*)$; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param SCRIPT_NAME $fastcgi_script_name;` into the `location /flask` in nginx. Seems `SCRIPT_NAME` will add the subpath automatically to all the routes, without changing anything in the flask code. – Guillaume Jun 12 '18 at 11:02

0 Answers0