linux - 如何在 docker 镜像中安装 python 模块?

标签 linux docker dockerfile torch pytorch

我有一个名为:Image 的图像和一个名为:container 的正在运行的容器。我想安装 pytorchanacoda。最简单的方法是什么?我是否必须更改 dockerfile 并构建新镜像?非常感谢。

最佳答案

是的,最好的办法是以其中包含 python 模块的方式构建您的图像。

这是一个例子。我使用构建依赖项构建图像:

$ docker build -t oz123/alpine-test-mycoolapp:0.5 - < Image
Sending build context to Docker daemon  2.56 kB
Step 1 : FROM alpine:3.5
 ---> 88e169ea8f46
Step 2 : ENV MB_VERSION 3.1.4
 ---> Running in 4587d36fa4ae
 ---> b7c55df49803
Removing intermediate container 4587d36fa4ae
Step 3 : ENV CFLAGS -O2
 ---> Running in 19fe06dcc314
 ---> 31f6a4f27d4b
Removing intermediate container 19fe06dcc314
Step 4 : RUN apk add --no-cache python3 py3-pip gcc python3-dev py3-cffi    file git curl autoconf automake py3-cryptography linux-headers musl-dev libffi-dev openssl-dev build-base
 ---> Running in f01b60b1b5b9
fetch http://dl-cdn.alpinelinux.org/alpine/v3.5/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.5/community/x86_64/APKINDEX.tar.gz
(1/57) Upgrading musl (1.1.15-r5 -> 1.1.15-r6)
(2/57) Upgrading zlib (1.2.8-r2 -> 1.2.11-r0)
(3/57) Installing m4 (1.4.17-r1)
(4/57) Installing perl (5.24.0-r0)
(5/57) Installing autoconf (2.69-r0)
(6/57) Installing automake (1.15-r0)
(7/57) Installing binutils-libs (2.27-r1)
...

请注意,我在图像中安装了 Python 的 pip,因此稍后我可以从 pypi 下载软件包。像 numpy 这样的包可能需要 C 编译器和工具链,所以我也安装了它们。

在构建需要构建工具链的包后,我删除了工具链包:

RUN apk del file pkgconf autoconf m4 automake perl g++ libstdc++

获得基础镜像后,您可以在其中运行应用程序代码 在其之上构建图像:

$ cat Dockerfile

    FROM oz123/alpine-test-mycoolapp
    ADD . /code
    WORKDIR /code
    RUN pip3 install -r requirements.txt -r requirements_dev.txt
    RUN pip3 install -e .
    RUN make clean
    CMD ["pytest", "-vv", "-s"]

我只是用 docker 运行它。

关于linux - 如何在 docker 镜像中安装 python 模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44339375/

相关文章:

docker - 如何处理 make missing from alpine docker image?

docker - 如何使一个简单的 docker 服务可以从外部主机访问

linux - glib-networking 找不到 gio/gio.h

docker - 如何通过命令行指定在docker-compose中使用哪个env_file

linux - 为什么 “who” 命令不显示从 “username” 用户记录的 "root"?

ruby-on-rails - Docker无法在Travis CI中与Postgresql建立连接

docker - 成为Docker容器中的root

Jenkins 的 Dockerfile

c - 以编程方式在 C 中检查 NIC 在 Linux 中关闭时是否有链接

linux - Cygwin bash 文件