---
title: 'Node Server Tutorial - Part 5: Docker Target'
description: In this tutorial you'll create a backend-focused workspace with Nx.
---

# 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.

```{% filename="/Dockerfile" %}
# 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.

```{% command="npx nx docker-build products-api" path="~/products-api" %}
> nx run products-api:build


> nx run products-api:docker-build

#1 [internal] load build definition from Dockerfile
#1 sha256:4c99d8269ea9b513bd4dc776dba71aa66d5829ea8e590b8aeb803a2067f59cd7
#1 transferring dockerfile: 37B done
#1 DONE 0.0s
#2 [internal] load .dockerignore
#2 sha256:e71d5f0270d20785d8ae5f235f0abefd0806a3001ce09bbd5fd6f34cb8b1ca81
#2 transferring context: 2B done
#2 DONE 0.0s
#3 [internal] load metadata for docker.io/library/node:lts-alpine
#3 sha256:e161ecf2e6f1cf45a4881933800e629a1213e55a987b539a70bb5826846509fd
#3 DONE 0.2s
#8 [1/5] FROM docker.io/library/node:lts-alpine@sha256:fda98168118e5a8f4269efca4101ee51dd5c75c0fe56d8eb6fad80455c2f5827
#8 sha256:00cf67cfc27afade2e1236f1196ec1d784e6c26792e57b580683350c09199e48
#8 DONE 0.0s
#9 [internal] load build context
#9 sha256:9103d257e071bd890d889058da8bff61c78e8bb01b7ed24337b78f75e8830218
#9 transferring context: 1.70MB 0.1s done
#9 DONE 0.1s
#4 [2/5] WORKDIR /app
#4 sha256:17db46c2fd7998a5902ae01d80def26aa254289bbab2c6fc5aacc55252ac84b0
#4 CACHED
#5 [3/5] RUN addgroup --system products-api &&           adduser --system -G products-api products-api
#5 sha256:b44659fc59b4a2b2d6e4ab5e87ab46bcef11185d06154f4b1ec6d7a1753379f2
#5 CACHED
#6 [4/5] COPY dist/products-api products-api
#6 sha256:b903d3e7efcc38acf17f87fc8de482eb267fe0269156e8862cf149cdee04c2df
#6 CACHED
#7 [5/5] RUN chown -R products-api:products-api .
#7 sha256:250b9a198f6002246bab3725d205af49d6327990451320d8642f56b9882f4f0a
#7 CACHED
#10 exporting to image
#10 sha256:e8c613e07b0b7ff33893b694f7759a10d42e180f2b4dc349fb57dc6b71dcab00
#10 exporting layers done
#10 writing image sha256:e3543d878821de18de83201719f1f333bb7072a50e42216ff5c253db9081ce71 done
#10 naming to docker.io/library/products-api done
#10 DONE 0.0s
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.

```json {% filename="/project.json" %}
{
  "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:

```{% command="npx nx g @nx/node:app orders-api --docker" path="~/products-api" %}
>  NX  Generating @nx/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](/node-server-tutorial/6-summary)
