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
Top answer
1 of 7
85

While T. Arboreus's answer might fix the issues with resolving 'archive.ubuntu.com', I think the last error you're getting says that it doesn't know about the packages php5-mcrypt and python-pip. Nevertheless, the reduced Dockerfile of you with just these two packages worked for me (using Debian 8.4 and Docker 1.11.0), but I'm not quite sure if that could be the case because my host system is different than yours.

FROM ubuntu:14.04

# Install dependencies
RUN apt-get update && apt-get install -y \
    php5-mcrypt \
    python-pip

However, according to this answer you should think about installing the python3-pip package instead of the python-pip package when using Python 3.x.

Furthermore, to make the php5-mcrypt package installation working, you might want to add the universe repository like it's shown right here. I had trouble with the add-apt-repository command missing in the Ubuntu Docker image so I installed the package software-properties-common at first to make the command available.

Splitting up the statements and putting apt-get update and apt-get install into one RUN command is also recommended here.

Oh and by the way, you actually don't need the -y flag at apt-get update because there is nothing that has to be confirmed automatically.

Finally:

FROM ubuntu:14.04

# Install dependencies
RUN apt-get update && apt-get install -y \
    software-properties-common
RUN add-apt-repository universe
RUN apt-get update && apt-get install -y \
    apache2 \
    curl \
    git \
    libapache2-mod-php5 \
    php5 \
    php5-mcrypt \
    php5-mysql \
    python3.4 \
    python3-pip

Remark: The used versions (e.g. of Ubuntu) might be outdated in the future.

2 of 7
19

This command worked fine for me:

RUN apt-get -y install python3-pip
Discussions

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
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
Pip installing the correct Python packages during cross-compiling
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 ... More on forums.docker.com
🌐 forums.docker.com
5
0
July 12, 2023
Dockerfile pip install from local directory
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? More on forums.docker.com
🌐 forums.docker.com
4
0
April 20, 2021
🌐
PyPI
pypi.org › project › docker
docker · PyPI
A Python library for the Docker Engine API. It lets you do anything the docker command does, but from within Python apps – run containers, manage containers, manage Swarms, etc. The latest stable version is available on PyPI. Install with pip:
      » pip install docker
    
Published   May 23, 2024
Version   7.1.0
🌐
Readthedocs
docker-sean.readthedocs.io › en › latest › reference › pip_install.html
pip install — docker 6.1.0.dev0 documentation
Each line of the requirements file ... to pip install, the following forms are supported: <requirement specifier> <archive url/path> [-e] <local project path> [-e] <vcs project url> Since version 6.0, pip also supports markers using the “; ” separator. Examples...
🌐
TutorialsPoint
tutorialspoint.com › 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
🌐
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 - Step 3: Install Python Libraries with pip3 Now that Python and pip3 are installed inside the Docker container, you can easily install Python libraries using pip3. Here’s an example of installing numpy and scikit-learn:
🌐
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

Find elsewhere
🌐
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:
🌐
Quora
quora.com › How-does-one-install-pip-in-a-Docker-container-using-a-Dockerfile
How does one install pip in a Docker container using a Dockerfile? - Quora
Answer (1 of 7): Hi, You have missed one detail, from which image are you trying build your own Docker. From there error message, it looks like you are extending a windows image. [code]E: Unable to locate package python-pip [/code]So I would suggest you 1. Download https://bootstrap.pypa.io/g...
🌐
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
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?
🌐
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...
🌐
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. pip install docker-py · Read the full documentation here.
      » pip install docker-py
    
Published   Nov 02, 2016
Version   1.10.6
🌐
Docker
docker.com › blog › containerized-python-development-part-1
Containerized Python Development - Part 1 | Docker
May 24, 2024 - /root/.local) RUN pip install --user -r requirements.txt # second unnamed stage FROM python:3.8-slim WORKDIR /code # copy only the dependencies installation from the 1st stage image COPY --from=builder /root/.local /root/.local COPY ./src .
🌐
LinkedIn
linkedin.com › pulse › installing-python-modules-docker-container-rajesh-tandukar-9fdvc
Installing python modules in Docker Container
December 18, 2023 - 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.
🌐
Medium
luis-sena.medium.com › creating-the-perfect-python-dockerfile-51bdec41f1c8
Creating the Perfect Python Dockerfile | by Luis Sena | Medium
September 20, 2021 - Using a remote image as a cache is especially useful for your CI build where a cache folder might not be available and you would have cold builds for every pipeline. ... # syntax=docker/dockerfile:1.2 FROM ubuntu:20.04 RUN apt-get update && apt-get install -y python3.9 python3.9-dev COPY requirements.txt .RUN --mount=type=cache,mode=0755,target=/root/.cache pip install -r requirements.txtCOPY .
🌐
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 ...
🌐
Whatap
docs.whatap.io › kubernetes monitoring › installation › application agent installation › installing docker python
Installing Docker Python | WhaTap Docs
March 6, 2026 - See the following definition example of the following Dockerfile. This is a complete example to run the entrypoint.sh script. FROM python:3.10 WORKDIR /app ADD . /app/ RUN pip3 install --upgrade whatap-python # Assign a role to run the entrypoint.sh script in the container.