Multistage Docker Builds
What is Multistage docker build?
With multi-stage builds, a Docker build uses one base image for compilation, packaging, and unit tests and then a separate image for the application runtime. As a result, the final image is smaller in size since it doesn't contain any development or debugging tools.
With multi-stage builds, you use multiple FROM
statements in your Dockerfile. Each FROM
instruction can use a different base, and each of them begins a new stage of the build. You can selectively copy artifacts from one stage to another, leaving behind everything you don't want in the final image.
What is Distroless images?
Distroless container images are a type of container image that is designed to be minimal. Unlike traditional images based on Debian or Ubuntu — which include package managers, utilities, and shells — distroless images typically contain only essential software required to run an application or service.
The concept of “distroless” images offers the promise of greatly reducing the time needed to keep applications secure by eliminating most of the software contained in typical container images. This approach also reduces the amount of time teams spend remediating vulnerabilities, allowing them to focus only on the software they are using.
Lets see a demo of it how we use multistage docker and distroless image to reduce the size of image as well as to keep our application safe.
Clone this repository and move to example folder
git clone https://github.com/APurwat/Docker-Zero-to-Hero.git
Browse a location
/home/ash/Docker/Docker-Zero-to-Hero/examples/golang-multi-stage-docker-build/dockerfile-without-multistage
First lets create a image with Dockerfile and see the image size.
docker build -t ashwini0246/dockerimagewithoutmultistage .
See docker images and Size
docker images
Now lest create docker image with multistage
go to the folder :
/home/ash/Docker/Docker-Zero-to-Hero/examples/golang-multi-stage-docker-build/
Create a image
docker build -t ashwini0246/mydockerfileimage .
See docker images and Size
docker images
Subscribe to my newsletter
Read articles from ashwini purwat directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by