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

<blockquote>
<pre><code>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 \
    &amp;&amp; chown ${uid}:${gid} $JENKINS_HOME \
    &amp;&amp; addgroup -g ${gid} ${group} \
&amp;&amp; adduser -h "$JENKINS_HOME" -u ${uid} -G ${group} -s /bin/bash -D ${user}

RUN chown -R ${user} "$JENKINS_HOME" /usr/share/jenkins/ref
</code></pre>
</blockquote>

<p><a href="https://github.com/jenkinsci/docker/blob/master/Dockerfile-alpine" rel="noreferrer">https://github.com/jenkinsci/docker/blob/master/Dockerfile-alpine</a></p>

CC BY-SA 4.0
2018-05-29T17:27:34.290Z
It makes sense. Do you know if there is a solution without creating a user in the Dockerfile at all?
Dougui
CC BY-SA 4.0
2018-05-29T18:14:00.157Z
It works on debian systems with `RUN addgroup --gid 1000 devgroup && adduser --disabled-password --disabled-login --gecos "" --uid 1000 --gid 1000 devuser`
Dougui
CC BY-SA 4.0
2023-03-25T19:27:41.593Z
what if this is a external volume mounted after your container is built
mike01010
CC BY-SA 4.0
2018-05-29T15:17:18.943Z
2018-05-29T15:17:18.943Z
030
<p>When I create a file inside a container with <code>docker-compose run web touch test</code> the file owns to <code>root:root</code>. I want to change it to be <code>$USER</code> without to use <code>chown</code>.</p>

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

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

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

<docker><docker-compose><dockerfile>
Change owner of files created inside a Docker container without changing the Dockerfile
Dougui
# Change owner of files created inside a Docker container without changing the Dockerfile

(Asked by: username_0 on 2018-05-29)

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?

**Tags:** docker, docker-compose, dockerfile


---

## 1 answers

### Answer by username_1 on 2018-05-29. Score: 5 | Accepted answer

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


https://github.com/jenkinsci/docker/blob/master/Dockerfile-alpine

> **Comments:**
> **username_0**: It makes sense. Do you know if there is a solution without creating a user in the Dockerfile at all?  
> **username_0**: It works on debian systems with `RUN addgroup --gid 1000 devgroup && adduser --disabled-password --disabled-login --gecos "" --uid 1000 --gid 1000 devuser`  
> **username_2**: what if this is a external volume mounted after your container is built  

---

