Home > Back-end >  Docker image far too big from a simple python service
Docker image far too big from a simple python service

Time:11-03

I've created a simple Python service that connects with RabitMQ and Mattermost. As I'm trying to build a Docker image, I noticed the image is far to big (916MB)

I created the image first without .dockerignore file. Deleted the image afterwards and created the image again by the command docker image build --no-cache --pull -t "gitlab-service" . but the size is still the same.

I uploaded the whole gitlab-service project on https://github.com/lucasscheepers/gitlab-service

Am I doing something wrong in the Dockerfile I created?

CodePudding user response:

You are using the python:3.8 base image which is alone 909MB, so it totally makes sense that your image size is in that range.

CodePudding user response:

You are using a really big base image, so it's normal that you have that size.

If you are using the python:3.8 image because you need some packages in order to compile your dependencies, then you can just get advantage of a multi-stage build and compile/download the dependencies with python:3.8 but serve your application with python:3.8-slim that is way lighter:

FROM python:3.8 as build

WORKDIR /usr/app
RUN python -m venv /usr/app/venv
ENV PATH="/usr/app/venv/bin:$PATH"

COPY requirements.txt .
RUN pip install -r requirements.txt && rm requirements.txt

FROM python:3.8-slim
RUN groupadd -g 999 python && \
    useradd -r -u 999 python -g python
RUN mkdir /usr/app && chown python:python /usr/app
WORKDIR /usr/app

COPY --chown=python:python --from=build /usr/app/venv ./venv
COPY --chown=python:python . .

USER 999

ENV PATH="/usr/app/venv/bin:$PATH"

EXPOSE 8088
ENTRYPOINT [ "python", "main.py"]

I built both from your GitHub project:

 docker images | grep gitlab-service
gitlab-service        after        f2c1d1fab6fd   4 seconds ago   140MB
gitlab-service        before       b3e87980bccb   6 minutes ago   916MB
  • Related