[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构建Docker镜像" 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
[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
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)