Home > Net >  Reducing Docker Image Size without using Alpine
Reducing Docker Image Size without using Alpine

Time:10-01

I am currently trying to reduce my Docker Image Size (Without using Alpine, since a few dependencies wouldn't work properly for my project).

Dockerfile:

FROM ubuntu:latest
# set work directory
WORKDIR /usr/src/app

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
ENV DEBIAN_FRONTEND noninteractive
# install dependencies
# copy project
COPY . /usr/src/app 
RUN apt-get update && apt-get install -y --no-install-recommends tesseract-ocr libpoppler-cpp-dev mupdf default-jre default-jdk python3-pip pkg-config && rm -rf /var/lib/apt/lists/* && apt autoremove && pip3 install -r requirements.txt
#RUN pip3 install virtualenv
#RUN python3 -m virtualenv env
#RUN source env/bin/activate


EXPOSE 8000

CMD ["python3", "manage.py", "runserver", "0.0.0.0:8000"]

I checked at docker hub and found the following image layers:

enter image description here

I came across multi-stage build using Docker but it's not much reducing the size. So, I want to know what other thing I can do to reduce the docker image size.

Current size is around 1.65GB

I also came across building your own Docker Image but the ubuntu:latest is already 72.8 MB around so I think it should work for me.

CodePudding user response:

I would start with replacing the ubuntu base image with a debian alternative. This should not bring too much pain and allows you to have access to -slim images.

e.g.

FROM debian:bullseye-slim

I also noticed you autoremove after pip install, I would advise you do this on the line you apt-install. Each layer is saved with all its content, so autoremove as soon as possible not to save it into the layer.

CodePudding user response:

Not quite a straight answer, but this blog series give a lot of context and solutions about reducing images size.

The quest for minimal docker images

  • Related