so my Docker build is essentially done and I just have to chown
and chmod
a large directory recursively.
The following is part of the output from docker build
:
Step 34/40 : RUN du -b -m -s / 2>/dev/null || true
---> Running in d261dfc7a9f8
1537 /
Removing intermediate container d261dfc7a9f8
---> 663d129f1487
Step 35/40 : RUN df --si -m /;df -i /;
---> Running in 8249096d3069
Filesystem 1M-blocks Used Available Use% Mounted on
overlay 29674 28609 1066 97% /
Filesystem Inodes IUsed IFree IUse% Mounted on
overlay 2737520 549839 2187681 21% /
Removing intermediate container 8249096d3069
---> fc4250e3433d
Step 36/40 : RUN chmod -R a+rwX /var/www/html
---> Running in 2705265de405
Error processing tar file(exit status 1): open /var/www/html/[...].php: no space left on device
I solved my issue by running docker system prune -a
, to free up an extra 908 MB, but I don’t understand why disk space was an issue in the first place (not to mention what tar
has to do with it…).
Running watch df -m /
I can clearly see that chmod
is resulting in large swings of several 100MBs (down and up), but that doesn’t make any sense since chmod
should not change disk allocation, not even with CoW (copy on write) files or sparse files.
Why does dockers implementation of chmod (I’m using FROM centos:7.4.1708
) cause extra disk space to be used?
2
Answers
It’s not
chmod
that is causing the extra disk space. Therun
command in Dockerfile creates a layer caching that will use up your disk space. I would suggest to aggregate the commands you run using&&
.Filesystem layers in docker are implemented with a copy-on-write solution. Any change to a file results in a copy of that file first, and then the change is applied to that file copy, even a permission, owner, or timestamp. The chown and chmod commands, when run recursively, will change timestamps on the file, even if no permissions are changed.
Therefore to minimize the size of layers in docker, make all changes to a file in the same layer. Each step creates a new layer, so consolidate changes into the same step. With a
COPY
command, fix the permissions on the source and see the options to adjust ownership. And withRUN
, you will often see commands chained with the&&
syntax to compact into a single step. This is particularly important when creating temporary files, since you need to delete them before the step finishes to avoid writing them into the image layer.