Mittwoch, 14. August 2019

Docker - How to shrink large Docker Images.

Some images become very big - one reason being that multiple RUN commands will create a layer and keep the data there. No removal.

Example:

RUN unzip myfile.war  -d mynewdirectory/
RUN zip -r myfile.war mynewdirectory/
RUN mv myfile.war somedirectory

The myfile.war will be kept. It will not be deleted by the mv command.

Reason: Docker will create a layer for each RUN command.

Solution:

RUN unzip myfile.war  -d mynewdirectory/ &&\
    zip -r myfile.war mynewdirectory/ &&\
    mv myfile.war somedirectory

Keep everything in one layer by utilizing concatenation with '&&\'.

WARNING:
Docker is highly optimized and based on Checksums.
If everything is concatenated, building an image can take very long. 

Example: COPY or ADD will check wether a change in the underlying files or source code has been made and only execute if a change is detected.
Through concatenation, you reap Docker of the benefits regarding optimization (e.g. Checksums). No check. Everything is executed.

It is a good practice to keep this in mind when concatenating instrunctions.

Keine Kommentare:

Kommentar posten

NEW BLOG! http://cleancode.consulting/

Dear Reader, This Blog is closed and remains as an archive. Please find our new Blog at  http://cleancode.consulting/