Node Standalone Tutorial - Part 5: Docker Target
Generate a Micro-service with a Docker File
Let's create a new orders-api
application with a docker file included:
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
This generator has created a basic Dockerfile for the new microservice.
# This file is generated by Nx.
#
# Build the docker image with `npx nx docker-build orders-api`.
# Tip: Modify "docker-build" options in project.json to change docker build args.
#
# Run the container with `docker run -p 3000:3000 -t orders-api`.
FROM docker.io/node:lts-alpine
ENV HOST=0.0.0.0
ENV PORT=3000
WORKDIR /app
RUN addgroup --system orders-api && \
adduser --system -G orders-api orders-api
COPY dist/orders-api orders-api
RUN chown -R orders-api:orders-api .
CMD [ "node", "orders-api" ]
You also have an Nx target to build your Docker image.
Terminal
~/products-api❯
npx nx docker-build orders-api
> nx run orders-api:build
> nx run orders-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 orders-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 orders-api
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"],
"executor": "nx:run-commands",
"options": {
"commands": ["docker build -f orders-api/Dockerfile . -t orders-api"]
}
}
}
}
What's Next
- Continue to 6: Summary