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
🌐
GitHub
github.com › mozilla › generic-python-docker
GitHub - mozilla/generic-python-docker: An example repo for a generic, dockerized Python project
February 4, 2021 - You can simply remove it and use make run COMMAND="python-application hello-world" (replaced with what your app is called) instead · application.py (including the correponding runner in __main__.py, as well as the test in tests/test_application.py, and that test file's name) APP_NAME in Dockerfile (line 4) setup.py file (Start at line 17) The directory for linting in .circleci/config.yml (line 60) This deploys using Dockerhub and CircleCI.
Starred by 17 users
Forked by 22 users
Languages   Python 39.9% | Makefile 24.9% | Dockerfile 19.1% | Shell 16.1% | Python 39.9% | Makefile 24.9% | Dockerfile 19.1% | Shell 16.1%
🌐
Reddit
reddit.com › r/docker › generic python docker container
r/docker on Reddit: Generic Python Docker Container
December 28, 2023 -

I thought today, about using Docker to setup virtual environments instead of using Pipenv because I'd like to learn Docker a bit. I watched some Python videos on it and all of these were setting up fastapi's which I don't need so I wanted to make a more generic dockerfile and yml file that I can come back to over and over again.

I created a dockerfile like below:

FROM python:3.12-slim

WORKDIR /code

COPY ./requirements.txt ./ RUN pip install --no-cache-dir -r requirements.txt

COPY ./src ./src

I created a docker-compose.yml file like below:

services:
  app: 
      build: . 
      container_name: py_env 
      volumes: 
          - .:/code

I ran the command docker compose up --build -d and had no errors, it said Container py_env Started but when doing docker ps there is no container and when using dev containers in VS code to try to attach to a running container it says there are no running containers to attach to.

What am I missing?

🌐
GitHub
github.com › mozilla › generic-python-docker › blob › master › Dockerfile
generic-python-docker/Dockerfile at master · mozilla/generic-python-docker
February 4, 2021 - An example repo for a generic, dockerized Python project - mozilla/generic-python-docker
Author   mozilla
🌐
GitHub
gist.github.com › bbornhau › a5d9717ce6216b981fd12921fe87652d
multicontainter generic python Dockerfile · GitHub
multicontainter generic python Dockerfile. GitHub Gist: instantly share code, notes, and snippets.
🌐
Docker Hub
hub.docker.com › _ › python
python - Official Image | Docker Hub
FROM python:2 WORKDIR /usr/src/app COPY requirements.txt ./ RUN pip install --no-cache-dir -r requirements.txt COPY . . CMD [ "python", "./your-daemon-or-script.py" ] Copy ... For many simple, single file projects, you may find it inconvenient to write a complete Dockerfile.
🌐
Visual Studio Code
code.visualstudio.com › docs › containers › quickstart-python
Python in a container
November 3, 2021 - Create a Dockerfile file describing a simple Python container.
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
Find elsewhere
🌐
Medium
luis-sena.medium.com › creating-the-perfect-python-dockerfile-51bdec41f1c8
Creating the Perfect Python Dockerfile | by Luis Sena | Medium
September 20, 2021 - You can use python instead of python3 or python3.9 command(Yes, there are other ways) You can have a single Dockerfile to run tests and deploy.
🌐
Docker
docker.com › blog › containerized-python-development-part-1
Containerized Python Development - Part 1 | Docker
May 24, 2024 - The way to get our Python code running in a container is to pack it as a Docker image and then run a container based on it. The steps are sketched below. To generate a Docker image we need to create a Dockerfile which contains instructions needed to build the image.
🌐
Python⇒Speed
pythonspeed.com › articles › base-image-python-docker-images
The best Docker base image for your Python application (February 2026)
February 4, 2026 - The uv package installation and venv managed tool also supports downloading and installing a copy of Python when creating a virtualenv. If you do this, you are not relying on a Docker image’s Python; you can use any base image you want. Rather, the Python install comes from the Python Standalone Build project, so it will get installed via running uv in the Dockerfile.
🌐
dockerlabs
dockerlabs.collabnix.com › beginners › dockerfile › lab_dockerfile_python.html
Writing Dockerfile with Hello Python Script Added | dockerlabs
So, default application mode of container would be python and if no other filename is provided as argument to it then it will execute hello.py placed in its /home directory. Benefit of this is that user can choose some other file to run with the same application at runtime, that is, while launching the container. So, our overall Dockerfile currently looks like this:
🌐
Medium
medium.com › @mjrod › deploying-a-python-application-to-docker-4478787c2add
Creating a Custom Python Application Image and Deploying to Docker | by Michael Rodgers | Medium
May 19, 2022 - CMD [ "python3", "-m" , "flask", "run", "--host=0.0.0.0"] supply the command to run that will launch the server, and make it accessible outside of the container ... # syntax=docker/dockerfile:1 # base python image for custom image FROM python:3.9.13-slim-buster # create working directory and install pip dependencies WORKDIR /hello-py COPY requirements.txt requirements.txt RUN pip3 install -r requirements.txt # copy python project files from local to /hello-py image working directory COPY .
🌐
Reddit
reddit.com › r/python › the perfect python dockerfile - better performance and security
r/Python on Reddit: The Perfect Python Dockerfile - better performance and security
May 19, 2021 -

Having a reliable Dockerfile as your base can save you hours of headaches and bigger problems down the road.

This article shares a Dockerfile base that has been battle-tested through many different projects.

https://luis-sena.medium.com/creating-the-perfect-python-dockerfile-51bdec41f1c8

This can also serve as a succinct tutorial of the different features/commands used to improve the final image.

Nothing is perfect I know! Please feel free to provide any feedback and we can iterate on the shared Dockerfile if needed.

🌐
Docker
docker.com › blog › how-to-dockerize-your-python-applications
How to “Dockerize” Your Python Applications | Docker
FROM python:3.9 # Or any preferred Python version. ADD main.py . RUN pip install requests beautifulsoup4 python-dotenv CMD [“python”, “./main.py”] # Or enter the name of your unique directory and parameter set. This Dockerfileis fairly basic, which is perfect for this application.
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%
🌐
Hasura
hasura.io › blog › how-to-write-dockerfiles-for-python-web-apps-6d173842ae1d
How to Write Dockerfiles for Python Web Apps
This post is filled with examples ranging from a simple Dockerfile to multistage production builds for Python apps.
🌐
DevOps.dev
blog.devops.dev › dockerfile-for-a-python-application-d88d6bf14a13
Dockerfile for a Python application | by Meghasharmaa | DevOps.dev
December 16, 2025 - Let’s create a simple Dockerfile for a Python application. This example assumes you have a Python script named app.py and a…
🌐
Runnable
runnable.com › docker guides › python › dockerize your python application
Dockerize your Python Application | Runnable Docker Guides
July 19, 2016 - Create a new text file in my_new_docker_build called Dockerfile (note no extension; on Windows, you may need to save the file as “All types” and put the filename in quotes to avoid automatically appending an extension); use whatever text file editor you already know (you might use Sublime, Notepad++, emacs, nano, or even vi). In our example, we use the basic Python 3 image as our launching point.
🌐
PyPI
pypi.org › project › dockerfile
dockerfile · PyPI
Python :: Implementation :: PyPy · Report project as malware · The goal of this repository is to provide a wrapper around docker/docker's parser for dockerfiles. This project uses setuptools-golang when built from source. To build from source you'll need a go compiler.
      » pip install dockerfile
    
Published   Jan 04, 2025
Version   3.4.0