Multi Stage Builds and Distroless Images
Multi Stage Builds and Distroless Images
In simple terms if you want to build an 3-tier application for that you created the
docker files and you run it, in this case most of the resources like OS or runtime
for which app should run we should not use all.
If we run that container the size should definitely been high to avoid that issues if
we use the multistage build approach it is very smooth
The main focus of the multistage is reuse the resources and utilise it properly and
make the end of container size low.
For that those parameters which is common and which is reuse we can put in one
stage and those which are dynamical values or parameters we can put in last
stage
If we do this approach for heavy loaded containers the size should be become
very less and we can also use resources properly .
Docker file
#######Stage-1-Docker-File##########
# we are taking Node js and we can name it as build
FROM node:12.13.0-alpine as build
#Working directory is /app meaning all upcoming activity will address from /app
WORKDIR /app
If we use that multistage builds it have made it much easier to create optimized
images this is a very good approach while dealing with prod environment.
Distroless Image:
Suppose if you load your docker file with OS and one runtime to run the app
many resources are going to be waste
And other point is if we go ahead and import any kind of OS to run the app there
is a defiantly security issue
To avoid that issue if we use the distroless image is the best option.
Because it contains only the runtimes which app required to run, so there is NO
OS is the best strategy in distroless images
Example for distroless image
ENV GO111MODULE=off
COPY . .
############################################
# HERE STARTS THE MAGIC OF MULTI STAGE BUILD
############################################
FROM scratch (#scratch is a very basic distro less images having low size and
flexible compatability)
Maximized Security
Simplified Dependencies:
small Images
Vulnerabilities less
About Alpine:
Best competitor for distroless.
Mostly flexible for Linux based apps.
Customization is very flexible meaning if we want use for OS dependet app
we can run that also with super performance and low size.
Here are the basic difference between distroless and alpine