使用maven与jenkins的docker镜像 - 无法创建本地存储库

时间:2018-03-10 23:11:20

标签: maven docker jenkins

我对Jenkins和Docker相当陌生,我在尝试获取maven的docker镜像以构建我的项目时遇到了问题。

我的Jenkins脚本非常简单:

[Pipeline] withDockerContainer
Jenkins does not seem to be running inside a container
$ docker run -t -d -u 123:128 -v /root/.m2:/root/.m2 -w /usr/share/tomcat7/.jenkins/workspace/MyProject -v /usr/share/tomcat7/.jenkins/workspace/MyProject:/usr/share/tomcat7/.jenkins/workspace/MyProject:rw,z -v /usr/share/tomcat7/.jenkins/workspace/MyProject@tmp:/usr/share/tomcat7/.jenkins/workspace/MyProject@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
$ docker top 70c607bda0e397ed8ff79a183f33b2953ed94d82e4f36eb26dea87fa8c67adf3 -eo pid,comm
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] sh
[OMRS] Running shell script
+ mvn -B -DskipTests clean install
[ERROR] Could not create local repository at /var/empty/.m2/repository -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/LocalRepositoryNotAccessibleException

这会产生错误“无法在/var/empty/.m2/repository创建本地存储库”。如果我在命令行中指定了一个本地repo,那么对于我指定的任何路径,我都会得到相同的错误。

Jenkins在tomcat服务器上安装并运行,而不是在docker中运行,Jenkins每次运行时都会提到它,并说“Jenkins似乎没有在容器内运行”。

Jenkins控制台输出如下所示:

Lambda

非常感谢任何帮助。感谢。

2 个答案:

答案 0 :(得分:1)

而不是将root / .m2文件夹挂载到容器,您可能只需要以root用户身份运行:

pipeline {


agent {
    docker {
        image 'maven:3-alpine'
        args '-u root'
    }
  }
  stages {
    stage('Build') {
        steps {
            sh 'mvn -B -DskipTests clean install'
        }
    }
  }
}

答案 1 :(得分:0)

要以非root用户身份使用jenkins管道中的当前maven映像,请尝试以下docker卷映射和maven环境配置:

args '-v $HOME/.m2:/var/maven/.m2:z -e MAVEN_CONFIG=/var/maven/.m2 -e MAVEN_OPTS="-Duser.home=/var/maven"'

https://github.com/carlossg/docker-maven#running-as-non-root