@@ -19,6 +19,7 @@ included in the project:
1919├── package-lock.json
2020├── Dockerfile
2121├── .dockerignore
22+ ├── .npmrc
2223```
2324
2425``` js
@@ -55,6 +56,9 @@ WORKDIR /usr/src/app
5556# https://cheatsheetseries.owasp.org/cheatsheets/NodeJS_Docker_Cheat_Sheet.html#3-optimize-nodejs-tooling-for-production
5657ENV NODE_ENV production
5758COPY package*.json .
59+ # we can mount .npmrc secret file without leaving the secrets in the final built image
60+ # refer to docs https://docs.docker.com/build/building/secrets/
61+ RUN --mount=type=secret,id=npmrc_secret,target=/usr/src/app/.npmrc,required npm ci --omit=dev
5862# https://cheatsheetseries.owasp.org/cheatsheets/NodeJS_Docker_Cheat_Sheet.html#2-install-only-production-dependencies-in-the-nodejs-docker-image
5963# when NODE_ENV is set to production, npm ci automatically omits dev dependencies
6064# https://docs.npmjs.com/cli/v10/commands/npm-ci#omit
@@ -162,6 +166,34 @@ CMD [ "node", "index.js" ]
162166
163167- The process should be owned by the ` node ` user instead of ` root ` .
164168
169+ 14 . ` RUN --mount=type=secret,id=npmrc_secret,target=/usr/src/app/.npmrc,required npm ci --omit=dev `
170+
171+ - The files mounted as secrets will be available during build, but they will not
172+ remain in the final image. The secret can be any file, but npmrc is most common
173+ so we use it as an example.
174+ To be able to use the secret, we must pass it either as a param to Docker build or
175+ define it in Docker compose.
176+
177+ - Docker build example:
178+ ` docker build -t ntc-lms . --secret id=npmrc_secret,src=.npmrc `
179+
180+ - Docker compose.yaml example:
181+ ``` yaml
182+ services :
183+ app :
184+ build :
185+ context : .
186+ secrets :
187+ - npmrc_secret
188+
189+ ...
190+
191+ secrets :
192+ npmrc_secret :
193+ file : .npmrc
194+ ` ` `
195+
196+
165197## Typescript NodeJs application
166198
167199### The application
0 commit comments