skip to Main Content

I want to reduce Docker image size for CentOS Python 3.9.

Earlier my image size was 2.4GB, later I implemented multi stage. It came down to 1.1GB.

Still is there any room to reduce size of image? My code files size is 19MB.

Below is my Docker file:

FROM    centos:7 AS base

FROM        base AS build

ARG         PYTHON_VERSION="3.9.5"

# Install all dependenticies
RUN yum -y update 
    && yum install -y -y sox gcc 
    openssl-devel bzip2-devel libffi-devel zlib-devel 
    cairo-devel dejavu-sans-fonts libxml2-devel 
    libxslt-devel libxml2-devel libxslt-devel 
    yum-utils curl git tmux emacs-nox make xz-devel wget

# compile python
RUN 
        DIR=/tmp/python && 
        mkdir -p ${DIR} && 
        cd ${DIR} && 
        wget https://www.python.org/ftp/python/${PYTHON_VERSION}/Python-${PYTHON_VERSION}.tgz && 
        tar xvf Python-${PYTHON_VERSION}.tgz && 
        cd Python-${PYTHON_VERSION}/ && 
        ./configure --enable-optimizations --prefix=/usr/local && 
        make altinstall && 
        cd && 
        rm -rf ${DIR}
FROM base
ENV         LD_LIBRARY_PATH=/usr/local/lib64:/usr/local/lib
COPY --from=build /usr/local/ /usr/local/
# Copy Code
COPY . /app/
WORKDIR /app
EXPOSE 8887
RUN /usr/local/bin/python3.9 -m pip install --upgrade pip 
    && pip3 install wheel && pip3.9 install -r requirements.txt
ENTRYPOINT ["python3.9"]
CMD ["app.py"]

2

Answers


  1. Your starting image is already 73MB, so you will never beat that. If you just want to run with python 3.9, I recommend using one of the slim versions of the python image, unless there is a very good reason why you want to use your own compiled build.

    In case you really need to compile your own version: I think the main reason why your final stage is so big compared to the base centos image is because you never remove the dependencies you need during compilation. This still effects you in your final stage because you copy all of the executables of gcc, git, tmux ect. to you new stage with the COPY --from=build /usr/local/ /usr/local/ line. So after compilation you should uninstall all packages you used for compilation, or simply copy only the /usr/local/bin/python3.9 from your build stage to your final stage

    Login or Signup to reply.
  2. It may be not suitable for your use case but you can give python distroless a try. Image size is only 54.2 MB and you can use it as your base image.

    FROM gcr.io/distroless/python3
    ...
    

    As stated in distroless documentation

    "Distroless" images contain only your application and its runtime dependencies. They do not contain package managers, shells or any other programs you would expect to find in a standard Linux distribution.

    I do not know why you are using centos as base image or building python itself (surely there is quite a good reason). But if those requirements are a must, this solution may not fit you, as there is no SO stuff installed withing the image.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search