Docker with a volume with user-level permissions

If you mount a folder in your docker all the files created in it will be owned by the root. To fix this add the following to the Dockerfile

ARG USER_ID
ARG GROUP_ID

RUN addgroup --gid $GROUP_ID user
RUN adduser --disabled-password --gecos '' --uid $USER_ID --gid $GROUP_ID user
USER user

and then create the image like so:

docker build -t tensorflow112 --build-arg USER_ID=$(id -u) --build-arg GROUP_ID=$(id -g) -f Dockerfiles/ubuntu-shared-cuda .

create the instance like so:

docker run -it --name tf112 --user "$(id -u):$(id -g)" -v $(pwd):/home tensorflow112

and you can start it later like so:

docker start -i tf112

How to give it a password:

docker exec -it --user root tf112 bash
echo 'user:1' | chpasswd

How to give the user sudo permission:

docker exec -it --user root tf112 bash
usermod -aG sudo user

Comments