Flask应用在docker上的配置

参考


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

版权所有,转载请注明出处 luowei.github.io.