Photo by Rubaitul Azad / Unsplash

Building smaller secure docker images with Multi-Stage builds - Part II

docker Jul 11, 2019

In this post, we checked how we can build smaller and secure docker images using multi-stage builds. In that post I missed one important factor that contributes to building smaller images. Let us check what it is.

This is the most simple way to reduce images sizes of our applications. In project's root directory we can add a .dockerignore file. This will have a list of files or directories which we do not want in our final docker image. Let us take an example of a Node.js project -

# contents of .dockerignore file
node_modules/*
.media/*
logs/*
*.logs
*.logs.gz
...

In this example, we do not want our image to have dev dependencies. To run our application in production we require only productional dependencies so we can ignore it by adding the path to our node_modules/ directory and all of its contents. We can also ignore the files created locally while building or testing the application like media files, log files, etc.

This will drastically reduce the image size.

Bonus

Simple script to deploy latest docker image - Github Gist

Tags