Docker 构建镜像

环境

Docker: Docker version 19.03.4, build 9013bf583a
System: CentOS Linux release 8.0.1905 (Core)
Python: Python 3.6.8
Flask: Flask 1.0.3
Gunicorn: gunicorn 19.9.0

Dockerfile 指令详解

  • FROM or FROM :
  • COPY
  • ADD
  • CMD or CMD ["可执行文件", "参数1", "参数2"...]
  • ENV or ENV = =...
  • EXPOSE [...]
  • WORKDIR
  • USER [:]
  • RUN or RUN ["可执行文件", "参数1", "参数2"]
  • VOLUME or VOLUME ["", ""...]

构建 Docker 镜像

Dockerfile

# 基于 Python:3.6 基础镜像
FROM python:3.6
# Docker 的工作目录
WORKDIR /app
# 复制文件到工作目录
ADD . /app
# 安装 Python 的依赖
RUN pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
# 设置运行的必要参数
ENV env=dev
# 程序启动的命令
CMD ["gunicorn", "manager:app", "-c", "run.ini"]

application.py

# coding:utf-8

from flask import Flask

app = Flask(__name__)


@app.route('/index')
def index():
    return 'Hello docker'


if __name__ == '__main__':
    app.run(debug=True)

run.ini

workers = 5
threads = 4
daemon = 'false'
worker_class = "gevent"
bind = "0.0.0.0:8000"

requirements.txt

gunicorn
gevent
flask

构建镜像


# docker build -t <tag>:<version> <dir>
docker build -t gunicron:v1.0.0 .

创建 Docker 容器

docker run -d --name gunicron-flask -p 8084:8084 gunicron

Docker 容器操作

docker [start | stop | restart] gunicorn-flask

01-15 14:39