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
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
What's Next
- Continue to 6: Summary