Layer Concept and Implication to Optimize a DockerFile
FROM node:12
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
EXPOSE 80
CMD ["node", "server.js"]
Note:
npm install will run only package.json file is changed. Most of the time, it does not change.
Therefore, DO NOT put the COPY command ahead of RUN npm install. The reason COPY command will cause the RUN npm install to run again because COPY will cause package.json file apparently changed in the new layer.
Layer Concept and Implication to Optimize a DockerFile
FROM node:12
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
EXPOSE 80
CMD ["node", "server.js"]
Note: npm install will run only package.json file is changed. Most of the time, it does not change. Therefore, DO NOT put the COPY command ahead of RUN npm install. The reason COPY command will cause the RUN npm install to run again because COPY will cause package.json file apparently changed in the new layer.