title: Run command in Docker with declarative Jenkins Pipeline
body: <p>My Jenkins is running in Docker. I have simple Dockerfile that has only <code>npm install</code>.</p>
  
  <p>I use declarative Jenkins pipeline so it builds and runs container but when I run <code>npm run test</code> Jenkins throws error that Jest doesn't exist and </p>
  
  <p><code>npm WARN Local package.json exists, but node_modules missing, did you mean to install?</code> </p>
  
  <p>though during image build <code>node_modules</code> should have been installed </p>
  
  <p><code>Step 4/5 : RUN npm install --unsafe-perm
   ---&gt; Using cache</code>.</p>
  
  <p><strong>Dockerfile</strong></p>
  
  <pre><code>WORKDIR /app
  COPY package*.json /app/
  RUN npm install --unsafe-perm
  COPY ./ /app/
  </code></pre>
  
  <p><strong>Jenkinsfile</strong></p>
  
  <pre><code>pipeline {
      agent {
          dockerfile {
              filename 'docker/Dockerfile.test'
              dir '.'
          }
      }
      stages {
          stage ('Test') {
              steps {
                  sh 'npm run test'
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>Jenkins console output</strong></p>
  
  <pre><code>+ docker build -t b4508423f0c768b0e72e947a272772d60a2bb495 -f ./docker/Dockerfile.test .
  Sending build context to Docker daemon  5.959MB
  
  Step 1/5 : FROM node:8-alpine
   ---&gt; 75a2ffce2f88
  Step 2/5 : WORKDIR /app
   ---&gt; Using cache
   ---&gt; 8fe07f9e374f
  Step 3/5 : COPY package*.json /app/
   ---&gt; Using cache
   ---&gt; 118fc7d484b4
  Step 4/5 : RUN npm install --unsafe-perm
   ---&gt; Using cache
   ---&gt; 0bd115e6b70a
  Step 5/5 : COPY ./ /app/
   ---&gt; Using cache
   ---&gt; 58a55b1c72ff
  Successfully built 58a55b1c72ff
  Successfully tagged b4508423f0c768b0e72e947a272772d60a2bb495:latest
  [Pipeline] dockerFingerprintFrom
  [Pipeline] }
  [Pipeline] // stage
  [Pipeline] sh
  + docker inspect -f . b4508423f0c768b0e72e947a272772d60a2bb495
  .
  [Pipeline] withDockerContainer
  Jenkins seems to be running inside container 95d484a9ea8a1a1377e19ac13a5303935bf3530c466b6dd88dba48798c98b00a
  $ docker run -t -d -u 0:0 -w /var/jenkins_home/workspace/customers_survey_front --volumes-from 95d484a9ea8a1a1377e19ac13a5303935bf3530c466b6dd88dba48798c98b00a -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 ******** b4508423f0c768b0e72e947a272772d60a2bb495 cat
  $ docker top 7b36043b2385a5e5a65de07284e14002bf5b59ec56724f575202fa9193d0ecf9 -eo pid,comm
  [Pipeline] {
  [Pipeline] timeout
  Timeout set to expire in 15 min
  [Pipeline] {
  [Pipeline] stage
  [Pipeline] { (Test)
  [Pipeline] sh
  + npm run test
  
  &gt; customers_survey@1.0.0 test /var/jenkins_home/workspace/customers_survey_front
  &gt; jest
  
  sh: jest: not found
  npm ERR! file sh
  npm ERR! code ELIFECYCLE
  npm ERR! errno ENOENT
  npm ERR! syscall spawn
  npm ERR! customers_survey@1.0.0 test: `jest`
  npm ERR! spawn ENOENT
  npm ERR! 
  npm ERR! Failed at the customers_survey@1.0.0 test script.
  npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
  npm WARN Local package.json exists, but node_modules missing, did you mean to install?
  </code></pre>