Files Written by Docker Belonging to Root

2018-02-02 | #docker, #solution, #ubuntu, #webdev

Say you mount your project folder as a volume into you docker container and start having your docker container write files via command line to your mount: What owner do those files have? Well, unfortunately the user logged into the command line of the docker container. In many cases that is the root user, resulting in root files appearing in your mounted folder.

At least, this is what happens in Linux/Debian/Ubuntu. Mac OS and Windows do not seem to handle this like this.

But what reason is there anyway for doing something like this?

Well, I for example, tend to let docker install js packages via npm/yarn to my project folder to have reasonable code completion without ssh and to be able to read the sources of packages I use.

But anyway, whenever you execute something via command line, writing files to the mount you'll meet this problem. So, how do we prevent this? By setting the user and group id of the docker user to the ids of the user you execute docker with. So even if the docker user is root and the host user foobar, both have the same ids, resulting in correctly owned files.

To achieve this, do:

docker-compose run --rm -u $(id -u):$(id -g) frontend_dev bash -c “yarn”