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
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
🌐
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

Discussions

Install python package in docker file - Stack Overflow
In my docker file, I want to install med2image python package (https://github.com/FNNDSC/med2image). I use the following code: 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 ... More on stackoverflow.com
🌐 stackoverflow.com
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
Any docker image with python and pip preinstalled?
Is there any docker image available which comes with pre installed python and pip. I am quite new to docker. More on forums.docker.com
🌐 forums.docker.com
6
0
December 28, 2017
python - Install pip in docker - Stack Overflow
I'm not able to install pip in Docker. Here's my Dockerfile: FROM ubuntu:14.04 # Install dependencies RUN apt-get update -y RUN apt-get install -y git curl apache2 php5 libapache2-mod-php5 php5-... 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
🌐
TestDriven.io
testdriven.io › blog › docker-best-practices
Docker Best Practices for Python Developers | TestDriven.io
February 12, 2024 - RUN pip install -r requirements.txt # final stage FROM python:3.12.2-slim COPY --from=builder /opt/venv /opt/venv WORKDIR /app ENV PATH="/opt/venv/bin:$PATH" It's a good idea to limit the memory usage of your Docker containers, especially if ...
🌐
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:
🌐
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?
Find elsewhere
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
🌐
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...
🌐
Readthedocs
docker-sean.readthedocs.io › en › latest › reference › pip_install.html
pip install — docker 6.1.0.dev0 documentation
For non-editable installs, the project is built locally in a temp dir and then installed normally. The “project name” component of the url suffix “egg=<project name>-<version>” is used by pip in its dependency logic to identify the project prior to pip downloading and analyzing the metadata.
🌐
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 ...
🌐
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...
🌐
Finxter
blog.finxter.com › home › learn python blog › how to install docker in python?
How to Install docker in Python? - Be on the Right Side of Change
September 28, 2021 - This installs docker for your default Python installation. The previous command may not work if you have both Python versions 2 and 3 on your computer. In this case, try "pip3 install docker" or “python -m pip install docker“.
🌐
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
🌐
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 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 ...
🌐
Lei Mao's Log Book
leimao.github.io › blog › Docker-Python-Setup
Python Setup In Docker - Lei Mao's Log Book
November 18, 2024 - This is the old way I used to set up Python in Docker containers for Ubuntu 22.04. I could install Python packages system-wide via pip freely.
🌐
Ansible
forum.ansible.com › archives › ansible project
pip docker install - Ansible Project - Ansible
October 2, 2019 - Hi all. I have the following… name: Install pip package: name: python-pip update_cache: true state: present name: Install docker libs with pip pip: name: docker state: present executable: pip When I run…