Permission denied on mkdir inside of a django Docker container when running collectstatic Permission denied on mkdir inside of a django Docker container when running collectstatic docker docker

Permission denied on mkdir inside of a django Docker container when running collectstatic


Finally found a workaround other than executing collectstatic as root. As I suspected, the problem was in docker's permissions, and we should grant Docker the permissions to create folders in /static folder, which is owned by django user inside django Docker container. We can do that, knowing that userId is the same between host system and the container, by running

docker-compose run django id -u django

It outputs the userId of the django user in the system. For instance, uid is 100. Then run (not sure about gid, but it works when gid = uid + 1)

chown -R 100:101 /static

If we run ls -lh, we can see that static folder is owned by systemd-network, which is sort of a Docker user mapped to uid = 100

drwxr-xr-x  4 root            root            4.0K Sep 27 11:23 composedrwxr-xr-x  3 root            root            4.0K Nov 27 12:09 configdrwxr-xr-x  3 root            root            4.0K Nov 14 02:04 docsdrwxr-xr-x  2 root            root            4.0K Sep 27 11:23 locale-rwxr-xr-x  1 root            root            1.1K Sep 27 12:56 manage.py...drwxr-xr-x 11 systemd-network systemd-journal 4.0K Nov 21 22:15 staticdrwxr-xr-x  2 root            root            4.0K Nov 27 13:37 utils

It should solve the problem. Beware that after rebuilding the container uid of django user may change, and the error will appear again, so you would have to repeat this.

Everyone who understands a bit more how Docker works is welcome to explain what happens here, and I will accept his answer.