title: Run Maven in Docker container via current user
body: <p>I want to run Maven to build a project in a Docker container. First, I came up with:</p>
  
  <pre><code>docker run -it --rm
             -v $HOME/.m2:/root/.m2:rw
             -v $PWD:$PWD:rw
             -w $PWD
             maven:alpine
             mvn "$@"
  </code></pre>
  
  <p>This builds fine, but the problem here is that all files are now written and owned by the <code>root</code> user. I want them to be owned by the current user, myself.</p>
  
  <p>So I tried this:</p>
  
  <pre><code>docker run -it --rm
             --user $(id -u):$(id -g)
             -v $HOME/.m2:/root/.m2:rw
             -v $PWD:$PWD:rw
             -w $PWD
             maven:alpine
             mvn "$@"
  </code></pre>
  
  <p>This did not work as expected. I believe I know why: now with <code>--user $(id -u):$(id -g)</code>, we are indeed executing as myself, but now the mapping of <code>-v $HOME/.m2:/root/.m2:rw</code> becomes incorrect, there is no <code>/root</code> in place anymore. </p>
  
  <p>So let's try this:</p>
  
  <pre><code>docker run -it --rm
             --user $(id -u):$(id -g)
             -v $HOME:$HOME:rw
             -v $PWD:$PWD:rw
             -w $PWD
             maven:alpine
             mvn "$@"
  </code></pre>
  
  <p>Now I am getting the following warning:</p>
  
  <pre><code>Can not write to /root/.m2/copy_reference_file.log. Wrong volume permissions? Carrying on ...
  </code></pre>
  
  <p>Also, Maven seems to be able to build (although I am having problems with accessing the Docker daemon during integration tests, but that might be better suited for another question), but I don't see any artifacts appearing in <code>~/.m2/repository</code> on the host? They are also not in <code>/root/.m2/repository</code> (which does not exist, as expected) on the host. Where are they? What am I doing wrong?</p>