I am trying to create a python based image with some packages installed. But i want the image layer not to show anything about the packages I installed.
I am trying to use the multistage build
eg:
FROM python:3.9-slim-buster as builder
RUN pip install django # (I dont want this command to be seen when checking the docker image layers, So thats why using multistage build)
FROM python:3.9-slim-buster
# Here i want to copy all the site packages
COPY --from=builder /usr/local/lib/python3.9/site-packages /usr/local/lib/python3.9/site-packages
Now build image
docker build -t python_3.9-slim-buster_custom:latest .
and later check the image layers
dive python_3.9-slim-buster_custom:latest
this will not show the RUN pip install django
line
Will this be a good way to achieve what i want (hide all the pip install commands)
3
Answers
Currently whats working for me is.
It depends on what you are installing, if this will be sufficient or not. Some python libraries add binaries to your system on which they rely.
The usual approach I see for this is to use a virtual environment in an earlier build stage, then copy the entire virtual environment into the final image. Remember that virtual environments are very specific to a single Python build and installation path.
If your application has its own
setup.cfg
orsetup.py
file, then a minimal version of this could look like:Note that this has only minimal protection against a curious user seeing what’s in the image. The
docker history
ordocker inspect
output will show the/app
container directory, you candocker run --rm the-image pip list
to see the package dependencies, and the application and library source will be present in a human-readable form.