简体   繁体   中英

Unable to mount a volume to a docker container during jenkins pipeline

I have a jenkins pipeline to checkout, build and deploy a react project.

pipeline {
    agent {
        docker {
            image 'node:6-alpine'
            args '-v /home/nginx/console:/home/'
            args '-p 3000:3000' 
        }
    }
        environment {
        CI = 'true' 
    }

    stages {
        stage('Install') { 
            steps {
                sh 'touch /tmp/azeiodfsihqznlkdsqjfn'
                sh 'npm install' 
            }
        }
        stage('Test') { 
            steps {
                sh 'npm test' 
            }
        }

        stage('Build') { 
            steps {
                sh 'npm run build' 
            }
        }



        stage('Deploy') {

            steps {
                sh 'ls'
                sh '$(pwd)'
            }
        }

    }
}

I want to mount the folder /home of my container with the directory /home/nginx/console on my host. After the build, inside the container, I will move the dist/* content to /home in order to have it inside /home/nginx/console

But it seems that the volume is not mounted. When I create a file inside /home, I have nothing in my host.

EDIT : in my console log I have :

Checking out Revision 3471c656f032692288de13f357c4b8ba54b16ca4 (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 3471c656f032692288de13f357c4b8ba54b16ca4
Commit message: "Update Jenkinsfile"
 > git rev-list --no-walk c5930fc469fd066720df45cdf466788957cdb0bc # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] sh
+ docker inspect -f . node:6-alpine
.
[Pipeline] withDockerContainer
Jenkins seems to be running inside container 1ce58d845edf831599312c3d016c418a720891ea17c7bacd8327c48f9c44e9f6
$ docker run -t -d -u 0:0 -p 3000:3000 -w /var/jenkins_home/workspace/gara-developer-console --volumes-from 1ce58d845edf831599312c3d016c418a720891ea17c7bacd8327c48f9c44e9f6 -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** node:6-alpine cat
$ docker top 2fe2bdef3f475373cf85dfd452b6bbda21bc02dc308458ffff061b0ae9e963ec -eo pid,comm
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Install)
[Pipeline] sh
+ touch /home/azeiodfsihqznlkdsqjfn
[Pipeline] sh
+ npm install

Please, do you know how could I correct it ?

I think you need to remove the trailing slash from the /home/ path on the mount as currently I'd expect your mount -v /home/nginx/console:/home/ to create the mount /home/console .

So either write your files to /home/console inside the container, or update your mount statement to read:

args '-v /home/nginx/console:/home'

You shouldn't need to do anything here (and in particular you should be able to remove the args '-v ...' line). Jenkins will on its own mount the workspace directory into the Docker container on the same path as on the host.

By the time you get up to the Deploy stage the built content is in the Jenkins workspace directory, both on your host and inside the container. If you're running Jenkins on your production system (consider moving it!) and the “deployment” action really is just copying to a different host directory, you can declare an alternate agent for that one step. I would try making the pipeline look like

stage('Deploy') {
  agent { any }
  steps {
    ...
  }
}

(Practically I'd probably invert this and run only the npm steps inside Docker, defaulting to run not in Docker.)

As said by @Matt in comment, the solution is to have just one command

Your second args is overriding the first. Combine them with args

'-v /home/nginx/console:/home/ -p 3000:3000'

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM