2

I've got a Docker image which generates log-like files when errors occur. I've mounted the directory it writes to to my host machine with a bind mount. However, the created files are owned by root. Though my user account has root privileges, it is tedious to run chown and chgrp after every run of the container in order to inspect the files.

Is there a way to have the container set the owner and group of the files to that of the user who ran the container?

For some context, here's a toy example I created:

Dockerfile

FROM debian

WORKDIR /root
VOLUME /root/output

COPY run.sh /root/

ENTRYPOINT ["./run.sh"]

run.sh

#!/bin/bash

echo hello > output/dump

My execution command is

docker run -v $PWD/output:/root/output test
3

the files are created by the user that runs within the container. if your containerized command runs as root, then all files will be created as root.

if you want your files to be created as another user, run the container as this other user. e.g.

docker run -v "$(pwd)/output":/root/output -u $(whoami) test

note: depending on your container this might not work out of the box (e.g. because within the container you need to open a privileged port, or your script is only accessible by a given (super)user)

4
  • So, I have to create a user with the same name inside the container? Otherwise, the container complains that the user could not be found in /etc/passwd. – Daniel Walker Jan 1 at 0:43
  • 1
    you could also just use the (numeric) uid instead of the username. e.g. docker run -v "$(pwd)/output":/root/output -u ${UID} test – umläute Jan 1 at 20:19
  • Passing in the UID gave docker: Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "exec: \"./run.sh\": stat ./run.sh: permission denied": unknown.. run.sh is executable by all. – Daniel Walker Jan 1 at 22:01
  • 1
    well your run.sh script lives in /root/, and /root/ is typically not readable by anyone but the superuser. – umläute Jan 1 at 22:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.