如何在 docker 镜像中安装 python 模块? [英] How can I install python modules in a docker image?

查看:31
本文介绍了如何在 docker 镜像中安装 python 模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an image called: Image and a running container called: container. I want to install pytorch and anacoda. what's the easiest way to do this? Do I have to change the dockerfile and build a new image? Thanks a lot.

推荐答案

是的,最好的方法是构建您的图像,使其包含 Python 模块.

Yes, the best thing is to build your image in such a way it has the python modules are in there.

这是一个例子.我使用构建依赖项构建了一个映像:

Here is an example. I build an image with the build dependencies:

$ 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 编译器和工具链,所以我也在安装这些.

Note, I am installing Python's pip inside the image, so later I can download packages from pypi. Packages like numpy might require a C compiler and tool chain, so I am installing these too.

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

After building the packages which require the build tools chain I remove the tool chain packages:

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

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

After you have your base image, you can run your application code in an image building on top of it:

$ 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 运行它.

这篇关于如何在 docker 镜像中安装 python 模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆