Reducing docker image size: multistage build or other suggestions

80 views Asked by At

I am trying to deploy my Django app with docker. This is my original docker file

FROM python:3.10.5-slim

ENV DockerHOME = /app

WORKDIR $DockerHOME

COPY requirements.txt .

RUN pip install --upgrade pip

COPY . $DockerHOME

RUN pip install --nocache-dir -r requirements.txt

EXPOSE 8000

CMD ["python", "manage.py", "runserver"]

After it build however, the image size was 300MB which was too big. I then tried doing a multi stage build as follow (which according to google can help reduce the image size significantly):

FROM python:3.10.5-slim as base

ENV DockerHOME = /app

WORKDIR $DockerHOME

COPY requirements.txt .

RUN pip install --upgrade pip

RUN pip install --nocache-dir -r requirements.txt

FROM python:3.10.5-slim

COPY --from=base $DockerHOME $DockerHOME
COPY . $DockerHOME

WORKDIR $DockerHOME

EXPOSE 8000

CMD ["python", "manage.py", "runserver"]

But this also resulted in an image that was 300MB.

Could someone help me with this? I have 2 questions:

  1. Is the multistage Dockerfile logical? I feel that there is something wrong which would explain why there was no significant reduction in the image size.
  2. Are there any other ways to reduce the image size? I was originally using 'FROM python:3.10.5' instead of 'FROM python:3.10.5-slim' and that gave me 600MB image size. I changed to slim and it dropped to 300MB. Maybe another smaller image or some configs in my django project?
0

There are 0 answers