How to Install Python Modules in a Docker Image

Install python package in docker file

Recommended base image

As suggested in my comment, you could write a Dockerfile that looks like:

FROM python:3

RUN pip install --no-cache-dir --upgrade pip && \
pip install --no-cache-dir nibabel pydicom matplotlib pillow med2image
# Note: we had to merge the two "pip install" package lists here, otherwise
# the last "pip install" command in the OP may break dependency resolution…

CMD ["cat", "/etc/os-release"]

And the command example above could confirm at runtime (docker build --pull -t test . && docker run --rm -it test) that this image is based on the GNU/Linux distribution "Debian stable".

Generic Dockerfile template

Finally to give a comprehensive answer, note that a good practice regarding Python dependencies consists in specifying them in a declarative way in a dedicated text file (in alphabetical order, to ease review and update) so that for your example, you may want to write the following file:

requirements.txt

matplotlib
med2image
nibabel
pillow
pydicom

and use the following generic Dockerfile

FROM python:3

WORKDIR /usr/src/app

COPY requirements.txt ./

RUN pip install --no-cache-dir --upgrade pip \
&& pip install --no-cache-dir -r requirements.txt

COPY . .

CMD ["python", "./your-daemon-or-script.py"]

To be more precise, this is the approach suggested in the documentation of the Docker official image python, §. How to use this image

How can I install python modules in a docker image?

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)
...

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"]

I simply run this with docker.

How to add a Python module to a docker container?

Use Dockerfile to install python packages.

Dockerfile content

FROM python:3

RUN pip install --upgrade pip && \
pip install aldryn_apphooks_config
$: docker build -t "web:python" .
Sending build context to Docker daemon 3.584kB
Step 1/2 : FROM python:3
---> 59a8c21b72d4
Step 2/2 : RUN pip install --upgrade pip && pip install aldryn_apphooks_config
---> Running in e16a679af3d8
.
.
.
.
Successfully built aldryn-apphooks-config django-treebeard djangocms-admin-style
Installing collected packages: pytz, Django, django-appdata, django-treebeard, django-classy-tags, django-sekizai, djangocms-admin-style, django-formtools, django-cms, aldryn-apphooks-config
Successfully installed Django-2.1.8 aldryn-apphooks-config-0.5.2 django-appdata-0.2.1 django-classy-tags-0.8.0 django-cms-3.6.0 django-formtools-2.1 django-sekizai-0.10.0 django-treebeard-4.3 djangocms-admin-style-1.3.0 pytz-2018.9
Removing intermediate container e16a679af3d8
---> 78ecd015d983
Successfully built 78ecd015d983
Successfully tagged web:python

Start the container and verify the package installion.

$ docker run -it web:python /bin/bash
root@1d1df7416b8a:/# pip freeze
aldryn-apphooks-config==0.5.2
Django==2.1.8
django-appdata==0.2.1
django-classy-tags==0.8.0
django-cms==3.6.0
django-formtools==2.1
django-sekizai==0.10.0
django-treebeard==4.3
djangocms-admin-style==1.3.0
pytz==2018.9

How to install a python module in a docker container

You can get bash from your container with this command:

docker-compose exec freqtrade bash

and then:

pip install finta

OR
run only one command:

docker-compose exec freqtrade pip install finta

If the above solutions didn't work, You can run docker ps command and get container id of your container. Then

docker exec -it CONTAINER_ID bash
pip install finta

How to add Python libraries to Docker image

To customize an image you generally want to create a new one using the existing image as a base. In Docker it is extremely common to create custom images when existing ones don't quite do what you want. By basing your images off public ones you can add your own customizations without having to repeat (or even know) what the base image does.

  1. Add the necessary steps to a new Dockerfile.

    FROM tensorflow/tensorflow:latest-gpu-jupyter

    RUN <extra install steps>
    COPY <extra files>

    RUN and COPY are examples of instructions you might use. RUN will run a command of your choosing such as RUN pip install matplotlib. COPY is used to add new files from your machine to the image, such as a configuration file.

  2. Build and tag the new image. Give it a new name of your choosing. I'll call it my-customized-tensorflow, but you can name it anything you like.

    Assuming the Dockerfile is in the current directory, run docker build:

    $ docker build -t my-customized-tensorflow .
  3. Now you can use my-customized-tensorflow as you would any other image.

    $ docker run my-customized-tensorflow


Related Topics



Leave a reply



Submit