nestjs: 添加 dockerfile

将 nestjs 放到 docker 中管理
更新于: 2023-12-27 11:02:08
nestjs - docker

创建 Dockerfile

实际中,我使用的是 node:20

# Base image
FROM node:18

# Create app directory
WORKDIR /usr/src/app

# A wildcard is used to ensure both package.json AND package-lock.json are copied
COPY package*.json ./

# Install app dependencies
RUN npm install

# Bundle app source
COPY . .

# Copy the .env and .env.development files
COPY .env .env.development ./

# Creates a "dist" folder with the production build
RUN npm run build

# Expose the port on which the app will run
EXPOSE 3001

# Start the server using the production build
CMD ["npm", "run", "start:prod"]

编译

docker build -t nestjs-app .

运行

docker run --rm -p 3001:3001 nestjs-app

优化建议

  • 缓存 node_modules(.cache),不一定有意义
  • 另外: 分离 entrypoint.sh 方便后续可以灵活的加一些脚本

参考

https://medium.com/@sujan.dumaru.official/dockerizing-nestjs-application-c4b25139fe4c