使用url路径在容器中运行flask app

时间:2018-06-05 14:20:29

标签: docker nginx flask dockerfile uwsgi

我有一个我希望作为容器运行的Flask应用程序,并且能够将其加载到我的服务器的url路径上,例如http://example.com/flask。我设法在根网址http://example.com上启动并运行Flask容器,但是当在路径上设置时,页面中的所有网址都保持<a href="/login"></a>而不是<a href="/flask/login"></a>。我的设置是:

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;
  }
}

我使用nginx直接提供静态文件,使用uwsgi将应用程序绑定到套接字。我是Flask的新手,试图运行别人做的项目,所以我不知道这是不是一个好方法,但它不起作用

0 个答案:

没有答案