Answers: <p>As usual I think I came up with the answers shortly after spelling out the problem carefully here. I would be great if someone could confirm that my understanding is correct.</p>
  
  <hr>
  
  <h2>TL;DR: It's not possible to use <code>--build-arg</code> for <code>FROM</code> images</h2>
  
  <p>The <code>FROM</code> command basically tells docker what binary image to start from. The <code>FROM</code> image is not built recursively. This obviously means that <code>--build-arg</code> won't be taken into account in the <code>FROM</code> image.</p>
  
  <h2>So why is <code>uid</code> and <code>gid</code> even <code>ARG</code>s in the first place if I can't use them?</h2>
  
  <p>It <em>is</em> possible to use them in a reasonable straight forward way. <code>docker build</code> accepts git repo URLs when building, so you can indeed kick off a Jenkins docker with custom <code>uid</code> and <code>gid</code> quite easily as follows:</p>
  
  <pre><code>docker build -t my-jenkins --build-arg uid=1003 --build-arg gid=1003 https://github.com/jenkinsci/docker.git
  docker run -it -p 8080:8080 --rm my-jenkins
  </code></pre>
  
  <h2>But I need to create my own <code>Dockerfile</code> :-(</h2>
  
  <p>If you really need to custobmize the Jenkins image <em>and</em> need a custom uid / gid, the best way forward is to change the uid / gid of the Jenkins user in your Dockerfile. The uid = 1000 and gid = 1000 is already hardwired when your part of the Dockerfile kicks in.</p>
  
Answers: CC BY-SA 4.0
Answers: 2020-01-25T16:13:38.317Z
Answers: 2020-01-25T16:13:38.317Z
Answers: aioobe
Body: <p>I have the following <code>Dockerfile</code>:</p>
  
  <pre><code>FROM jenkins/jenkins:latest
  </code></pre>
  
  <p>I'm build a Jenkins container as follows:</p>
  
  <pre><code>docker build -t my-jenkins --build-arg uid=1003 --build-arg gid=1003 .
  </code></pre>
  
  <p>I then run the image as follows:</p>
  
  <pre><code>docker run -it -p 8080:8080 my-jenkins
  </code></pre>
  
  <p>I go to <code>localhost:8080</code> and finish installation (without any plugins selected).</p>
  
  <p>I then create a job with a single shell command:</p>
  
  <pre><code>id
  </code></pre>
  
  <p>I run the job and I expect the output of the job to be</p>
  
  <pre><code>+ id
  uid=1003(jenkins) gid=1003(jenkins) groups=1003(jenkins)
  </code></pre>
  
  <p>The output however, is</p>
  
  <pre><code>+ id
  uid=1000(jenkins) gid=1000(jenkins) groups=1000(jenkins)
  </code></pre>
  
  <p>which seems to indicate that my <code>--build-arg</code>s for <code>uid</code> and <code>gid</code> are <strong>completely ignored</strong>.</p>
  
  <p>Why is this? The Jenkins Dockerfile <a href="https://github.com/jenkinsci/docker/blob/master/Dockerfile" rel="nofollow noreferrer">should accept uid/gid as guild args</a>.</p>
  
Tags: <docker><jenkins>
Title: Running Jenkins in docker with custom uid / gid
UserDisplayName: aioobe
ThreadText: # Running Jenkins in docker with custom uid / gid
  
  (Asked by: username_0 on 2020-01-25)
  
  I have the following `Dockerfile`:
  
  ```
  FROM jenkins/jenkins:latest
  
  ```
  
  I'm build a Jenkins container as follows:
  
  ```
  docker build -t my-jenkins --build-arg uid=1003 --build-arg gid=1003 .
  
  ```
  
  I then run the image as follows:
  
  ```
  docker run -it -p 8080:8080 my-jenkins
  
  ```
  
  I go to `localhost:8080` and finish installation (without any plugins selected).
  
  I then create a job with a single shell command:
  
  ```
  id
  
  ```
  
  I run the job and I expect the output of the job to be
  
  ```
  + id
  uid=1003(jenkins) gid=1003(jenkins) groups=1003(jenkins)
  
  ```
  
  The output however, is
  
  ```
  + id
  uid=1000(jenkins) gid=1000(jenkins) groups=1000(jenkins)
  
  ```
  
  which seems to indicate that my `--build-arg`s for `uid` and `gid` are **completely ignored**.
  
  Why is this? The Jenkins Dockerfile [should accept uid/gid as guild args](https://github.com/jenkinsci/docker/blob/master/Dockerfile).
  
  **Tags:** docker, jenkins
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2020-01-25. Score: 1
  
  As usual I think I came up with the answers shortly after spelling out the problem carefully here. I would be great if someone could confirm that my understanding is correct.
  
  ---
  
  ## TL;DR: It's not possible to use `--build-arg` for `FROM` images
  
  The `FROM` command basically tells docker what binary image to start from. The `FROM` image is not built recursively. This obviously means that `--build-arg` won't be taken into account in the `FROM` image.
  
  ## So why is `uid` and `gid` even `ARG`s in the first place if I can't use them?
  
  It *is* possible to use them in a reasonable straight forward way. `docker build` accepts git repo URLs when building, so you can indeed kick off a Jenkins docker with custom `uid` and `gid` quite easily as follows:
  
  ```
  docker build -t my-jenkins --build-arg uid=1003 --build-arg gid=1003 https://github.com/jenkinsci/docker.git
  docker run -it -p 8080:8080 --rm my-jenkins
  
  ```
  
  ## But I need to create my own `Dockerfile` :-(
  
  If you really need to custobmize the Jenkins image *and* need a custom uid / gid, the best way forward is to change the uid / gid of the Jenkins user in your Dockerfile. The uid = 1000 and gid = 1000 is already hardwired when your part of the Dockerfile kicks in.
  
  
  ---
  
  