Containerisation Docker

Docker Saving 361MB on an Image File with dockerignore

Initially I created a Dockerfile to run my django app. I chose python alpine to save on image size.

There were a few issues with that but that is fixed in the below Dockerfile

FROM python:3.8-alpine

RUN mkdir -p /code/requirements

RUN pip install --upgrade pip --no-cache-dir

# Installing requirements.txt from project
COPY ./requirements/*.txt /code/requirements/.
RUN apk add --no-cache --virtual .build-deps gcc libffi-dev openssl-dev musl-dev mariadb-dev \
    && pip install --no-cache-dir -r /code/requirements/production.txt \
    && apk del .build-deps gcc libffi-dev musl-dev openssl-dev mariadb-dev

COPY . /code/

# Give access to non root user
RUN rm -rf /code/.git* && \
    chown -R 1001 /code && \
    chgrp -R 0 /code && \
    chmod -R g+w /code

USER 1001


CMD ["sh", "-c", "python collectstatic --no-input; python migrate; python runserver"]

The image size is a whopping 536MB

    REPOSITORY                                                                   TAG                 IMAGE ID            CREATED             SIZE
    trademate-app                                                                latest              c68e2e23464f        40 seconds ago      536MB

After checking the history:

$ docker history trademate-app 
IMAGE               CREATED              CREATED BY                                      SIZE                COMMENT
6f2ed6c9d629        About a minute ago   /bin/sh -c #(nop)  CMD ["sh" "-c" "python ma…   0B                  
da5c27376179        About a minute ago   /bin/sh -c #(nop)  EXPOSE 8000                  0B                  
49e374f1bd68        About a minute ago   /bin/sh -c #(nop)  USER 1001                    0B                  
f705cd2c2415        About a minute ago   /bin/sh -c rm -rf /code/.git* &&     chown -…   189MB               
d48349f175fa        2 minutes ago        /bin/sh -c #(nop) COPY dir:47dc9677cabb09e63…   193MB               
848177bf54b2        2 minutes ago        /bin/sh -c apk add --no-cache --virtual .bui…   37.9MB              
b5ab3803fe68        8 minutes ago        /bin/sh -c #(nop) COPY file:7b8d1a2c6c47119d…   215B                
ffe7a4e57a38        8 minutes ago        /bin/sh -c pip install --upgrade pip --no-ca…   5.03MB              
d7e45c46def5        8 minutes ago        /bin/sh -c #(nop) WORKDIR /code                 0B                  
8278e34b58a3        8 minutes ago        /bin/sh -c mkdir -p /code/requirements          0B                  
204216b3821e        2 months ago         /bin/sh -c #(nop)  CMD ["python3"]              0B                  
<missing>           2 months ago         /bin/sh -c set -ex;   wget -O "$P…   6.24MB              
<missing>           2 months ago         /bin/sh -c #(nop)  ENV PYTHON_GET_PIP_SHA256…   0B                  
<missing>           2 months ago         /bin/sh -c #(nop)  ENV PYTHON_GET_PIP_URL=ht…   0B                  
<missing>           2 months ago         /bin/sh -c #(nop)  ENV PYTHON_PIP_VERSION=19…   0B                  
<missing>           2 months ago         /bin/sh -c cd /usr/local/bin  && ln -s idle3…   32B                 
<missing>           2 months ago         /bin/sh -c set -ex  && apk add --no-cache --…   98.6MB              
<missing>           2 months ago         /bin/sh -c #(nop)  ENV PYTHON_VERSION=3.8.0     0B                  
<missing>           2 months ago         /bin/sh -c #(nop)  ENV GPG_KEY=E3FF2839C048B…   0B                  
<missing>           2 months ago         /bin/sh -c apk add --no-cache ca-certificates   551kB               
<missing>           2 months ago         /bin/sh -c #(nop)  ENV LANG=C.UTF-8             0B                  
<missing>           2 months ago         /bin/sh -c #(nop)  ENV PATH=/usr/local/bin:/…   0B                  
<missing>           2 months ago         /bin/sh -c #(nop)  CMD ["/bin/sh"]              0B                  
<missing>           2 months ago         /bin/sh -c #(nop) ADD file:fe1f09249227e2da2…   5.55MB

So you can see the removal of the git folder and env folder was screwing me along with the copy.

So I added a .dockerignore file containing:


and removing the rm -rf /code/.git* command.

The image is now 175 MB

REPOSITORY                                                                   TAG                 IMAGE ID            CREATED             SIZE
trademate-app                                                                latest              0040fef600ad        8 minutes ago       175MB

Saving a whopping 361MB

Any other improvements would be welcomed…send me an email.