“Containerisation”, my manager said unequivocally when I asked him about the single most thing that revolutionized the Software development industry. He argued that without containerisation, the rapid scaling and global deployment of today's software would have been a far more formidable challenge. It's remarkable to note that nearly all the software in use today leverages containerisation in various capacities, underscoring its indispensable role in powering the modern software ecosystem!
For those of you who don’t know, here is a definition from AWS’s website ~
*“Containerisation is a software deployment process that bundles an application’s code with all the files and libraries it needs to run on any infrastructure. Traditionally, to run any application on your computer, you had to install the version that matched your machine’s operating system.”*
Images serve as the building blocks for containers, akin to blueprints guiding their creation. Therefore, maintaining lightweight and streamlined container images accelerates the deployment process and facilitates the distribution of these images. By optimizing your Docker images, not only can you expedite the overall development cycle, but you also bolster the security measures in place, ensuring a more robust and efficient software environment.
In this article, we will explore various methods to create efficient Docker images, ultimately enhancing the efficiency of your application.
It is a common mistake to treat container like Virtual Machines (VMs) and packaging more than one application inside a single container. While increasing the size of your container, this practice has other downsides too.
A container is anticipated to have a lifespan that is closely related to the application it is running. The container should start when the app starts, and cease when the app finishes. Container orchestration solutions such as Docker and Kubernetes rely significantly on this attribute of containers. If a container is running many apps, the orchestration tools will be unable to identify and restart your application!
Images are composed of layers, and each instruction in your Dockerfile adds a new layer. Containerisation technologies, such as Docker, will attempt to reuse layers from prior builds wherever feasible in order to cut build time. Cache for a layer may only be used if none of the underlying layers have changed since prior builds.
To enhance the speed of image building, it's beneficial to position instructions that may frequently change below in your Dockerfile. This approach enables Docker to leverage its caching system more effectively. For instance, since source code often undergoes frequent changes, it's advisable to copy it to the image towards the end of the process.
While it does not make your images lighter, it makes the build process run fast!
Frequently, container images include unnecessary tools, leading to image bloat and potential security vulnerabilities. For example, tools like netcat
can be exploited by hackers to carry out malicious activities within your container if they gain unauthorised access.
Strictly maintaining the set of tools and libraries that get shipped with your container image can significantly help in reducing security threats and would also result in slimmer images. Smaller images download faster, meaning container start faster.
The image that we reference in the FROM instruction is called the base image. Base image greatly dictates the size of the final image created. We should try to use the smallest base image possible to reduce the final size of our image.
Try to use alpine images wherever possible since they are smaller in size and often contain enough tools to support development activity.
Even when a small image does not contain all the tools that you require, using it may still be a better option. Installing packages that you require on top of the lighter base image may still provide saving over using the heavier image.
While copying source code, many of the unwanted files and folder might also get copied over. This increases the size of your image and ultimately leads to bloating. Try to minimise the amount of files copied and try to keep them to a minimum.
The significance of optimizing container images and establishing best practices is often overlooked in our industry. However, the substantial benefits that arise from adhering to these practices cannot be overstated. As developers, it's essential to consistently prioritize the creation of slim and fast container images.
With that we reach the end of this article. Hope you enjoyed reading it!