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

Answer from ErikMD on Stack Overflow
🌐
PyPI
pypi.org › project › docker
docker · PyPI
Maintainer: Docker Inc. ... A Python library for the Docker Engine API.
      » pip install docker
    
Published   May 23, 2024
Version   7.1.0
Discussions

How to run python package inside a Docker container?
Hi there, My project directory looks like this the following. As you can see, I have structured the directory myproject to be seen as a Python package. . |-- Dockerfile |-- README.md |-- requirements.txt `-- myproject … More on forums.docker.com
🌐 forums.docker.com
1
0
September 3, 2020
Setting up the docker module in Python
If you're using pycharm or vsc or similar you might need to set the python version to use in the settings. More on reddit.com
🌐 r/docker
4
7
December 16, 2022
Installing a new python package for an existing container
Note: after running the command sudo docker compose run web django-admin startproject [DjangoAppName]. the first time, my directory looks like this: Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 28/05/2024 13:13 [DjangoAppName] d----- 28/05/2024 13:13 data -a---- 28/05/2024 11:46 498 docker-compose.yml -a---- 28/05/2024 11:45 195 Dockerfile -a---- 28/05/2024 13:13 663 manage.py -a---- 28/05/2024 13:13 0 README.md -a---- 28/05/2024 13:34 38 requirements.txt I don't know where the data directory comes from..... Nowhere in my Dockerfile or the docker-compose.yml have a defined the name for this directory. Is this the "image"? If so, how can I rebuild this image after adding the new python package to requirements.txt? More on reddit.com
🌐 r/docker
5
4
May 28, 2024
How to install a python module in a docker container - Stack Overflow
Please, post your docker file. ... Typically you'd whatever dependency to a Pipfile or setup.py in your source tree, using standard Python packaging tools; run pip freeze if needed to regenerate a requirements.txt file; then re-run docker build to get a new image. More on stackoverflow.com
🌐 stackoverflow.com
Top answer
1 of 4
91

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

2 of 4
48

Some of the other answers/comments are suggesting to change your base image but if you want to keep your ubuntu 16.04 you can also simply specify your version of pip/python to use pip3 or pip3.5 like shown below.

FROM ubuntu:16.04

RUN apt-get update && apt-get install -y --no-install-recommends \
    python3.5 \
    python3-pip \
    && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

RUN pip3 install nibabel pydicom matplotlib pillow
RUN pip3 install med2image
🌐
Docker Hub
hub.docker.com › _ › python
python - Official Image | Docker Hub
When using this image pip install will work if a suitable built distribution is available for the Python distribution package being installed. pip install may fail when installing a Python distribution package from a source distribution. This image does not contain the Debian packages required to compile extension modules written in other languages.
🌐
Docker Community
forums.docker.com › general
How to run python package inside a Docker container? - General - Docker Community Forums
September 3, 2020 - Hi there, My project directory looks like this the following. As you can see, I have structured the directory myproject to be seen as a Python package. . |-- Dockerfile |-- README.md |-- requirements.txt `-- myproject |-- __init__.py |-- __main__.py |-- __pycache__ |-- config.py |-- db.py |-- env.py |-- filehandle.py |-- main.py |-- pepkeys |-- test.py `-- util.py On my host system, I can run the package like this without any issue: python -m mypro...
🌐
Reddit
reddit.com › r/docker › setting up the docker module in python
r/docker on Reddit: Setting up the docker module in Python
December 16, 2022 -

Hi. Could anyone help explain to me how to actually setup the docker module in python. I have tried so far:

pip install docker

However, after doing this, I try running the basic script:

import docker
client = docker.from_env()

After doing so, I get the following:

Exception has occurred: ModuleNotFoundError

No module named 'docker'

Could anyone please explain to me when I am getting this message. It's really confusing. Apologies if this is very obvious

🌐
GitHub
github.com › docker › docker-py
GitHub - docker/docker-py: A Python library for the Docker Engine API · GitHub
A Python library for the Docker Engine API. Contribute to docker/docker-py development by creating an account on GitHub.
Starred by 7.2K users
Forked by 1.7K users
Languages   Python
Find elsewhere
🌐
GitHub
github.com › docker-library › python
GitHub - docker-library/python: Docker Official Image packaging for Python · GitHub
This is the Git repo of the Docker "Official Image" for python (not to be confused with any official python image provided by python upstream).
Starred by 2.7K users
Forked by 1.1K users
Languages   Dockerfile 50.3% | Shell 49.7%
🌐
Docker
docker.com › blog › how-to-dockerize-your-python-applications
How to “Dockerize” Your Python Applications | Docker
To develop with Python and Docker, first ensure that Python v3.7.13+ is installed on your machine. Downloadable packages are available at Python.org for all mainstream OSes:
Published   November 6, 2024
🌐
Docker
docker.com › blog › containerized-python-development-part-1
Containerized Python Development - Part 1 | Docker
May 24, 2024 - One way to manage dependencies is by using a package installer such as pip. For this we need to create a requirements.txt file and write the dependencies in it. An example of such a file for our simple server.py is the following: ... We create a dedicated directory for the source code to isolate it from other configuration files. We will see later why we do this. To execute our Python program, all is left to do is to install a Python interpreter and run it.
🌐
PyPI
pypi.org › project › docker-py
docker-py · PyPI
It does everything the docker command does, but from within Python – run containers, manage them, pull/push images, etc. The latest stable version is always available on PyPi. ... Read the full documentation here. The source is available in the docs/ directory. Docker is licensed under the Apache License, Version 2.0. See LICENSE for full license text ... Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
      » pip install docker-py
    
Published   Nov 02, 2016
Version   1.10.6
🌐
Medium
medium.com › @shuklashubh818 › exploring-docker-py-a-python-library-for-docker-automation-8df5bc727fbe
Exploring Docker-Py: A Python Library for Docker Automation | by Shubh Prakash Shukla | Medium
February 15, 2024 - Docker-Py is a Python client library that allows developers to communicate with the Docker Engine’s Remote API programmatically.
🌐
LinkedIn
linkedin.com › pulse › installing-python-modules-docker-container-rajesh-tandukar-9fdvc
Installing python modules in Docker Container
December 18, 2023 - root@43368e3d2a10:/l# pip --version pip 20.3.4 from /usr/lib/python3/dist-packages/pip (python 3.9) ... Now that Pip is installed, you can proceed to install the desired Python module. In this example, we'll install the 'mysql-connector-python' module: root@43368e3d2a10:/# pip install mysql-connector-python · This command fetches and installs the mysql-connector-python module along with any dependencies. Once the installation is complete, you can use the module in your Python application within the Docker container.
🌐
Python⇒Speed
pythonspeed.com › docker
Articles: Production-ready Docker packaging for Python developers
July 8, 2022 - The uv package manager can also install Python for you. Should you use this feature in production? Installing system packages in Docker with minimal bloat Learn how to minimize your Docker image size while installing or updating system packages on on Debian, Ubuntu, and RHEL.
🌐
Reddit
reddit.com › r/docker › installing a new python package for an existing container
r/docker on Reddit: Installing a new python package for an existing container
May 28, 2024 -

I am new to docker and playing around to replace my python virtual environment for my r/django development.

I have followed this guide to both set up the container and a django project.

That guide sets up a Dockerfile which installs the python packages defined in requirements.txt.
Suppose now after doing some development on the django project that I find I need a new python package. I can install it by running:

docker exec -it 1c7cfa468346 pip install [packageName]

but I found that after stopping and restarting the container, the package is gone. So it appears I need to build a new image entirely (I don't fully grasp the vocabulary yet....). But it appears that images are immutable, so I CAN'T change the existing image. So I have to rebuild the whole thing entirely? Following the guide that would mean to run:

sudo docker compose run web django-admin startproject [DjangoAppName] .

again. BUT that would overwrite the already existing app, no? I don't want to startproject again...

What is the correct way to add a new python package to my docker development environment without losing existing files?

🌐
Bhaskarvk
bhaskarvk.github.io › docker
Wraps Docker Python SDK • docker
The docker R package provides access to the docker API to programmatically control a docker engine from R. The docker engine could be running either locally or remotely. The docker package uses the reticulate R package to invoke the Python SDK for docker. Using the Python SDK allows the docker ...
🌐
Django Stars
djangostars.com › home › docker tutorial: using docker with python
Python Docker Tutorial: How to Use Docker with Python | Django Stars
September 11, 2025 - Also, depending on your stack, you can manage several programming language versions in containers of Docker: Python 3.9 and Python 3.7 for example. You can deliver your application in one piece. Most programming languages, frameworks and all operating systems have their own packaging managers.
🌐
Visual Studio Code
code.visualstudio.com › docs › containers › quickstart-python
Python in a container
November 3, 2021 - According to official Flask Documentation, users generally create a Flask instance in the main module or in the __init__.py file of their package in this manner: from flask import Flask app = Flask(__name__) # Flask instance named app · To accomplish this binding, the final line in the Dockerfile says:
🌐
PyPI
pypi.org › project › python-docker
python-docker · PyPI
Download the file for your platform. If you're not sure which to choose, learn more about installing packages. ... Details for the file python-docker-0.2.0.tar.gz.
      » pip install python-docker
    
Published   Aug 02, 2022
Version   0.2.0