Search code examples
dockerjenkinsjenkins-pipelinejenkins-blueocean

Dependencies installed with Dockerfile are inaccessible in later pipeline steps. How to enable?


Currently using Jenkins Blue Ocean to try and setup a new pipeline. It's a node based project and I am running npm install within the Dockerfile with success, however later pipeline steps do not see the installed node_modules. This behaviour is rather strange.

Jenkinsfile.

pipeline {
  agent {
    dockerfile {
      filename 'Dockerfile.prod'
    }
  }
  stages {
    stage('Test') {
      steps {
        sh 'ls node_modules'
      }
    }
  }
}

Dockerfile.prod

FROM node:8

ADD . /var/www    
WORKDIR /var/www
RUN ["npm", "install"]

During Dockerfile build:

Step 4/4 : RUN ["npm", "install"]

 ---> Running in de3cd138a40e
...
added 1283 packages in 39.842s

Removing intermediate container de3cd138a40e

Within the test stage of the Jenkinsfile:

+ ls node_modules

ls: cannot access node_modules: No such file or directory

Solution

  • The issue has been resolved. After Jenkins compiles the initial dockerfile, for the later pipeline steps it properly launches it. However, if one views the job logs (not in the blueocean view), one can see that Jenkins then overrides the WORKDIR while launching the container docker run -t -d -u 1000:1000 -w /var/jenkins_home/workspace/workspace_name. Therefore one needs to manually navigate to the expected WORKDIR.