Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker: Permissions for a mounted volume

I have this image in which I mount a volume from the host

-v /Users/john/workspace:/data/workspace

Inside the container I'm using a user different than root. Now the problem is that it cannot create/modify files inside /data/workspace (permission denied). Now I solved it for now to do chmod -R 777 workspace on the host. What would be the docker way to solve this ?

like image 836
Jeanluca Scaljeri Avatar asked Dec 19 '15 09:12

Jeanluca Scaljeri


Video Answer


1 Answers

This might be solved with user mapping (issue 7198), but that same thread include:

Managed to solve this using the new dockerfile args. It doesn't require doing anything special after the container is built, so I thought I'd share. (Requires Docker 1.9)

In the Dockerfile:

# Setup User to match Host User, and give superuser permissions
ARG USER_ID=0
RUN useradd code_executor -u ${USER_ID} -g sudo
RUN echo 'code_executor ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
USER ${USER_ID} 

Then to build:

docker build --build-arg USER_ID=$(id -u)

That way, the user in the container can write in the mounted host volume (no chown/chmod required)

like image 57
VonC Avatar answered Nov 15 '22 03:11

VonC