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
The latest stable version is available on PyPI. Install with pip:
      » pip install docker
    
Published   May 23, 2024
Version   7.1.0
Discussions

Dockerfile Error RUN pip install -r requirements.txt
Hi , i am pretty new for docker, I just try to write a docker file for a python flask web app. but it gives an error: ERROR: could not find a version that satisfies the requirement python==3.10.5(from versions:none) ERROR: no matching distribution found for python==3.10.5 Hier is my ... More on forums.docker.com
🌐 forums.docker.com
5
0
August 24, 2022
python - Setting a non-root user in a Dockerfile and installing pip packages - Stack Overflow
I am not sure since I am copying ... using pip’s --user option, why the files are being installed at .local directory such as is highlighted here ... ... Is better open a new question. The original problem is totally different from this new one. The first was a run as configuration on Dockerfile, this one ... More on stackoverflow.com
🌐 stackoverflow.com
RUN pip install --upgrade pip and RUN pip install -r requirements.txt not working
so here’s the story, this is my first time using docker because i was told it is really easy to deploy script, model and many more. Today i try to build my first docker image for my python script using flask. i try to build it and it work on the first couple of iteration and fail like 10 ... More on forums.docker.com
🌐 forums.docker.com
8
0
December 15, 2023
Making library pip-installable vs. creating Docker image with the library inside
To me it makes more sense in your case that you go with Docker-based delivery. It looks like this software is proprietary, and you don't want to distribute publicly. Also, you want to go with SageMaker. Therefore you might want to add Code pipeline to integration test if it's working before building image, then to ECR. You'll have more observability built-in to it by default in AWS. On the otherhand, setup private Pypi, and setup new distribution channel - is fun to do, but don't add much value, in-term of project delivery for your case. It just adds another layer of complexity - to me. My two cents. More on reddit.com
🌐 r/Python
26
17
June 24, 2022
🌐
Docker Community
forums.docker.com › general
Dockerfile pip install from local directory - General - Docker Community Forums
April 20, 2021 - The server I am using is not connected to the internet. one of the Dockerfile commands is pip install: RUN pip install -r requirements.txt --no-index --find-links …/py-pks/ and it fails. any suggestions?
Top answer
1 of 2
5

If you want to install in one image and run in the other, it's often easier to install into a virtualenv and copy the virtualenv, because then you get all the files, e.g. data files and executables and such like. Copying site-packages doesn't get you that.

You can then chown the virtualenv to your new user as you copy it.

Something like

FROM python:3.9-slim AS compile-image
RUN apt-get update
RUN apt-get install -y --no-install-recommends build-essential gcc

RUN python -m venv /opt/venv
# Make sure we use the virtualenv:
ENV PATH="/opt/venv/bin:$PATH"

COPY requirements.txt .
RUN pip install -r requirements.txt

COPY setup.py .
COPY myapp/ .
RUN pip install .

FROM python:3.9-slim AS build-image
RUN useradd --create-home appuser
USER appuser
COPY --from=compile-image --chown=appuser /opt/venv /opt/venv

# Make sure we use the virtualenv:
ENV PATH="/opt/venv/bin:$PATH"
CMD ['myapp']

Note the use of --chown in COPY to ensure it changes ownership as you copy it. (Based on https://pythonspeed.com/articles/activate-virtualenv-dockerfile/, https://pythonspeed.com/articles/multi-stage-docker-python/ https://pythonspeed.com/articles/root-capabilities-docker-security/)

2 of 2
-1

You can add USER some_user to your Dockerfile o run with a specific user

cat Dockerfile
...    
USER site
RUN echo 'RUNNING as $USER'

I ran the image

Or at execution

docker run -it --user site image_name bash

I don't know if there are limitations in the USER instruction só you could try add RUN root in the intermediate container and then RUN site in the final container.

, maybe the user you want is www-data

🌐
Medium
luis-sena.medium.com › creating-the-perfect-python-dockerfile-51bdec41f1c8
Creating the Perfect Python Dockerfile | by Luis Sena | Medium
September 20, 2021 - RUN pip3 install --no-cache-dir -r requirements.txt FROM ubuntu:20.04 AS runner-image RUN apt-get update && apt-get install --no-install-recommends -y python3.9 python3-venv && \ apt-get clean && rm -rf /var/lib/apt/lists/* COPY --from=builder-image /opt/venv /opt/venv # activate virtual environment ENV VIRTUAL_ENV=/opt/venv ENV PATH="/opt/venv/bin:$PATH" CMD ["python"]
Find elsewhere
🌐
Docker Community
forums.docker.com › general
RUN pip install --upgrade pip and RUN pip install -r requirements.txt not working - General - Docker Community Forums
December 15, 2023 - so here’s the story, this is my first time using docker because i was told it is really easy to deploy script, model and many more. Today i try to build my first docker image for my python script using flask. i try to build it and it work on the first couple of iteration and fail like 10 times on the RUN section this is the log [+] Building 9.7s (9/10) docker:default => [internal] load build definition from Dockerf...
🌐
TutorialsPoint
tutorialspoint.com › article › how-does-one-install-pip-in-a-docker-container-using-a-dockerfile
How Does One Install Pip in a Docker Container using a Dockerfile?
July 10, 2023 - After adding this the complete Dockerfile will look like this. 1. FROM ubuntu:latest 2. RUN apt-get update 3. RUN apt-get install -y python3-pip
🌐
Reddit
reddit.com › r/docker › i can't run pip for the docker build
r/docker on Reddit: I can't run pip for the docker build
January 5, 2022 -

Hello,

I try to run my dockerfile on a RaspberryPi 4 with raspian-buster and I get this error:

Sending build context to Docker daemon  14.56MB
Step 1/9 : FROM python:slim
 ---> 6beb215998ec
Step 2/9 : WORKDIR /bot
 ---> Using cache
 ---> fd86572b0023
Step 3/9 : COPY libs-py.txt .
 ---> Using cache
 ---> f0f89e156d7a
Step 4/9 : RUN pip install --upgrade pip
 ---> Running in f8b9b524a31d
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/local/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/usr/local/lib/python3.10/site-packages/pip/__main__.py", line 29, in <module>
    from pip._internal.cli.main import main as _main
  File "/usr/local/lib/python3.10/site-packages/pip/_internal/__init__.py", line 4, in <module>
    from pip._internal.utils import _log
  File "/usr/local/lib/python3.10/site-packages/pip/_internal/utils/_log.py", line 8, in <module>
    import logging
  File "/usr/local/lib/python3.10/logging/__init__.py", line 57, in <module>
    _startTime = time.time()
PermissionError: [Errno 1] Operation not permitted
The command '/bin/sh -c pip install --upgrade pip' returned a non-zero code: 1

inxi:
Argument "Raspberry Pi 4 Model B Rev 1.2" isn't numeric in sprintf at /usr/bin/inxi line 6969.
CPU: Quad Core ARMv7 v7l (-MCP-) speed/min/max: 1500/600/1500 MHz Kernel: 5.10.63-v7l+ armv7l Up: 14d 8h 00m 
Mem: 528.8/3914.7 MiB (13.5%) Storage: 59.48 GiB (49.3% used) Procs: 176 Shell: bash 5.0.3 inxi: 3.0.32

On my Linux mashine with Linux Mint does it run without any problems. Has anybody a idea how i can solve this problem?

🌐
Reddit
reddit.com › r/docker › how to manually install python package in dockerfile?
r/docker on Reddit: How to Manually Install Python Package in DockerFile?
October 26, 2023 -

I have been trying to use the module "bonsai" in my AWS Lambda function. Unfortunately, the module does not work in a Linux Environment via pip installation, therefore I have to manually install it. With that being said I created a Docker Container that will include all the needed modules for my project.

MyDESKTOP:~/lambda_container$ ls

Dockerfile bonsai lambda_function.py requirements.txt

Currently, in my project directory, the "bonsai" folder includes all the code needed for that module:

MyDESKTOP:~/lambda_container/bonsai$ ls
init.py,       active_directory,  ldapclient.py,      ldapreference.py,   pool.py,   utils.py, pycache,       asyncio,            ldapconnection.py,  ldapurl.py,        py.typed,  _bonsai.cpython-311-x86_64-linux-gnu.so,  errors.py,      ldapdn.py,          ldapvaluelist.py,  tornado _bonsai.py,         geventldapentry.py,      ldif.py,           trio

I am calling the module in my lambda_function.py file using import bonsai. When I start up my container to test I get this error: {"errorMessage": "Unable to import module 'lambda_function': No module named 'bonsai'"

Here is my DockerFile:

FROM public.ecr.aws/lambda/python:3.11
COPY requirements.txt ${LAMBDA_TASK_ROOT} COPY bonsai ${LAMBDA_TASK_ROOT}
RUN pip install setuptools RUN pip install boto3 RUN pip install cryptography RUN pip install ldap3
COPY lambda_function.py ${LAMBDA_TASK_ROOT}
CMD [ "lambda_function.handler" ]

In conclusion, How do you properly manually install a Python module in Docker?

Thank Y'all in advance!

🌐
TestDriven.io
testdriven.io › blog › docker-best-practices
Docker Best Practices for Python Developers | TestDriven.io
February 12, 2024 - $ docker images REPOSITORY TAG ... . . # buildkit 6.71kB buildkit.dockerfile.v0 <missing> 58 seconds ago RUN /bin/sh -c pip install -r requirements.t…...
🌐
NVIDIA Developer Forums
forums.developer.nvidia.com › robotics & edge computing › jetson systems › jetson agx orin
Pip install in a docker Container - Jetson AGX Orin - NVIDIA Developer Forums
July 19, 2025 - Then you could install it with pip. Now, pip is immediately available in a new container, but it can no longer find the sources. Example: #pip install numpy results in: Looking in indexes: ...
🌐
APXML
apxml.com › courses › docker-for-ml-projects › chapter-2-building-ml-dockerfiles › managing-pip-dependencies
Managing Python Dependencies with pip in Docker
# Install dependencies RUN pip install -r requirements.txt # Now copy the rest of the application code COPY . . # Define how to run the application (example) # CMD ["python", "train.py"] Docker builds images in layers. Each instruction in the Dockerfile (like COPY, RUN, WORKDIR) creates a new layer.
Top answer
1 of 3
8

I'm not sure why people have downvoted you, other than there is a well documented and easy way to go about this.

Docker images are layered, and you can build all your essential-for-all-images packages into one new docker image of our own making, and then extend from that.

For example, when you write your docker file, your first line will be

FROM someothercontainer

What you can do is then create a new image to use in your "FROM" in all future containers that you write, that will have this built in. The trivial example is to build a container something like

FROM ubuntu
apt-get install python

And then build this image as my-image-with-python-installed or whatever works.

Then, in the later container where you want to install your own unique-to-each-container stuff, you write your Dockerfile as such:

FROM my-image-with-python-installed
ADD my-local-package
...

The documentation on Docker's page is a bit low level as they want you to build the smallest base images you can, but you can find it here: https://docs.docker.com/develop/develop-images/baseimages/

Your documentation for the FROM command comes in here https://docs.docker.com/engine/reference/builder/#from but you'll need to build your base image first, but there is loads of articles out there on how to do that.

Edit:

It's very common to have a base image stored in a repository, especially if the contents don't change. This way you can access it from different build machines and don't have to rebuild it often if at all, setting up small local repositories is very easy, and many artifact management tools have this feature built in as well. If, however, you're building images on the fly but they all still have a common base it isn't necessary to store that in a repository - you just build the base image first and then as its local to that build machine all other images built on that machine will be able to access it.

2 of 3
3

A nice pattern for a Dockerfile is something like this:

FROM ubuntu:18.04

RUN apt-get update && apt-get install -y python3 python3-pip

COPY requirements.txt /tmp/base_requirements.txt
RUN python3 -m pip install -r /tmp/base_requirements.txt

COPY my_package_name/ /usr/lib/python3.6/my_package_name/

This way the package is accessible in the path and can be imported.

🌐
Docker Community
forums.docker.com › general
Pip installing the correct Python packages during cross-compiling - General - Docker Community Forums
July 12, 2023 - I’m trying to build a multi-platform image using multi-stage cross-compiling, but I’m a bit lost as to how and where I should be doing pip3 install in order to get the correct python packages for either amd64 or arm. Building via the below Dockerfile results in containers that run correctly ...
🌐
Docker Community
forums.docker.com › general
Install pip packages in the image, on the host, or both? - General - Docker Community Forums
October 10, 2021 - For what I know, the common practice of Python development with Docker is to have: # Install pip requirements COPY requirements.txt . RUN python -m pip install -r requirements.txt In my Dockerfile . This way the packages are installed while ...
🌐
Medium
medium.com › @gauravkamble9112 › installing-python-libraries-inside-docker-with-pip3-d5348afb7a80
Installing Python Libraries Inside Docker with pip3 | by Gauravkamble | Medium
July 2, 2023 - These containers can be used to isolate and run applications, making it easier to manage dependencies and ensure consistent environments. In this tutorial, we will guide you through the process of installing Docker, pulling a CentOS Docker image, and installing Python libraries inside the Docker container using pip3.
🌐
Quora
quora.com › Is-it-better-to-pip-install-packages-in-the-Dockerfile-directly-or-using-a-requirements-txt-when-not-wanting-to-rebuild-a-container-unnecessarily
Is it better to pip install packages in the Dockerfile directly or using a requirements.txt, when not wanting to rebuild a container unnecessarily? - Quora
Answer (1 of 2): I did a little research on this and found this page: Efficient management Python projects dependencies with Docker. I like the “Combo” solution proposed by the author. Here's the summary: 1. If you use requirements.txt, then everytime you rebuild your container, it will ...
🌐
Docker
docker-py.readthedocs.io
Docker SDK for Python — Docker SDK for Python 7.1.0 documentation
The latest stable version is available on PyPI. Either add docker to your requirements.txt file or install with pip: