title: How to avoid npm build node.js packages every time when creating a Docker image via Circle CI?
body: <p>I deploy my Node.Js app via <code>AWS ECS Docker</code> container using <code>Circle CI</code>.</p>
  <p>However, each time I build a new image it runs <code>npm build</code> (because it's in my <code>Dockerfile</code>) and downloads and builds all the <code>node</code> modules again every time. Then it uploads a new image to the AWS ECS repository.</p>
  <p>As my environment stays the same I don't want it to build those packages every time. So do you think it is possible for Docker to actually update an existing image rather than building a new one from scratch with all the modules every time? Is this generally a good practice?</p>
  <p>I was thinking the following workflow:</p>
  <ol>
  <li>Check if there are any new Node packages compared to the previous image</li>
  <li>If yes, run <code>npm build</code></li>
  <li>If not, just keep the old <code>node_modules</code> folder, don't run build and simply update the code</li>
  <li>Deploy</li>
  </ol>
  <p>What would be the best way to do that?</p>
  <p>Here's my <code>Dockerfile</code></p>
  <pre><code>FROM node:12.18.0-alpine
  RUN mkdir -p /usr/src/app
  WORKDIR /usr/src/app
  COPY . .
  COPY package.json package-lock.json* ./
  RUN npm install
  RUN npm install pm2 -g
  EXPOSE 3000
  CMD [ &quot;pm2-runtime&quot;, &quot;ecosystem.config.js&quot;]
  </code></pre>
  <p>My <code>Circle CI</code> workflow (from the <code>./circleci/config.yml</code>):</p>
  <pre><code>workflows:
    version: 2.1
    test:
      jobs:
        - test
        - aws-ecr/build-and-push-image:
            create-repo: true
            no-output-timeout: 10m
            repo: 'stage-instance'
  </code></pre>