2

I am working on creating a docker image with the following

FROM node:lts-alpine

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

RUN npm install --only=production

# Bundle app source
COPY . .

EXPOSE 8080
CMD [ "npm", "start"]

I am confused about the following line

# Bundle app source
COPY . .

What exactly is meant here by bundling? Copy everything? IF that is the case why is it copying the package.json file beforehand?

Cemre Mengü
  • 18,062
  • 27
  • 111
  • 169
  • 3
    This is an almost universal Docker pattern which will cause a second `docker build` to not re-run the `npm install` step if the `package.json` hasn't changed. [How to cache the RUN npm install instruction when docker build a Dockerfile](/questions/35774714/how-to-cache-the-run-npm-install-instruction-when-docker-build-a-dockerfile) has a couple of explanations of the mechanism and pattern. – David Maze Jan 16 '19 at 11:56
  • Another part of the pattern is you have to add `node_modules/` in your `.dockerignore` – Siyu Jan 16 '19 at 13:08

2 Answers2

0

I was confused about the exact same thing.

The solution is the distinction between your application and its dependencies: Running npm install after copying package.json only installs the dependencies (creating the node_modules folder), but your application code is still not inside the container. That's what COPY . . does. I don't think the word "bundle" has any special meaning here, since it is just a normal file copy operation.

Separating those steps means the results of npm install (i.e. the state of the container after executing the command) can be cached by docker and thus don't have to be executed every time a part of the application code changes. This makes deploys faster.

PS: When talking about making deploys faster, have a look at npm ci: https://blog.npmjs.org/post/171556855892/introducing-npm-ci-for-faster-more-reliable

JLubberger
  • 43
  • 7
-1

To bundle your app's source code inside the Docker image, use the COPY instruction:

SalihE
  • 1