当前位置:   article > 正文

Windows 11 使用DockerFile起Python服务

Windows 11 使用DockerFile起Python服务

#Windows 11#Dockerfile#Python

1. 创建dockerfile

1. 在项目的根目录进行
pip freeze > requirements.txt

改命令将已安装的所有依赖包及其版本号导出到一个名为requirements.txt文本文件中。你可以打开该文件查看已安装的依赖包及其版本

2. 在项目的根目录创建Dockerfile

Dockerfile内容:
  1. # 使用Python 3.9 作为基础镜像
  2. FROM python:3.9
  3. # 设置工作目录
  4. WORKDIR /app
  5. # 将当前目录下的所有文件复制到容器的工作目录中
  6. COPY . /app
  7. # 安装依赖包
  8. RUN pip install --no-cache-dir -r requirements.txt
  9. # 设置环境变量
  10. ENV PYTHONUNBUFFERED=1
  11. # 定义容器启动时执行的命令
  12. CMD ["python", "/app/main.py"]

2. 基于dockerfile构造镜像

在根目录执行命令:

docker build -t 服务名字:版本 .

3. 错误解决

在构建镜像的时候报错

  1. error during connect: this error may indicate that the docker daemon is not running: Get "http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.24/version": open //./pipe/docker_engine: The system cannot find the file specified.
  2. Client:
  3. Cloud integration: v1.0.35-desktop+001
  4. Version: 24.0.5
  5. API version: 1.43
  6. Go version: go1.20.6
  7. Git commit: ced0996
  8. Built: Fri Jul 21 20:36:24 2023
  9. OS/Arch: windows/amd64
  10. Context: default


解决方法:

        网上众说纷纭,我是Windows的命令行cmd 中输入:bcdedit /set hypervisorlaunchtype auto 再重開機,再在Windows上登录Docker。再构建镜像就没有问题了。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/54911?site
推荐阅读
相关标签
  

闽ICP备14008679号