CentOS7.6 Docker+Gunicorn部署Flask

CentOS7.6 Docker+Gunicorn部署Flask,第1张

CentOS7.6 Docker+Gunicorn部署Flask 安装Docker
[root@ecs-9e76 ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
[root@ecs-9e76 ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@ecs-9e76 ~]# yum makecache fast
[root@ecs-9e76 ~]# yum install -y docker-ce-19.03.9 docker-compose
创建Flask测试项目
[root@ecs-9e76 ~]# mkdir flasktest
[root@ecs-9e76 ~]# cd flasktest

[root@ecs-9e76 flasktest]# vi app.py

内容如下:

# -*- coding:utf-8 -*-
from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'

if __name__ == "__main__":
    app.run()
创建Gunicorn配置
[root@ecs-9e76 flasktest]# vi gun.conf

内如如下:

bind = '0.0.0.0:8001'
backlog = 512
timeout = 30
worker_class = 'gevent'
workers = 4
threads = 8
loglevel = 'info'
accesslog = "gunicorn_access.log"
errorlog = "gunicorn_error.log"
reload=True
创建Dockerfile
[root@ecs-9e76 flasktest]# vi Dockerfile 

内容如下:

FROM python:3.7
MAINTAINER "xx"

ENV PIPURL "https://repo.huaweicloud.com/repository/pypi/simple"
WORKDIR /flasktest

COPY . .
RUN pip --no-cache-dir install  -i ${PIPURL} --upgrade pip
RUN pip --no-cache-dir install  -i ${PIPURL} gunicorn flask gevent

CMD gunicorn  -c gun.conf app:app
构建Docker镜像
[root@ecs-9e76 flasktest]# docker build . -t=flask-test:latest
验证Docker镜像
[root@ecs-9e76 flasktest]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
flask-test   latest    b1496afce06d   2 minutes ago   943MB
python       3.7       869a8debb0fd   10 days ago     903MB
运行Docker镜像
[root@ecs-9e76 flasktest]# docker run -it -p 80:8001 --name="flask-test" -d flask-test
查看正在运行的容器
[root@ecs-9e76 flasktest]# docker ps
ConTAINER ID   IMAGE        COMMAND                  CREATED          STATUS          PORTS                  NAMES
b2b8fabfd61a   flask-test   "/bin/sh -c 'gunicor…"   1 minutes ago   Up 1 minutes   0.0.0.0:80->8001/tcp   flask-test
测试
[root@ecs-9e76 flasktest]# curl 127.0.01
Hello, World!
优化

创建的镜像文件较大,通过观察可知文件较大主要因为Python3.7较大,因此可改用其他尺寸较小的Python镜像,比如python:3.7-slim。

通过观察可知,使用python:3.7-slim镜像构建的镜像尺寸小了近800M。

[root@ecs-9e76 flasktest]# docker images
REPOSITORY   TAG        IMAGE ID       CREATED         SIZE
flask-test   v2         7dacd036b8a2   9 seconds ago   160MB
flask-test   v1         aac1d385c323   3 hours ago     943MB
python       3.7-slim   e7de75b2cb5a   10 days ago     120MB
python       3.7        869a8debb0fd   10 days ago     903MB

Dockerfile内容如下:

FROM python:3.7-slim
MAINTAINER "xx"

ENV PIPURL "https://repo.huaweicloud.com/repository/pypi/simple"
WORKDIR /flasktest

COPY . .
RUN pip --no-cache-dir install  -i ${PIPURL} --upgrade pip
RUN pip --no-cache-dir install  -i ${PIPURL} gunicorn flask gevent

CMD gunicorn  -c gun.conf app:app

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5620017.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存