Create a Docker configuration for a NestJS API

InstructorBram Borggreve

Share this video with your friends

Send Tweet

In this lesson, we add a Docker configuration to our project. In the Dockerfile we specify the different layers of our Docker image. We use a pretty standard Dockerfile and use the build and start scripts in our package.json do the actual work.

We create a shortcut called docker:build to quickly build an image.

When building the image, we see that the context being sent to the container is huge. By adding a .dockerignore file we can exclude what is being sent. We add dist and node_modules.

Other scripts we add are docker:run and docker:push.

The code of the Dockerfile can be found below:

FROM node:14-alpine

WORKDIR /workspace

COPY package.json yarn.lock /workspace/

RUN yarn

COPY . .

RUN yarn build

CMD ["yarn", "start"]