This error occurs when a Docker container tries to execute a script or command that requires Python, but the Python interpreter is not installed or is at a different path in the container's filesystem.
The error "cannot find /usr/bin/python: No such file or directory" indicates that the container is trying to run a script or command that expects Python to be available at /usr/bin/python, but no Python executable exists at that location. This is a common issue because the specific path /usr/bin/python typically refers to Python 2, which is no longer included by default in most modern Linux distributions and Docker base images. This error can occur in several scenarios: when running a script with a shebang line pointing to /usr/bin/python, when a build tool or package manager expects Python at that location, or when using a minimal base image (like Alpine or slim variants) that doesn't include Python at all. The root cause is usually a mismatch between what the script or application expects and what's actually available in the container. Modern systems typically only have Python 3 installed, and it's usually available as python3 or /usr/bin/python3 rather than the unversioned python command.
First, determine if your application needs Python 2 or Python 3. Check your scripts' shebang lines and any documentation:
# View the shebang line of your script
head -1 your_script.py
# Common shebangs:
#!/usr/bin/python # Usually expects Python 2
#!/usr/bin/python3 # Explicitly Python 3
#!/usr/bin/env python # Uses PATH to find python
#!/usr/bin/env python3 # Uses PATH to find python3Most modern applications should use Python 3, as Python 2 reached end-of-life in January 2020.
Add Python installation to your Dockerfile using the appropriate package manager for your base image:
# For Debian/Ubuntu-based images
RUN apt-get update && apt-get install -y python3 python3-pip && \
ln -s /usr/bin/python3 /usr/bin/python && \
rm -rf /var/lib/apt/lists/*
# For Alpine-based images
RUN apk add --no-cache python3 py3-pip && \
ln -s /usr/bin/python3 /usr/bin/python
# For RHEL/CentOS/Fedora-based images
RUN dnf install -y python3 python3-pip && \
ln -s /usr/bin/python3 /usr/bin/pythonThe symlink command creates the /usr/bin/python alias pointing to python3.
If your application heavily relies on Python, consider using an official Python base image:
# Full Python image
FROM python:3.12
# Slim variant (smaller, but has Python)
FROM python:3.12-slim
# Alpine variant (smallest, uses musl libc)
FROM python:3.12-alpine
# Your application code
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
CMD ["python", "app.py"]This ensures Python is properly installed and configured with all necessary tools.
If you control the scripts, update the shebang to use python3 explicitly or use env for portability:
# Change from:
#!/usr/bin/python
# To one of these:
#!/usr/bin/python3
#!/usr/bin/env python3You can update shebangs in your Dockerfile:
# Fix shebang in existing scripts
RUN sed -i 's|#!/usr/bin/python|#!/usr/bin/python3|g' /app/script.pyUsing #!/usr/bin/env python3 is more portable as it finds python3 in the PATH.
If the error occurs during npm install (node-gyp needs Python), configure the Python path:
# Install Python and set it for node-gyp
RUN apt-get update && apt-get install -y python3 make g++ && \
rm -rf /var/lib/apt/lists/*
# Tell npm/node-gyp where Python is
ENV PYTHON=/usr/bin/python3
# Or configure npm directly
RUN npm config set python /usr/bin/python3
# Then run npm install
RUN npm installFor newer Node.js versions, you may also need build-essential or equivalent build tools.
If you're unsure what Python is available, inspect the container:
# Run an interactive shell in the container
docker run -it --entrypoint /bin/sh <image_name>
# Check if Python exists and where
which python
which python3
ls -la /usr/bin/python*
# Check PATH
echo $PATH
# Test Python directly
python3 --versionThis helps identify exactly what's available and what's missing.
For multi-stage builds or minimal images where you don't want full Python:
# Multi-stage build example
FROM python:3.12 AS builder
WORKDIR /app
COPY requirements.txt .
RUN pip install --user -r requirements.txt
FROM python:3.12-slim
WORKDIR /app
COPY --from=builder /root/.local /root/.local
COPY . .
ENV PATH=/root/.local/bin:$PATH
CMD ["python", "app.py"]This keeps the final image smaller while ensuring Python is available.
Python 2 vs Python 3 in containers: The unversioned python command historically referred to Python 2. Since Python 2's end-of-life in 2020, most distributions no longer include it. If you absolutely need Python 2 for legacy code, you'll need to install it explicitly, but be aware that Python 2 packages are increasingly unavailable in modern package repositories.
Alpine Linux considerations: Alpine uses musl libc instead of glibc. Some Python packages with C extensions may not work or may need additional build dependencies. Consider using python:3.12-slim instead of python:3.12-alpine if you encounter compatibility issues.
The python-is-python3 package: On Debian/Ubuntu-based images, you can install the python-is-python3 package which creates the necessary symlinks:
RUN apt-get update && apt-get install -y python-is-python3PEP 394 compliance: According to PEP 394, the python command should not be expected to exist on systems. Scripts should use python3 explicitly. Update legacy scripts to follow this standard for better compatibility.
Distroless and scratch images: These ultra-minimal images contain no package manager or shell. If you need Python, you must use multi-stage builds to copy the Python installation, which is complex. Consider using gcr.io/distroless/python3 instead.
image operating system "linux" cannot be used on this platform
How to fix 'image operating system linux cannot be used on this platform' in Docker
manifest unknown: manifest unknown
How to fix 'manifest unknown' in Docker
cannot open '/etc/passwd': Permission denied
How to fix 'cannot open: Permission denied' in Docker
Error response from daemon: failed to create the ipvlan port
How to fix 'failed to create the ipvlan port' in Docker
toomanyrequests: Rate exceeded for anonymous users
How to fix 'Rate exceeded for anonymous users' in Docker Hub