Add .dockerignore for node_modules & explanation

This commit is contained in:
Dominik Mahnkopf 2020-06-02 22:07:07 +02:00
parent 35dc319a76
commit fc9025459a
1 changed files with 15 additions and 0 deletions

View File

@ -79,6 +79,21 @@ a change to the `package.json`. Make sense?
CMD ["node", "/app/src/index.js"]
```
1. Create a file named `.dockerignore` in the same folder as the Dockerfile with the following contents.
```ignore
node_modules
```
`.dockerignore` files are an easy way to selectively copy only image relevant files.
You can read more about this
[here](https://docs.docker.com/engine/reference/builder/#dockerignore-file).
In this case, the `node_modules` folder should be omitted in the second `COPY` step because otherwise,
it would possibly overwrite files which were created by the command in the `RUN` step.
For further details on why this is recommended for Node.js applications and other best practices,
have a look at their guide on
[Dockerizing a Node.js web app](https://nodejs.org/en/docs/guides/nodejs-docker-webapp/).
1. Build a new image using `docker build`.
```bash