nest服务端docker部署记录

298 阅读2分钟

1702264124946.png

[vue&docker&node] docker安装部署记录

之前在用koa2的时候部署过,现在学了nest.js,重新部署到lunix服务器,记录一下。

1.lunix服务器安装docker (略)

2. 编写Dockfile和workflows,实现自动部署

2.1 Dockfile

# 使用官方 Node.js 镜像作为基础镜像
FROM node:16

# 将工作目录设置为 /app
WORKDIR /app

# 将 package.json 和 package-lock.json 复制到工作目录
COPY package*.json ./

# 安装依赖
RUN npm install

# 将其余的应用代码复制到工作目录
COPY . .

# 构建应用
RUN npm run build

# 复制 .env.prod、package.jsonDockerfile 到 dist 目录
COPY .env.prod dist/
COPY package.json dist/
COPY Dockerfile dist/

# 将工作目录更改为 /app/dist
WORKDIR /app/dist

# 安装生产依赖
RUN npm install -P

# 暴露端口 13000
EXPOSE 13000

# 全局安装 PM2
RUN npm install -g pm2

# 使用 PM2 启动应用
CMD ["pm2-runtime", "--name", "nest-app", "npm", "--", "run", "start:prod"]

2.2 workflows,用于github action

# deploy.yml
name: deploy-docker
on:
  workflow_dispatch:
  push:
    branches:
      - master # 上传到此分支触发
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      # 检查代码
      - name: Checkout # 将仓库内master分支的内容下载到工作目录
        uses: actions/checkout@v3 # 脚本来自 https://github.com/actions/checkout
      - name: Setup Node.js
        uses: actions/setup-node@v3
        with:
          node-version: 16  # 可以根据需要选择 Node.js 版本

      - name: Install npm dependencies
        run: npm install
      # 登录到阿里云容器镜像服务
      - name: Login to Ali Docker
        uses: docker/login-action@v1
        # 配置登录信息,secrets 变量在 github settings -> secrets 中设置
        with:
          registry: ${{ secrets.REGISTRY_PATH }}
          username: xxxx@qq.com
          password: ${{ secrets.REGISTRY_PASSWORD }}
        # 构建镜像并上传到阿里云容器镜像仓库 (自行百度创建自己的镜像仓库)
      - name: Build and push
        id: docker_build
        uses: docker/build-push-action@v2
        with:
          context: .
          push: true
          tags: ${{ secrets.REGISTRY_PATH }}

3. 服务端

3.1 docker-compose文件

# Use root/example as user/password credentials
version: '3.1'

services:

  db:
    image: mysql
    # NOTE: use of "mysql_native_password" is not recommended: https://dev.mysql.com/doc/refman/8.0/en/upgrading-from-previous-series.html#upgrade-caching-sha2-password
    # (this is just an example, not intended to be a production configuration)
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: xxxxx
      MYSQL_DATABASE: nestjs-backend
      MYSQL_USER: xxxx
      MYSQL_PASSWORD: xxxxx
    volumes:
      - /home/mysql/db:/var/lib/mysql
    ports:
      - "12000:3306"


  adminer:
    image: adminer
    restart: always
    ports:
      - "12005:8080"

  nestjs-backend:
    image: registry.cn-hangzhou.aliyuncs.com/david-test-docker/nest-app
    restart: always
    ports:
      - "13000:13000"

  harem-vue:
    image: registry.cn-hangzhou.aliyuncs.com/david-test-docker/harem-vue
    restart: always
    ports:
      - "10080:10080"

4. 执行脚本

# deploy.sh
#!/bin/bash

# Step 1: Bring down existing containers
docker-compose down

# Step 2: Pull the latest image for nestjs-backend
docker-compose pull nestjs-backend
docker-compose pull harem-vue

# Step 3: Start the containers in the background
docker-compose up -d

# Step 4: Display the status of all containers
docker ps -a

服务端项目nestjs-backend,前端vue项目harem-vue都使用了git action自动部署到阿里云的镜像服务器。 每次代码更新后,等待自动部署完成。 然后进入服务器执行deploy.sh,停掉当前的容器并拉取nestjs-backend和harem-vue最新镜像更新,再启动容器。