怎么使用Docker运行Python代码

需要使用Docker镜像运行Python代码

  1. 编写Dockefile文件
    示例如下
# 需要使用的镜像版本
FROM python:3.11
# 代码工作目录
WORKDIR /code
# 拷贝依赖文件
COPY requirements.txt requirements.txt
# 安装依赖
RUN pip install -r requirements.txt -i https://pypi.doubanio.com/simple/
# 拷贝所有代码
COPY . .
# 容器运行命令
CMD ["python", "run.py"]
  1. 构建Docker镜像
    使用Python构建镜像,镜像名为pythontest
# docker build -t pythontest .
  1. 查看镜像
# docker images
REPOSITORY                             TAG       IMAGE ID       CREATED             SIZE
pythontest                             latest    21eeaac0870f   31 seconds ago      1.51GB
  1. 运行
# 后台运行,启动端口
# docker run -d -p 7865:7865 pythontest
posted @ 2024-03-05 11:52  minseo  阅读(133)  评论(0编辑  收藏  举报