Drenai
Drenai

Reputation: 12347

Docker - CMD npm start precedes Copy all

I'm going through the Docker's official getting started guide, and there's one part that I not sure about:

At the end of the Dockerfile the CMD [ "npm", "start" ] is written before COPY . ., should it not be the other way around? The Node.js Dockerfile documentation has a different ordering

# 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 . .

Upvotes: 2

Views: 222

Answers (1)

Stefano
Stefano

Reputation: 5076

It actually does not really matter where the CMD is located. It'll remain the command execute by docker when the container is generated.

Since the content of the folder does not affect the steps inside the image, it makes sense to have it as last available step to use the cache at its best (even though it does not really affect much there).

Upvotes: 2

Related Questions