Node Server Tutorial - Part 5: Docker Target

Using Docker

Let's take a look at the Dockerfile that was generated when we first created the repo.

1# This file is generated by Nx. 2# 3# Build the docker image with `npx nx docker-build products-api`. 4# Tip: Modify "docker-build" options in project.json to change docker build args. 5# 6# Run the container with `docker run -p 3000:3000 -t products-api`. 7FROM docker.io/node:lts-alpine 8 9ENV HOST=0.0.0.0 10ENV PORT=3000 11 12WORKDIR /app 13 14RUN addgroup --system products-api && \ 15 adduser --system -G products-api products-api 16 17COPY dist/products-api products-api 18RUN chown -R products-api:products-api . 19 20CMD [ "node", "products-api" ] 21

There is also an Nx target to build your Docker image.

~/products-api

npx nx docker-build products-api

1> nx run products-api:build 2 3 4> nx run products-api:docker-build 5 6#1 [internal] load build definition from Dockerfile 7#1 sha256:4c99d8269ea9b513bd4dc776dba71aa66d5829ea8e590b8aeb803a2067f59cd7 8#1 transferring dockerfile: 37B done 9#1 DONE 0.0s 10#2 [internal] load .dockerignore 11#2 sha256:e71d5f0270d20785d8ae5f235f0abefd0806a3001ce09bbd5fd6f34cb8b1ca81 12#2 transferring context: 2B done 13#2 DONE 0.0s 14#3 [internal] load metadata for docker.io/library/node:lts-alpine 15#3 sha256:e161ecf2e6f1cf45a4881933800e629a1213e55a987b539a70bb5826846509fd 16#3 DONE 0.2s 17#8 [1/5] FROM docker.io/library/node:lts-alpine@sha256:fda98168118e5a8f4269efca4101ee51dd5c75c0fe56d8eb6fad80455c2f5827 18#8 sha256:00cf67cfc27afade2e1236f1196ec1d784e6c26792e57b580683350c09199e48 19#8 DONE 0.0s 20#9 [internal] load build context 21#9 sha256:9103d257e071bd890d889058da8bff61c78e8bb01b7ed24337b78f75e8830218 22#9 transferring context: 1.70MB 0.1s done 23#9 DONE 0.1s 24#4 [2/5] WORKDIR /app 25#4 sha256:17db46c2fd7998a5902ae01d80def26aa254289bbab2c6fc5aacc55252ac84b0 26#4 CACHED 27#5 [3/5] RUN addgroup --system products-api && adduser --system -G products-api products-api 28#5 sha256:b44659fc59b4a2b2d6e4ab5e87ab46bcef11185d06154f4b1ec6d7a1753379f2 29#5 CACHED 30#6 [4/5] COPY dist/products-api products-api 31#6 sha256:b903d3e7efcc38acf17f87fc8de482eb267fe0269156e8862cf149cdee04c2df 32#6 CACHED 33#7 [5/5] RUN chown -R products-api:products-api . 34#7 sha256:250b9a198f6002246bab3725d205af49d6327990451320d8642f56b9882f4f0a 35#7 CACHED 36#10 exporting to image 37#10 sha256:e8c613e07b0b7ff33893b694f7759a10d42e180f2b4dc349fb57dc6b71dcab00 38#10 exporting layers done 39#10 writing image sha256:e3543d878821de18de83201719f1f333bb7072a50e42216ff5c253db9081ce71 done 40#10 naming to docker.io/library/products-api done 41#10 DONE 0.0s 42Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them 43 44 ———————————————————————————————————————————————————————————————————————————————————————————————————————————— 45 46 > NX Successfully ran target docker-build for project products-api and 1 task it depends on (2s) 47 48 View logs and investigate cache misses at https://nx.app/runs/NrNdfzx12g 49

The docker-build command is defined as a target in the root project.json file. If you need to make any modifications to the command, you can make them there. Note that this target is set up so that the build target will always be run first.

1{ 2 "targets": { 3 "docker-build": { 4 "dependsOn": ["build"], 5 "command": "docker build -f orders-api/Dockerfile . -t orders-api" 6 } 7 } 8} 9

Generate a Micro-service with a Docker File

You can also add a Dockerfile to a new node app using the --docker flag. Here we're creating an orders-api application:

~/products-api

npx nx g @nx/node:app orders-api --docker

1> NX Generating @nx/node:application 2 3✔ Which framework do you want to use? · express 4CREATE orders-api/src/assets/.gitkeep 5CREATE orders-api/src/main.ts 6CREATE orders-api/tsconfig.app.json 7CREATE orders-api/tsconfig.json 8CREATE orders-api/project.json 9CREATE orders-api/.eslintrc.json 10CREATE orders-api/jest.config.ts 11CREATE orders-api/tsconfig.spec.json 12CREATE orders-api-e2e/project.json 13CREATE orders-api-e2e/jest.config.ts 14CREATE orders-api-e2e/src/orders-api/orders-api.spec.ts 15CREATE orders-api-e2e/src/support/global-setup.ts 16CREATE orders-api-e2e/src/support/global-teardown.ts 17CREATE orders-api-e2e/src/support/test-setup.ts 18CREATE orders-api-e2e/tsconfig.json 19CREATE orders-api-e2e/tsconfig.spec.json 20CREATE orders-api-e2e/.eslintrc.json 21CREATE orders-api/Dockerfile 22
Nx 15 and lower use @nrwl/ instead of @nx/

What's Next