Chowning files can take a lot of space in a Docker image
· 3 min read
Today I learned that recursively changing the owner of a directory tree in a Dockerfile can result in some serious increase in image size.
🚛 The issue
You may remember how in a previous post we used a small example to discuss layers and final image size. Well, here's our example again, slightly modified.
1 2 3 4 5
Given that the base image weighs ~75MB, we could expect the final image to weigh ~175MB (~75 from the base image + ~100 from the big file we generated).
It turns out that since
chowning the file modifies it, the
example file will count twice: once in the
fallocate layer, and once in the
chown layer, resulting in an image size of ~275MB.
Since creating "large" amounts of data in a Docker image can be quite common (think about dependencies, static files, etc), I guess that workaround strategies are required. Fortunately, our backs are covered.
Let's take a slightly more complex example to illustrate some real life situations you might encounter:
1 2 3 4 5 6 7 8 9
This results in an image weighing 492MB. Let's bring it down to 283MB! (2x~100MB + ~75MB)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
There you go! By being smart about when to run the permission changes, we just saved ourselves 200MB of disk space and network bandwidth. That's about 60% for this specific image!
In the specific case I was investigating at ITSF, the image went from ~1.6GB to ~0.95GB just from this
chown trick. We were copying a bunch of files in a directory and at the end we chowned the whole directory recursively. That directory weighed about 650MB, which counted twice in the final image size.
Of course this also works with "simple"
ADD instructions. It's not reserved to copying files from other stages.
📓 Don't forget history!
I discovered that the
chown was taking that much space using the underrated
docker history command. I already briefly introduced it previously but now felt like a good time to remind you of its existence 🙂
Running it with our big 492MB image, here's the output:
1 2 3 4 5 6 7 8 9 10 11 12
<missing> rows plus the first row with a real ID above (
f643c72bc252) are the layers of the base image. All the layers above are the ones that compose our image. We can clearly see that the
chown layer weighs 210MB by itself.
That wraps it up for today! As always, I hope you learned something along the way 😊