I've been playing around with Docker for a while and keep on finding the same issue when dealing with persistent data.
I create my Dockerfile
and expose a volume or use --volumes-from
to mount a host folder inside my container.
What permissions should I apply to the shared volume on the host?
I can think of two options:
So far I've given everyone read/write access, so I can write to the folder from the Docker container.
Map the users from host into the container, so I can assign more granular permissions. Not sure this is possible though and haven't found much about it. So far, all I can do is run the container as some user:
docker run -i -t -user="myuser" postgres
, but this user has a different UID than my hostmyuser
, so permissions do not work. Also, I'm unsure if mapping the users will pose some security risks.
Are there other alternatives?
How are you guys/gals dealing with this issue?
This is arguably not the best way for most circumstances, but it's not been mentioned yet so perhaps it will help someone.
Bind mount host volume
Host folder FOOBAR is mounted in container /volume/FOOBAR
Modify your container's startup script to find GID of the volume you're interested in
$ TARGET_GID=$(stat -c "%g" /volume/FOOBAR)
Ensure your user belongs to a group with this GID (you may have to create a new group). For this example I'll pretend my software runs as the
nobody
user when inside the container, so I want to ensurenobody
belongs to a group with a group id equal toTARGET_GID
I like this because I can easily modify group permissions on my host volumes and know that those updated permissions apply inside the docker container. This happens without any permission or ownership modifications to my host folders/files, which makes me happy.
I don't like this because it assumes there's no danger in adding yourself to an arbitrary groups inside the container that happen to be using a GID you want. It cannot be used with a
USER
clause in a Dockerfile (unless that user has root privileges I suppose). Also, it screams hack job ;-)If you want to be hardcore you can obviously extend this in many ways - e.g. search for all groups on any subfiles, multiple volumes, etc.