python项目大多都需要引入而外的模块,显然每个项目的模块都装在一起显然不好管理,而是应该给每个项目建立一个专属的python环境。像docker、anaconda、python3自带的venv都可以用来建立python环境。而本文将介绍docker下建立独立的python运行环境。
一个用Flask写的简单web服务
http端口是1068
为了演示方便,Dockerfile、requirements.txt等都放在项目目录下:
如果不了解requirements.txt的同学,建议先百度下。
把目录切换到项目下,使用命令生成requirements.txt:pip freeze > requirements.txt
打开requirements.txt,会有类似的内容:
FROM python:3
WORKDIR /usr/src/app
COPY requirements.txt ./
RUN pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --no-cache-dir -r requirements.txt
CMD [ "python", "./index.py" ]
langid:
build: .
ports:
- "8080:1068"
volumes:
- "./:/usr/src/app"
container_name: python-app
docker-compose up