- Reduce the size of the layer
- Reduce the number of layers
A layer is a change applied to an image. Every command you specify ( FROM , RUN , COPY , etc.) in your Dockerfile causes the previous image to change, thus creating a new layer.
Fig.1 - Dockerfile Layer credit: Agent of Change's YouTube
The best way to reduce the number of layers is to use a Multistage build. This turns our Docker build into 2 stages. The first stage to be built will be the exe file and the second stage will be an image using that exe file.
The following code shows how to make a multistage Docker.
Fig.2 - Dockerfile Multistage Code Example: Agent of Change's YouTube
Each FROM creates a new stage. The first stage is tagged as ''builder" by the code
FROM golang 1.7.3 AS builder
command. The code afterwards complies the code and all its dependencies together with the RUN command.
The second FROM creates a new stage from the lightweight alpine container and then copies the complied code from the "builder" stage we created previously. The container will be much smaller because the second stage doesn't have to create the complied code; instead it can use the code from the first stage.
Credit for all screenshots posted here are from the amazing YouTube channel Agent of Change. This post is just meant to summarize information taken from his video. Please go check it out and it is linked under Resources.