Keywords: Docker | Python Package Installation | Container Deployment
Abstract: This article provides an in-depth exploration of best practices for installing Python packages in Docker containers. Through analysis of common installation error cases, it explains Python version compatibility issues and their solutions in detail. The focus is on the advantages of using official Python base images and standardized dependency management via requirements.txt files. Alternative approaches for maintaining Ubuntu base images are also provided, with comparisons of different methods' pros and cons. Complete Dockerfile templates and build verification steps are included to help developers create stable and reliable Python application containers.
Problem Background and Analysis
When installing Python packages in Docker environments, developers often encounter version compatibility issues. The original Dockerfile uses Ubuntu 16.04 as the base image and installs Python 3.5, but encounters a "Sorry, only Python 3.5+ is supported" error when executing pip install med2image. This seemingly contradictory issue actually stems from the default behavior of the pip command.
Root Cause Analysis
In Ubuntu systems, the pip command typically points to Python 2.x, while pip3 points to Python 3.x. When using pip install, the system actually executes the installation in a Python 2 environment, which conflicts with med2image's requirement for Python 3.5+. The "Python 3.5+" requirement in the error message conflicts with the actual Python 2 execution environment.
Recommended Solution: Using Official Python Images
The most elegant solution is to use Docker's official Python base images. This approach offers several advantages:
FROM python:3
RUN pip install --no-cache-dir --upgrade pip && \
pip install --no-cache-dir nibabel pydicom matplotlib pillow med2image
Using the python:3 image ensures:
- Pre-installed latest stable Python 3 version
- pip command automatically points to the correct Python 3 environment
- Reduced complexity of system dependencies
- Adherence to Docker best practices
Standardized Dependency Management
For production environments, using a requirements.txt file for declarative dependency management is recommended:
# requirements.txt content
matplotlib
med2image
nibabel
pillow
pydicom
Corresponding Dockerfile configuration:
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"]
Advantages of this approach include:
- Explicit declaration of dependencies for team collaboration
- Support for dependency version locking
- More predictable and repeatable build processes
- Easier dependency auditing and updates
Alternative Approach: Maintaining Ubuntu Base Image
If the project requires maintaining the Ubuntu 16.04 base image, compatibility issues can be resolved by explicitly specifying the pip version:
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
Using the pip3 command ensures execution in the Python 3 environment, avoiding version confusion issues.
Build Verification and Testing
After completing the Dockerfile, images can be built and verified using:
docker build --pull -t test . && docker run --rm -it test
Verification steps include:
- Confirming base operating system information
- Checking Python version compliance
- Verifying all dependency packages are correctly installed
- Testing application functionality
Best Practices Summary
When installing Python packages in Docker, priority should be given to: using official Python base images, managing dependencies via requirements.txt, merging related RUN instructions to reduce image layers, and using --no-cache-dir options to optimize image size. These practices ensure build process reliability, maintainability, and performance optimization.