Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker image multistage build: How to create a docker image copying only python packages

Tags:

docker

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)

like image 952
Santhosh Avatar asked Apr 10 '26 06:04

Santhosh


1 Answers

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.

FROM python:3.9-alpine as builder
# install stuff


FROM python:3.9-alpine

# this is for sure required
COPY --from=builder /usr/local/lib/python3.9/site-packages /usr/local/lib/python3.9/site-packages

# this depends on what you are installing
COPY --from=builder /usr/local/bin /usr/local/bin
like image 60
The Fool Avatar answered Apr 12 '26 20:04

The Fool



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!