Rob Wilkinson
Rob Wilkinson

Reputation: 1307

How to install missing python modules on distroless Image?

I am trying to install some missing modules on a python distroless image however I am getting below error

 => ERROR [3/7] RUN pip install --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host files.pythonhosted.org --upgrade pip                                                                                    0.3s
------
 > [3/7] RUN pip install --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host files.pythonhosted.org --upgrade pip:
0.253 runc run failed: unable to start container process: exec: "/bin/sh": stat /bin/sh: no such file or directory

Looks like the distroless image does not have shell (/bin/sh). Below is the Dockerfile. Is there an alternate way to install the required modules? Could you please advise.

FROM gcr.io/distroless/python3-debian12:nonroot

RUN pip install --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host files.pythonhosted.org --upgrade pip
RUN pip install --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host files.pythonhosted.org requests simplejson python-json-logger

Upvotes: 1

Views: 2031

Answers (1)

The DevOps Dude
The DevOps Dude

Reputation: 1937

To give you an idea, if I were to install Python packages in a distroless image, I’ll use a multi-stage build. I’ll first install the packages in a regular Python image, then copy them to the distroless image. Something like this:

# Use a regular Python image to install packages
FROM python:3.12-slim as builder

# Install the required packages
RUN pip install --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host files.pythonhosted.org --upgrade pip
RUN pip install --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host files.pythonhosted.org requests simplejson python-json-logger

# Switch to the distroless image
FROM gcr.io/distroless/python3-debian12:nonroot

# Copy the installed packages
COPY --from=builder /usr/local/lib/python3.12/site-packages /usr/local/lib/python3.12/site-packages
COPY --from=builder /usr/local/bin /usr/local/bin

This approach would get around the missing shell issue in distroless images by doing the package installation in a regular Python environment first.

I hope this helps.

Upvotes: 4

Related Questions