Title: Bind mount docker with specific user and permission
Tags: docker;docker-compose
Question: Scenario
I want to use Docker in Docker as per this guide
The key point is that you'll need to bind mount the Docker socket from host machine into the Docker socket inside the container in order to use Docker from inside the container.
The Docker socket on the host machine is always bind mounted into Docker container under root:root. When I execute the container as a non-root user, executing Docker commands gives permission denied.
Question
How to bind mount a file from host into Docker container with specific user:group and permission?


Here is another answer: I think you can workaround that:
set the group permission on the file to ID not exists on the host, and give the group full access:
chown :999 /path/to/file
chmod 775 /path/to/file
chmod g+s /path/to/file
then on your Dockerfile add the group and assign the user to it:
RUN addgroup --gid 999 GROUPNAME
RUN adduser --disabled-password --gecos "" --force-badname --ingroup 999 USERNAME
Comment for this answer: Thanks for the suggestion, but if I set the file to some non-existing ID on host machine, the file (docker socket) will become unusable on host machine. So this doesn't work for me.
Comment for this answer: Guess I'll try that. But I still want to look for an option that does not require me to modify the file on host gholland@example.com. The idea is that I should be able to just start the container from the docker-compose file and be able to use it. Modifying the file in host machine seems like adhoc solution to me and is an extra step must be done separately.
Comment for this answer: no it will still usable, the owner will still the root

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2019:00:09/17/19', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<docker><docker-compose>', 'Title': 'Bind mount docker with specific user and permission', 'ViewCount': 761, 'id': 57966781, 'user_AboutMe': '<p>Currently a disciple of Computer and Science at Ho Chi Minh University of Technology.</p>\n\n<p>Have a passion with human language and some programming languages (but still prefer human languages :D)</p>\n\n<p>Been into Unity and Android development for more than 2 years and still learning.</p>\n\n<p>Hobbies: </p>\n\n<ul>\n<li>Language, language and more about language, Unity and Android. </li>\n<li>Playing/Listening to music</li>\n</ul>\n', 'user_AccountId': 7563086.0, 'user_DisplayName': 'Tran Triet', 'user_Reputation': 1137.0, 'user_Views': 129.0, 'user_id': 5739790.0}}