1

I am trying to dockerize a Python streamlit app, but the image I built is way to big (1.3GB). Is there any way to minimise the image size?

Here is my dockerfile:

FROM python:3.9

WORKDIR /app

COPY ./requirements.txt ./requirements.txt

RUN pip3 install --no-cache-dir -r requirements.txt

EXPOSE 8501

COPY ./src ./src

COPY ./deployment/nginx.conf ./deployment/nginx.conf

And my requirements.txt file:

requests==2.16.0
beautifulsoup4==4.10.0
streamlit==1.10.0
lxml==4.7.1

Thanks for any help!

retr0327
  • 141
  • 4
  • 9

1 Answers1

3

Inspect image to find culprit

You can look more closely at the docker image to find out which layers are taking up a lot of space. To look at the different layers and their sizes you can use the docker history command.
Example:

docker history <image-name>

Methods to get smaller images

To improve your image size you can look into multi-staged builds.

A really good blog that shows how to realize multi-stage builds in python and create smaller images and faster builds: (blog consists of three articles)

Another improvement would be, like @pierpy pointed out, to use the alpine-version of the python image. For example:

FROM python:3.9-alpine

See all the image versions here.

kaffarell
  • 639
  • 5
  • 16