title: NodeJS in Docker - cannot access node_modules
body: <p>We have a NodeJS project building with Jenkins inside Docker containers.</p>
  
  <p>The problem is taht after image started - we have an error:</p>
  
  <blockquote>
  <pre><code>transform_1 | &gt; node ./build/server.js
  transform_1 | 
  transform_1 | module.js:472
  transform_1 |     throw err;
  transform_1 |     ^
  transform_1 | 
  transform_1 | Error: Cannot find module './lib/express'
  transform_1 |     at Function.Module._resolveFilename (module.js:470:15)
  </code></pre>
  </blockquote>
  
  <p>During build in Jenkins - I check permissions and they are OK:</p>
  
  <blockquote>
  <pre><code>...
  + ls -l node_modules/express/lib
  total 80
  -rw-r--r-- 1 jenkins 1001 14202 Feb 28 12:18 application.js
  -rw-r--r-- 1 jenkins 1001  1954 Feb 28 12:18 express.js
  drwxr-xr-x 2 jenkins 1001  4096 Feb 28 12:18 middleware
  ...
  </code></pre>
  </blockquote>
  
  <p>But from container - there is "<em>Permission denied</em>" error:</p>
  
  <blockquote>
  <pre><code>admin@swarm-master-EB25F3D5-0:~$ docker run -ti automation/cms-transform-layer bash
  app@3b86a8e8cc4d:~$ ls -l node_modules/express/
  ls: cannot access node_modules/express/lib: Permission denied
  ls: cannot access node_modules/express/node_modules: Permission denied
  total 124
  -rw-r--r-- 1 app app 99111 Feb 28 12:18 History.md
  -rw-r--r-- 1 app app  1249 Feb 28 12:18 LICENSE
  -rw-r--r-- 1 app app  4541 Feb 28 12:18 Readme.md
  -rw-r--r-- 1 app app   224 Feb 28 12:18 index.js
  d????????? ? ?   ?       ?            ? lib
  d????????? ? ?   ?       ?            ? node_modules
  -rw-r--r-- 1 app app  4939 Feb 28 12:18 package.json
  </code></pre>
  </blockquote>
  
  <p>What the hell is <code>"d????????? ? ?   ?       ?            ? node_modules"</code>?</p>
  
  <p>The <code>Dockerfile</code> is:</p>
  
  <pre><code>FROM node:7.5.0
  
  RUN useradd --user-group --create-home --shell /bin/false app
  
  ENV HOME=/home/app
  
  USER root
  COPY . $HOME/
  RUN chown -R app:app $HOME/*
  
  USER app
  WORKDIR $HOME
  
  CMD ["npm", "run", "start:production"]
  </code></pre>
  
  <p><strong>UPD</strong></p>
  
  <p>Related entries in <code>package.json</code>:</p>
  
  <pre><code>...
  "contentful": "^3.8.0",
  "express": "^4.14.0",
  "jsum": "^0.1.1",
  ...
  </code></pre>
  
  <p>Service definition from <code>docker-compose.yml</code>:</p>
  
  <pre><code>...
  transform:
  environment:
    - LOGZ_TOKEN=""
    - LOGZIO_API_KEY=""
    - NPM_TOKEN=""
  image: "company/transform-layer:latest"
  restart: "always"
  ports:
    - "3003:3003"
  ...
  </code></pre>
  
  <p>This is Jenkins pipeline script:</p>
  
  <pre><code>...
  def npmBuildTransform(repoUrl='1', env='2') {
      docker.image('node').inside('-v /var/run/docker.sock:/var/run/docker.sock') {
          git branch: "${BRANCH}", credentialsId: 'github', url: "${repoUrl}"
          stage('Transform build') {
             sh 'apt-get update &amp;&amp; apt-get -y install rsync'
             sh 'npm config set //registry.npmjs.org/:_authToken=$NPM_TOKEN'
             sh 'npm install --production=false'
             ...
  </code></pre>