title: How to keep node_modules inside container while using docker-compose and a non-root user?
body: <p>I'm looking for a way to achieve these goals at the same time:</p>
  
  <ul>
  <li>using a <strong>non-root user</strong> inside the container</li>
  <li>keeping <code>node_modules</code> inside container (to not to "pollute" the working directory on the host)</li>
  <li>not using a <code>Dockerfile</code></li>
  </ul>
  
  <p>I'm not sure if these goals are considered "best practice". For example, keeping <code>node_modules</code> inside the container has its <a href="https://stackoverflow.com/questions/36756168/reuse-containers-with-docker-compose">disadvantages</a>. </p>
  
  <p>Currently my compose file is like this:</p>
  
  <pre><code>services:
    # ...
  
    node:
      image: "node:9"
      user: "node"
      working_dir: /home/node/app
      environment:
        # - NODE_ENV=production
        - NPM_CONFIG_PREFIX=/home/node/.npm-global
        - PATH=$PATH:/home/node/.npm-global/bin
      volumes:
        - ./proj/:/home/node/app
        - /home/node/app/node_modules # mark1
      ports:
        - "3001:3001"
      command: &gt;
        bash -c "echo hello
        &amp;&amp; ls -lh /home/node/app/ 
        &amp;&amp; npm install
        &amp;&amp; npm i -g babel-cli
        &amp;&amp; npm i -g flow-bin
        &amp;&amp; npm start"
      depends_on:
        - redis
  </code></pre>
  
  <p>but there's </p>
  
  <blockquote>
    <p><code>"Error: EACCES: permission denied, access</code>
    <code>'/home/node/app/node_modules'".</code></p>
  </blockquote>
  
  <p>If I comment out the <code>#mark1</code> line, the container runs, however <code>node_modules</code> will be written onto the host (since <code>./proj</code> is <a href="https://docs.docker.com/compose/compose-file/#volumes" rel="nofollow noreferrer">mounted</a>)</p>
  
  <p>I have read these two articles on the topic:</p>
  
  <ul>
  <li><a href="https://blog.getjaco.com/jaco-labs-nodejs-docker-missing-manual/" rel="nofollow noreferrer">https://blog.getjaco.com/jaco-labs-nodejs-docker-missing-manual/</a></li>
  <li><a href="http://jdlm.info/articles/2016/03/06/lessons-building-node-app-docker.html" rel="nofollow noreferrer">http://jdlm.info/articles/2016/03/06/lessons-building-node-app-docker.html</a></li>
  </ul>
  
  <p>but neither meets my goal.</p>
  
  <h2>Update:</h2>
  
  <p>I added a line of <code>ls -lh /home/node/app/</code> and found <code>node_modules</code> is owned by <code>root</code>. This could be the problem.</p>