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.
# This file is generated by Nx.
#
# Build the docker image with `npx nx docker-build products-api`.
# Tip: Modify "docker-build" options in project.json to change docker build args.
#
# Run the container with `docker run -p 3000:3000 -t products-api`.
FROM docker.io/node:lts-alpine
ENV HOST=0.0.0.0
ENV PORT=3000
WORKDIR /app
RUN addgroup --system products-api && \
adduser --system -G products-api products-api
COPY dist/products-api products-api
RUN chown -R products-api:products-api .
CMD [ "node", "products-api" ]
There is also an Nx target to build your Docker image.
Terminal
~/products-api❯
npx nx docker-build products-api
> nx run products-api:build
> nx run products-api:docker-build
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
————————————————————————————————————————————————————————————————————————————————————————————————————————————
> NX Successfully ran target docker-build for project products-api and 1 task it depends on (2s)
View logs and investigate cache misses at https://nx.app/runs/NrNdfzx12g
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.
{
"targets": {
"docker-build": {
"dependsOn": ["build"],
"command": "docker build -f orders-api/Dockerfile . -t orders-api"
}
}
}
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:
Terminal
~/products-api❯
npx nx g @nrwl/node:app orders-api --docker
> NX Generating @nrwl/node:application
✔ Which framework do you want to use? · express
CREATE orders-api/src/assets/.gitkeep
CREATE orders-api/src/main.ts
CREATE orders-api/tsconfig.app.json
CREATE orders-api/tsconfig.json
CREATE orders-api/project.json
CREATE orders-api/.eslintrc.json
CREATE orders-api/jest.config.ts
CREATE orders-api/tsconfig.spec.json
CREATE orders-api-e2e/project.json
CREATE orders-api-e2e/jest.config.ts
CREATE orders-api-e2e/src/orders-api/orders-api.spec.ts
CREATE orders-api-e2e/src/support/global-setup.ts
CREATE orders-api-e2e/src/support/global-teardown.ts
CREATE orders-api-e2e/src/support/test-setup.ts
CREATE orders-api-e2e/tsconfig.json
CREATE orders-api-e2e/tsconfig.spec.json
CREATE orders-api-e2e/.eslintrc.json
CREATE orders-api/Dockerfile
What's Next
- Continue to 6: Summary