Dockers aka Micro-Containers: Scale with limitless possibilities
you must have heard about deploying your applications on Docker, shippable, maintainable …. blah, blah…, but there are alarming things we must keep an eye onto.
Docker enables you to package up your application along with all of the application’s dependencies into a nice self-contained image. You can then use that image to run your application in containers.
The problem is you usually package up a lot more than what you need so you end up with a huge image and therefore huge containers.
I was able to strip it down to barely 23MB by using microcontainers.
What is Microcontainer?
A Microcontainer contains only the OS libraries and language dependencies required to run an application and the application itself. Nothing more.
Rather than starting with everything but the kitchen sink, start with the bare minimum and add dependencies on an as-needed basis.
Taking the exact same Node app above, using a really small base image and installing just the essentials, namely Node.js and its dependencies, it comes out to 23MB. A full 30 times smaller!
Why Are Microcontainers Great?
The above image itself explains why is it great ?…. I still have doubts following will give you a better understanding.
- Size — MicroContainers are small. As shown above, without changing any code the image is 30 times smaller than a typical image.
- Fast/Easy Distribution — Because the size is so much smaller, it’s much quicker to download the image from a Docker registry (eg: Docker Hub) and therefore it can be distributed to different machines much quicker.
- Improved Security — Less code/fewer programs in the container means less attack surface. And, the base OS can be more secure.
These benefits are similar to the benefits of Unikernels, with none of the drawbacks & the best thing is you can write your own container using kernel-level virtualization.
How to Build Microcontainers?
The base image for all Docker images is the `scratch` image. It has essentially nothing in it. This may sound useless, but you can actually use it to create the smallest possible image for your application if you can compile your application to a static binary with zero dependencies as you can with Go or C.
Not everyone is using Go (unfortunately) so you’ll probably have more dependencies and you’ll want something with a bit more than the scratch image. Use Alpine Linux “Alpine Linux is a security-oriented, lightweight Linux distribution based on libc and busybox.”
You can read more about what each of those things means here, but what we care the most about for this article is the “lightweight” part. The base Alpine image is only 5MB:
Now to add our code to the image, it’s just a few more lines in our Dockerfile
Smaller Base Image for All Languages
Just look at this much smaller docker base images for almost all languages.
These have some optimizations in them to make them as small as possible and we update them regularly, which makes them a slightly better choice than doing it yourself.
Using the Iron.io base images, the Dockerfile above for the Node app changes to this:
Also, for every language, they have built two versions of the image. one for building and one for running. The images for the building have all the build tools in them so they are generally much bigger than the ones for running.
Now you will be able to create Docker images for your applications that contain nothing more than what is required to run your app.
A container is essentially an instance of an image, so once you start firing up containers using your image, you’ve entered the world of microcontainers.