Ok, my question is all about
Am I doing it right? if not, what’s the best practice?
Recently I’ve used Docker for my project. Currently, I build a new docker image for each version of my project that I want to release. For example, I have a new version per month, so I’m making a new docker image monthly.
As you know, each docker image has its own base image. I use the node
image as the base image this way;
FROM node:alpine
The line above is always the first line of my Dockerfile which causes over 100MB size of the generated image. I want to know, am I doing it correctly? Should it be always there for each image? Isn’t there any better approach to not embedding the base image in each version/image?
2
Answers
Each image you build with docker is a standalone image so there is no way to avoid this base image line.
If size is the concern you can always use smallest linux images like alpine or busybox and then install your dependencies like node using package manager. There are other size optimization techniques like staged build which you can try.
Docker images use a layered filesystem. If a layer is unchanged between two builds, you only pull the new layers to update to the next version. If the base image changes between builds, then those layers will be different, but that would be needed to get the patches from that image.
A good resource for more on improving Dockerfiles is Docker’s best practices.