Docker Tip #5: The Benefits of Using the Same Base OS for All Images
Docker will happily build your images if they have a different OS, but you can get a few quick wins by using the same base OS all around.
In the previous tip, I mentioned your Docker image does not need to match your Docker host’s OS, but now we’re talking about something much different.
Let’s say that your project is composed of 3 services:
- A Flask web application
You would likely have 3 separate Docker images, and it would be a good idea to reach for the official PostgreSQL, Redis and Python (Flask is a Python web framework) Docker images.
Save Disk Space
If you pick the Alpine based official images for all 3, then a single copy of Alpine will be shared for all 3 Docker images as long as the Alpine version lines up. This is a very efficient way to use the disk space on your machine, and you can thank Docker’s layer caching mechanism for that.
It’s also worth mentioning that you could use a different base OS for each Docker image, but then you lose out on the ability to cache it across all of your images, however it will still work.
Easier to Maintain Your Dockerfiles
If you’re mixing and matching your base operating systems for each Docker image then you’re putting a pretty big cognitive load on your brain.
Now you need to keep track of package managers for different distributions of Linux. For example, Debian has
apt but Alpine uses
apk. Package names will also be different too.
If you stick to 1 base OS in your Docker images, then you always know what you’re working with. It doesn’t matter if it’s Alpine or not, just pick one.
What other benefits have you found?