When I create a file inside a container with docker-compose run web touch test the file owns to root:root. I want to change it to be $USER without to use chown.

I'm aware of the --user option but it requires to have the user created on the Dockerfile. I prefer to not change the Dockerfile because it is committed on a git repo and I don't want to commit this because it is kind of personal config.

I tried to mount the file /etc/passwd but the user is not present when we run a command. I can use su $USER -c "..." but there is some issues with this.

So, what's the best way to set create new files with the owner set as the user of the host machine?

1 Answer 1


No need to change the Dockerfile. Just define a certain user, e.g. jenkins and a certain uid, e.g. 1000 and ensure the same uid is used when a folder is mounted.

ARG user=jenkins
ARG uid=1000

# Jenkins is run with user `jenkins`, uid = 1000
# If you bind mount a volume from the host or a data container,
# ensure you use the same uid
RUN mkdir -p $JENKINS_HOME \
    && chown ${uid}:${gid} $JENKINS_HOME \
    && addgroup -g ${gid} ${group} \
&& adduser -h "$JENKINS_HOME" -u ${uid} -G ${group} -s /bin/bash -D ${user}

RUN chown -R ${user} "$JENKINS_HOME" /usr/share/jenkins/ref


  • It makes sense. Do you know if there is a solution without creating a user in the Dockerfile at all?
    – Dougui
    May 29, 2018 at 17:27
  • It works on debian systems with RUN addgroup --gid 1000 devgroup && adduser --disabled-password --disabled-login --gecos "" --uid 1000 --gid 1000 devuser
    – Dougui
    May 29, 2018 at 18:14
  • what if this is a external volume mounted after your container is built
    – mike01010
    Mar 25, 2023 at 19:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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