title: How to cache local Maven repository using Docker with Pipelines?
body: <p>I want to run my Maven builds in a Docker container. I don't want to upload all depenedencies with every build, so I tried to mount host's local Maven repository as documented at <a href="https://jenkins.io/doc/book/pipeline/docker/#caching-data-for-containers" rel="noreferrer">Using Docker with Pipeline</a>:</p>
  
  <blockquote>
    <p><strong>Caching data for containers</strong></p>
    
    <p>[...]</p>
    
    <p>Pipeline supports adding custom arguments which are passed to Docker, allowing users to specify custom Docker Volumes to mount, which can be used for caching data on the agent between Pipeline runs. The following example will cache ~/.m2 between Pipeline runs utilizing the maven container, thereby avoiding the need to re-download dependencies for subsequent runs of the Pipeline.</p>
  
  <pre><code>pipeline {
      agent {
          docker {
              image 'maven:3-alpine'
              args '-v $HOME/.m2:/root/.m2'
          }
      }
      stages {
          stage('Build') {
              steps {
                  sh 'mvn -B'
              }
          }
      }
  }
  </code></pre>
  </blockquote>
  
  <p><strong>Code</strong></p>
  
  <pre><code>pipeline {
      agent {
          docker { 
              image 'maven:3-alpine' 
              args '-v /home/jenkins/.m2:/root/.m2'
          }    
      }
      stages {
          stage('Build') {
              steps {
                  sh 'mvn -B clean verify'
              }
          }
      }   
  }
  </code></pre>
  
  <p><strong>Log</strong></p>
  
  <pre><code>Running in Durability level: MAX_SURVIVABILITY
  [Pipeline] Start of Pipeline
  [Pipeline] node
  Running on jenkins-docker in /home/jenkins/workspace/Test/Docker Test@2
  [Pipeline] {
  [Pipeline] sh
  + docker inspect -f . maven:3-alpine
  .
  [Pipeline] withDockerContainer
  jenkins-docker does not seem to be running inside a container
  $ docker run -t -d -u 1000:1000 -v /home/jenkins/.m2:/root/.m2 -w "/home/jenkins/workspace/Test/Docker Test@2" -v "/home/jenkins/workspace/Test/Docker Test@2:/home/jenkins/workspace/Test/Docker Test@2:rw,z" -v "/home/jenkins/workspace/Test/Docker Test@2@tmp:/home/jenkins/workspace/Test/Docker Test@2@tmp:rw,z" -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** maven:3-alpine cat
  
  [...]
  
  [DEBUG] Reading global settings from /usr/share/maven/conf/settings.xml
  [DEBUG] Reading user settings from ?/.m2/settings.xml
  [DEBUG] Reading global toolchains from /usr/share/maven/conf/toolchains.xml
  [DEBUG] Reading user toolchains from ?/.m2/toolchains.xml
  [DEBUG] Using local repository at /home/jenkins/workspace/Test/Docker Test@2/?/.m2/repository
  [DEBUG] Using manager EnhancedLocalRepositoryManager with priority 10.0 for /home/jenkins/workspace/Test/Docker Test@2/?/.m2/repository
  </code></pre>
  
  <p><strong>Problem</strong></p>
  
  <p>After building the <code>~/m2</code> directory is empty, no file/directory was added. All files were added under <code>/home/jenkins/workspace/Test/Docker Test@2/?/.m2</code> (<em>Test</em> is the name of the folder, <em>Docker Test</em> is the name of the pipline). </p>
  
  <p>The problem is that this directory is only used for this particular pipeline not for other pipelines, so I could not share local Maven repository with different pipelines/jobs. </p>
  
  <p>Also my <code>settings.xml</code> is not used, because it is saved under <code>~/m2</code>.</p>
  
  <p>Is there any solution for sharing local Maven repository and Maven settings with different pipelines using Docker? </p>