[Docker] Create a Docker configuration for a NestJS API

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.

 

Dockerfile:

FROM node:14-alpine

WORKDIR /workspace

COPY package.json yarn.lock /workspace/

RUN yarn

COPY . .

RUN yarn build

CMD ["yarn", "start"]

 

.dockerignore:

dist
node_modules

 

scripts:

"start": "node dist/apps/api/main",
"build": "nx build api --prod",
"docker:build": "docker build . -t myapp/api",
"docker:run": "docker run -it -p 8000:3000 myapp/api",
"docker:push": "docker push myapp/api",

 

posted @ 2020-09-13 22:19  Zhentiw  阅读(198)  评论(0编辑  收藏  举报