Flask应用在docker上的配置
参考
- 基础
- Python Tutorials:https://pythonspot.com/
- Login authentication with Flask:https://pythonspot.com/login-authentication-with-flask/
- Using Flask-Login for User Management with Flask:https://realpython.com/using-flask-login-for-user-management-with-flask/
- Flask基础参考:
- Flask Quickstart:http://flask.pocoo.org/docs/1.0/quickstart/#routing
- Jinja Template Designer Documentation:http://jinja.pocoo.org/docs/2.10/templates/#synopsis
- Flask WTForms Flask-WTF:https://flask-wtf.readthedocs.io/en/stable/
- Download Flask Examples:https://pythonspot-9329.kxcdn.com/wp-content/uploads/2016/07/FlaskExamples.zip
- Python Web Applications with Flask – Part I:https://realpython.com/python-web-applications-with-flask-part-i/
- How To Deploy a Flask Application on an Ubuntu VPS:https://www.digitalocean.com/community/tutorials/how-to-deploy-a-flask-application-on-an-ubuntu-vps
- Ubuntu Apache Server 部署 Flask 程序:https://eliyar.biz/deploy-a-flask-application-on-an-ubuntu-apache-server/
- 纯JS 全屏滚动 / 整屏翻页:https://blog.csdn.net/tangdou5682/article/details/52351404
- docker相关
- 在 Ubuntu 上使用 uWSGI 和 Nginx 部署 Flask 项目:https://lufficc.com/blog/how-to-serve-flask-applications-with-uwsgi-and-nginx-on-ubuntu
- 使用Nginx和uWSGI部署Flask程序:http://www.xumenger.com/nginx-flask-python-20180331/
- Flask + uWSGI + nginx + mysql + Docker-compose 搭建环境:https://blog.szfszf.top/tech/flask-uwsgi-nginx-mysql-docker-compose-%E6%90%AD%E5%BB%BA%E7%8E%AF%E5%A2%83/
- Docker构建nginx+uwsgi+flask镜像(一):https://www.cnblogs.com/beiluowuzheng/p/10219506.html
- Docker构建nginx+uwsgi+flask镜像(二):https://www.cnblogs.com/beiluowuzheng/p/10220860.html
- 如何解决Python包依赖问题:https://www.jianshu.com/p/8a7de18e0ffb
- share .sock file between nginx docker and uwsgi docker:https://stackoverflow.com/questions/44424170/how-to-share-sock-file-between-nginx-docker-and-uwsgi-docker
- Python/WSGI 应用快速入门:https://uwsgi-docs-cn.readthedocs.io/zh_CN/latest/WSGIquickstart.html
- 使用Nginx和uWSGI来运行Python应用:http://www.bjhee.com/nginx-uwsgi.html
- 给Django应用的所有URL添加前缀(SCRIPT_NAME)的六种方案:https://note.qidong.name/2017/11/uwsgi-script-name/
docker 上布署
构建Flask基础镜像
创建Flask-base.dockerfile
FROM alpine:3.8
# 添加标签说明
LABEL author="luowei" email="luowei@wodedata.com" purpose="flask基础镜像"
# 安装依赖
RUN echo "https://mirror.tuna.tsinghua.edu.cn/alpine/v3.8/main/" > /etc/apk/repositories \
&& apk add --update --upgrade \
&& apk add --no-cache bash vim python3 uwsgi uwsgi-python3 \
&& apk add --no-cache --virtual .build-deps gcc musl-dev curl \
&& pip3 install --no-cache-dir --upgrade pip \
&& ln -s /usr/bin/python3 /usr/bin/python \
# && ln -s /usr/bin/pip3 /usr/bin/pip \
&& pip3 install flask
制作Flask基础镜像
docker build -t myflask-base:v1.0 . -f Flask-base.dockerfile
构建Flask App镜像
创建Dockerfile
FROM myflask-base:v1.0 AS myflask
# 添加标签说明
LABEL author="luowei" email="luowei@wodedata.com" purpose="uwsgi+Python3基础镜像"
# 创建工作路径
RUN mkdir /app
ENV APP_DIR /app
VOLUME [${APP_DIR},/tmp/uwsgi.sock]
WORKDIR ${APP_DIR}
# 将本地app目录下的内容拷贝到容器的app目录下
COPY ./app/ /app/
# 安装依赖
RUN chmod 777 /tmp/ -R \
&& pip3 install -r /app/requirements.txt --no-cache-dir
EXPOSE 9000
# 启动uwsgi
# ENTRYPOINT ["/entrypoint.sh"]
ENTRYPOINT uwsgi --ini /app/uwsgi.ini
uwsgi.ini文件
[uwsgi]
; uwsgi-socket = /tmp/uwsgi.sock
socket=:9000
; chdir = ./
chmod-socket = 777
callable = app
plugin = python3
wsgi-file = app/__init__.py
; 加上以这这行代码支持nginx配置带前缀的location
route-run = fixpathinfo:
; ; 支持nignx配置location前缀为'/myflask'
; mount=/myflask=app/__init__.py
; manage-script-name=true
buffer-size = 65535
processes = %(%k * 2)
threads = %(%k * 10)
disable-logging = true
; 这里的processes和threads,分别是开启的进程数和线程数,而%k是魔数变量,代表CPU核数,
; 如果我们是双核CPU,那这里的processes和threads分别为4和40,即有4个进程,每个进程有40个线程。
制作镜像
docker build -t myflask:v1.0 .
运行flask app
docker run --rm -it\
--name my-flask \
--network network_mynginx \
-p 9000:9000 \
myflask:v1.0
运行nginx
nginx配置
server {
......
# location / {
# root /usr/share/nginx/flask;
# try_files $uri @myflask;
# }
# location @myflask {
# include uwsgi_params;
# uwsgi_pass my-flask:9000;
# uwsgi_read_timeout 60s;
# uwsgi_send_timeout 60s;
# uwsgi_connect_timeout 60s;
# }
# location前缀
location /myflask {
# rewrite ^/myflask/?(.*)$ /$1 break;
# root /usr/share/nginx/flask;
include uwsgi_params;
uwsgi_param SCRIPT_NAME /myflask;
uwsgi_pass my-oss:5000;
uwsgi_read_timeout 60s;
uwsgi_send_timeout 60s;
uwsgi_connect_timeout 60s;
}
}
启动nginx
cd /var/www/mynginx
docker run ---d -p 80:80 \
--name mynginx \
--network network_mynginx \
--hostname=wodedata.com \
--add-host=app.wodedata.com:127.0.0.1 \
--add-host=bbs.wodedata.com:127.0.0.1 \
--add-host=myoss.wodedata.com:127.0.0.1 \
-v $PWD/conf/conf.d:/etc/nginx/conf.d \
-v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf \
-v $PWD/log:/var/log/nginx \
-v $PWD/www:/usr/share/nginx \
-v $PWD/../jekyll/_site:/usr/share/nginx/jekyll \
-v $PWD/../php:/usr/share/nginx/php \
-v $PWD/../myoss:/usr/share/nginx/flask \
nginx