I am fairly new to Jenkins and Docker, and I am hitting a problem trying to get a docker image of maven to build my project.
My Jenkins script is fairly straightforward atm:
pipeline {
agent {
docker {
image 'maven:3-alpine'
args '-v /root/.m2:/root/.m2'
}
}
stages {
stage('Build') {
steps {
sh 'mvn -B -DskipTests clean install'
}
}
}
}
This produces the error "Could not create local repository at /var/empty/.m2/repository". If I specify a local repo in the command line I get the same error for whatever path I specified.
Jenkins is installed and running on a tomcat server, not in docker, which Jenkins mentions each time it runs, saying "Jenkins does not seem to be running inside a container".
The Jenkins console output looks like this:
[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
Any help would be greatly appreciated. Thanks.
instead of mounting the root/.m2 folder to the container, you probably just need to run as root user:
pipeline {
agent {
docker {
image 'maven:3-alpine'
args '-u root'
}
}
stages {
stage('Build') {
steps {
sh 'mvn -B -DskipTests clean install'
}
}
}
}
To use current maven images in a jenkins pipeline as a non root user, try the following docker volume mapping and maven environment configuration:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.