title: Should Dockerfile execute "npm install" and "npm run build" or should it only copy those files over?
body: <p>I'm a little new to Docker and trying to wrap my head around some of the concepts.</p>
  <p>In a lot of tutorials and articles (actually, almost all of them), this is a typical Dockerfile for a create-react-app and nginx configuration:</p>
  <pre><code># CRA
  FROM node:alpine as build-deps
  WORKDIR /usr/src/app
  COPY package.json package-lock.json ./
  RUN npm install
  COPY . ./
  RUN npm run build
  
  # Nginx
  FROM nginx:1.12-alpine
  COPY --from=build-deps /usr/src/app/build /usr/share/nginx/html
  EXPOSE 80
  CMD [&quot;nginx&quot;, &quot;-g&quot;, &quot;daemon off;&quot;]
  </code></pre>
  <p>Assuming everything works as expected, the image would be huge.</p>
  <p>I had the idea of taking a slightly different approach. Run <code>npm install &amp;&amp; npm run build</code> locally, and then have this as the Dockerfile:</p>
  <pre><code>FROM nginx:1.12-alpine
  WORKDIR /opt/app-root
  
  COPY ./nginx/nginx.conf /etc/nginx/
  COPY ./build ./src/dist/
  
  COPY ./node_modules .
  
  USER 1001
  EXPOSE 8080
  ENTRYPOINT [&quot;nginx&quot;, &quot;-g&quot;, &quot;daemon off;&quot;]
  </code></pre>
  <p>Which approach is better? Whenever I run <code>docker build -t app-test:0.0.1 .</code>, it seems to me that the second approach is always faster.</p>