I use build-arg
to ensure my container runs with the same UID as my host user:
--build-arg UID=$(UID)
I am mounting a volume with:
-v $(PWD)/packages:/mnt/packages
Because I want my container to produce some output into that directory.
The directory belongs to the host user.
But, not matter what I try, the mountpoint in the container belongs to root. I can correct it with sudo in the running container:
function correct-mountpoint-permissions () {
# Hack alert:
# When mounting a host volume, it belongs to root, no matter what I do in the host or in the Dockerfile
# Here we correct the situation by using sudo. It's not nice, but that's life
# Note: the same problem does NOT happen with *named* volumes, but that completely defeats the purpose of
# mounting the volume in the first place - which is to produce some output visible on the host.
# If I have to start copying from containers to host, mounting volumes offers no benefit
sudo chown "$USER:$USER" "$PACKAGES"
}
But this is just ugly. I should not even need to have sudo installed in the first place, or sudo rights at all in my container.
The alternative is to either mount a named volume, which is difficult to access from the host, or to avoid mounting volumes in the container at all, and just docker cp
from it when my container has produced the expected output.
Is there a simple way of mounting a host volume with the right ownership into a container (which means, not root, but my selected UID
)