0x01 导出python依赖
在 app.py 当前目录下执行名令, 得到依赖文件
pip3 freeze > requirements.txt
但不是所有依赖都是必需的, 可以根据app.py 的头部依赖删除不需要的部分
此时, 当前目录存在两个文件
- app.py
- requirements.txt
0x02 Dockerfile 编写
关键点是 基础镜像的选择, 然后在Dockerfile中执行pip install 动作, 完成依赖拉取
FROM python:3.8-slim-buster
COPY ./requirements.txt /
COPY ./app.py /
RUN pip install -r /requirements.txt
EXPOSE 80
CMD ["python", "/app.py"]
0x03 打镜像, 自测
docker build .
docker run -p 10080:80 {imageId}
网友评论