Docker-CMD npm启动先于Copy all



我正在阅读Docker的官方入门指南,其中有一部分我不确定:

Dockerfile的末尾,CMD [ "npm", "start" ]写在COPY . .之前,是否应该相反?Node.js Dockerfile文档有不同的排序

# Use the official image as a parent image.
FROM node:current-slim
# Set the working directory.
WORKDIR /usr/src/app
# Copy the file from your host to your current location.
COPY package.json .
# Run the command inside your image filesystem.
RUN npm install
# Inform Docker that the container is listening on the specified port at runtime.
EXPOSE 8080
# Run the specified command within the container.
CMD [ "npm", "start" ]
# Copy the rest of your app's source code from your host to your image filesystem.
COPY . .

CMD位于何处实际上并不重要。当容器生成时,它仍然是由docker执行的命令。

由于文件夹的内容不会影响图像中的步骤,因此将其作为最后一个可用步骤以最佳状态使用缓存是有意义的(尽管它对图像的影响不大(。

最新更新