Matt
Matt

Reputation: 4387

Docker permissions host user volume in the container

I'm looking for a "clean" solution to share a volume and share the permissions. I mean, if it's possible, I would like to have the same user in my container than the local user who has started it.

Currently, I'm using:

docker run --rm -v ~/Projects/:/projects/ mycontainer

It works but the problem is, when my container writes some content in this volume, it's root who is the proprietary and not my local user.

What is the best way to handle this issue? If it's possible, I don't want to create user information in the Dockerfile, because I want to share this container with colleagues and I want to avoid any permissions issue between the local user and the container user.

Thanks

Upvotes: 3

Views: 1473

Answers (2)

Matt
Matt

Reputation: 4387

Thank for your help Henry.

My complete solution was:

docker run --rm -e USER_ID=$(echo "$UID") -v ~/Projects/:/projects/ mycontainer

And I've this script as entrypoint value:

#!/bin/bash
adduser -D -u $USER_ID alpine &>/dev/null
cd /projects/
su alpine -c 'mycommand ...."

With this my container can access and add/modify content in the Projects folder with the same permissions as my local user.

Upvotes: 1

Henry
Henry

Reputation: 43728

You can specify the user on the docker run command:

--user, -u      Username or UID (format: <name|uid>[:<group|gid>])

If you use the numeric UID and GID of the host user, the created files will belong to it.

Upvotes: 1

Related Questions