【问题标题】:Run pip in python docker在 python docker 中运行 pip
【发布时间】:2021-08-15 01:38:53
【问题描述】:

我对 docker 完全陌生(在 Windows 10 机器上)。我打算将 python 开发环境设置为 docker 容器。我所做的大部分阅读都涉及到 Dockerfile 的使用。我想从头开始,而不是纯粹使用命令。

我打算做的是非常基本的要求:拥有 python docker 映像,并且我应该能够在该映像中安装更多库并将这些更新提交到该映像。但我想完全使用命令(而不是通过 Dockerfile)来完成。

我在 Windows 10 机器上使用 Docker Desktop。我做了docker pull python:latest,它像这样拉出图像:

C:\Users\MyHomeDirectory>docker pull python:latest
latest: Pulling from library/python
d960726af2be: Pull complete
e8d62473a22d: Pull complete
8962bc0fad55: Pull complete
65d943ee54c1: Pull complete
532f6f723709: Pull complete
1334e0fe2851: Pull complete
062ada600c9e: Pull complete
aec2e3a89371: Pull complete
1ec7c3bcb4b2: Pull complete
Digest: sha256:65367d1d3eb47f62127f007ea1f74d1ce11be988044042ab45d74adc6cfceb21
Status: Downloaded newer image for python:latest
docker.io/library/python:latest

然后我做了docker images,它显示python最新图像的大小为886 MB

C:\Users\Tejas.Khajanchee>docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
python        latest    5b3b4504ff1f   47 hours ago   886MB

我也可以通过 docker run -it python 进入交互式 python 并生成交互式 shell:

Python 3.9.5 (default, May 12 2021, 15:26:36)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import gc
>>> import numpy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'numpy'
>>>
>>> import pandas
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'pandas'
>>>
>>> import openpyxl
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'openpyxl'
>>>

但很明显,有些库没有安装。但这就是我卡住的地方。如何将库安装到 python 映像中并更新映像。另外,如果这个 shell 是到目前为止我唯一可以做的事情,886 MB 内容代表什么?我也希望能够使用这个 docker 图像运行脚本。当我尝试在一个非常基本的 hello world 脚本上执行此操作时,会出现以下错误:

C:\Users\MyHomeDirectory\Downloads>docker run -it python a.py
docker: Error response from daemon: OCI runtime create failed: container_linux.go:367: starting container process caused: exec: "a.py": executable file not found in $PATH: unknown.

我希望能够完全使用命令而不是 Dockerfile 来执行此操作。请帮忙。

【问题讨论】:

  • 你能进入你的容器并运行python -m pip install numpy吗?如果是我,我会使用 docker 文件来处理这个问题。
  • 不,它不允许我进入容器。我做了docker exec -it python /bin/bash。它给出了一个错误:Error: No such container: python.

标签: python-3.x docker pip


【解决方案1】:

首先,看来你混淆了imagecontainer 的概念。

  • Docker 镜像:只读,用作容器的基础
  • Docker 容器:在 docker 镜像的只读层上叠加一个可写层,所有容器都以镜像为基础

第二,对你来说,你提到要在镜像中安装numpy,最好的方法是自定义一个Dockerfile,如下所示:

Dockerfile:

FROM python
RUN pip install numpy

然后,用docker build -t newpython .构建一个新镜像

但是,你提到你不想使用Dockerfile,那么接下来就是替换了:

  1. 在容器中安装 numpy:

    docker run -it python /bin/bash
    # pip install numpy
    
  2. 使用docker ps -a获取容器id,例如:0a6b4df8e2c2,然后将这个已经安装了numpy的容器提交到新镜像:

    docker commit 0a6b4df8e2c2 newpython
    

最后,所有新容器都需要基于newpython 映像而不是python 映像运行,因为只有newpython 映像安装了numpy

   docker run --rm newpython python -c "import numpy; print(numpy.__version__)"
   1.20.3

附加,对于docker run -it python a.py,我认为您误解了这个概念。像python a.py这样的容器命令表示该命令将在容器中执行,因此a.py应该在容器中,而不是在主机中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-18
    • 2019-06-29
    • 2022-01-12
    • 2013-09-10
    • 1970-01-01
    相关资源
    最近更新 更多