Nodejs.org: Dockerizing a Node.js web app guide improve Dockerfile

Created on 22 Feb 2018  路  8Comments  路  Source: nodejs/nodejs.org


The Dockerfile in the Dockerizing a Node.js web app guide contains the instruction:

COPY . .

This also copies Dockerfile and .dockerignore into the image and that does not seem right to me.

Most helpful comment

It's for caching your node_modules in a separate layer, faster successive build times.

All 8 comments

Should we change .dockerignore to the following?

npm-debug.log
.dockerignore
Dockerfile

@styfle You can't seem to ignore .dockerignore and Dockerfile.

@fhemberger The documentation says you can.

You can even use the .dockerignore file to exclude the Dockerfile and .dockerignore files. These files are still sent to the daemon because it needs them to do its job. But the ADD and COPY instructions do not copy them to the image.

Tried it a few days ago, didn't work for me. But feel free to check for yourself.

For this example I would suggest to do COPY server.js . instead of COPY . .

Yeah it does seem a bit odd. Why does it have two copy steps?

COPY package*.json ./

RUN npm install
# If you are building your code for production
# RUN npm install --only=production

# Bundle app source
COPY . .

I would think the js source and the package.json could be copied in the same step.

It's for caching your node_modules in a separate layer, faster successive build times.

@fhemberger You are spot on.

About the second copy, in anything else than a simple example the source code of an application typically consists of multiple .js files which are collected in a folder namedsrc. One would do a COPY src /usr/src/app in that case for example.

I think a good project folder structure together with explicitly copying what you want/need into an image delivers the best result. Using wildcard copies will end up in a mess.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mlibby picture mlibby  路  3Comments

gbhrdt picture gbhrdt  路  4Comments

Fishrock123 picture Fishrock123  路  4Comments

luminoso picture luminoso  路  3Comments

JungMinu picture JungMinu  路  8Comments