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
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
🌐
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
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
How to install a python package with all the dependencies into a Docker image? - Stack Overflow
When I run the image and try to import the package with import folium it doesn't find the folium package: ImportErrorTraceback (most recent call last) in () ----> 1 import folium ImportError: No module named 'folium' ... To install python pacakges into Docker ... More on stackoverflow.com
🌐 stackoverflow.com
Making library pip-installable vs. creating Docker image with the library inside
One thing about pip is it lets you easily choose which version of a module you want, so it helps you in a scenario where you want to easily upgrade ("pip --upgrade mymodule"), or choose a specific version ("pip install mymodule==2.3" ). More on reddit.com
🌐 r/Python
26
17
June 24, 2022
🌐
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
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 ...
🌐
GitHub
github.com › docker › docker-py
GitHub - docker/docker-py: A Python library for the Docker Engine API · GitHub
It lets you do anything the docker command does, but from within Python apps – run containers, manage containers, manage Swarms, etc.
Starred by 7.2K users
Forked by 1.7K users
Languages   Python
🌐
Docker
docker-py.readthedocs.io
Docker SDK for Python — Docker SDK for Python 7.1.0 documentation
It lets you do anything the docker command does, but from within Python apps – run containers, manage containers, manage Swarms, etc.
🌐
Docker Community
forums.docker.com › general
How to run python package inside a Docker container? - General - Docker Community Forums
September 3, 2020 - . |-- Dockerfile |-- README.md ... `-- util.py On my host system, I can run the package like this without any issue: python -m mypro......
Find elsewhere
🌐
Docker
docker.com › blog › how-to-dockerize-your-python-applications
How to “Dockerize” Your Python Applications | Docker
Patrick’s example leveraged two popular libraries—requests and BeautifulSoup—to create his app’s crawling capabilities on IMDB’s database. However, these two libraries have widespread appeal. Screenshot courtesy of python-requests.org.
Published   November 6, 2024
🌐
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%
🌐
GeeksforGeeks
geeksforgeeks.org › python › setting-up-docker-for-python-projects-a-step-by-step-guide
Setting Up Docker for Python Projects: A Step-by-Step Guide - GeeksforGeeks
July 23, 2025 - For Python projects, a Dockerfile typically defines the base Python image, installs dependencies and sets up the application environment. # Use an official Python runtime as a parent image FROM python:3.9-slim # Set the working directory in the container WORKDIR /usr/src/app # Copy the current directory contents into the container at /usr/src/app COPY . . # Install any needed packages specified in requirements.txt RUN pip install --no-cache-dir -r requirements.txt # Make port 80 available to the world outside this container (Optional, only for web apps) EXPOSE 80 # Define environment variable (optional) ENV NAME World # Run app.py when the container launches CMD ["python", "./app.py"]
🌐
Bhaskarvk
bhaskarvk.github.io › docker
Wraps Docker Python SDK • docker
mkvirtualenv --python=/usr/bin/python3 docker workon docker pip install docker # Test the SDK againsts a locally running docker # You should have a locally running docker for this.
🌐
Docker
docs.docker.com › guides › python › containerize your app
Containerize your app | Docker Docs
What is the command to run your app? python3 -m uvicorn app:app --host=0.0.0.0 --port=8000 · Create a file named .gitignore with the following contents. ... # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] *$py.class # C extensions *.so # Distribution / packaging .Python build/ develop-eggs/ dist/ downloads/ eggs/ .eggs/ lib/ lib64/ parts/ sdist/ var/ wheels/ share/python-wheels/ *.egg-info/ .installed.cfg *.egg MANIFEST # Unit test / coverage reports htmlcov/ .tox/ .nox/ .coverage .coverage.* .cache nosetests.xml coverage.xml *.cover *.py,cover .hypothesis/ .pytest_cache/ cover/ # PEP 582; used by e.g.
🌐
Real Python
realpython.com › python-versions-docker
Run Python Versions in Docker: How to Try the Latest Python Release – Real Python
May 12, 2023 - You can also request specific versions like python:3.6.3 or python:3.8.0b4, the fourth beta version of Python 3.8. You can even run PyPy using a tag like pypy:latest. A Docker container is an isolated environment. Therefore, you usually don’t need to add a virtual environment inside the container. Instead, you can run pip directly to install the necessary packages. To modify the container to include the extra packages, you use a Dockerfile. The following example adds parse and realpython-reader to a Python 3.7.5 container:
🌐
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.
🌐
KDnuggets
kdnuggets.com › containerize-python-apps-with-docker-in-5-easy-steps
Containerize Python Apps with Docker in 5 Easy Steps - KDnuggets
Next, code a Python application which we can containerize using Docker. Here we’ll containerize a simple command-line TO-DO list app. The code for this app is on GitHub: todo.py file. You can containerize any Python app of your choice or follow along with the example we use here.
🌐
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.