用docker-compose部署mongdb的node后端服务

2020-05-19 loading

# 前提

# 核心事项

链接外部容器,需要保证当前容器和外部容器在同一个network内

创建一个network,便于其他容器使用

docker network create mongo_net

# 独立部署MongoDB

# 新建docker-compose.yml

version: '3'
services:
  mongo_db:
    image: mongo
    restart: always
    ports:
      - "27017:27017"
    volumes:
      - "~/data/mongodb/db:/data/db"
    container_name: mongo_db
    networks:
      - default
      - mongo_net
networks:
  mongo_net:
    external: true

构建容器

docker-compose up -d

# 部署node服务

# 在根目录新建Dockerfile

FROM node:12
RUN mkdir -p /home/Service
WORKDIR /home/Service  
COPY ./ /home/Service  
RUN  npm install --production
EXPOSE 3001
CMD ["node","./src/main.js"]

# 新建deploy/docker-compose.yml

version: '3'
services:
  web:
    image: heyff12/nest-test
    ports:
      - 3001:3001
    container_name: heyff12_nest_test
    networks:
      - default
      - mongo_net
    external_links:
      - mongo_db
networks:
  mongo_net:
    external: true

# 新建.github/workflows/ali.yml


name: Ali deploy CI

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

env:
  IMAGE_NAME: heyff12/nest-test
  VERSION: latest
  DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
  DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [12.10.0]
    steps:
      - uses: actions/checkout@v2
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v1
        with:
          node-version: ${{ matrix.node-version }}

      - run: npm ci
      - run: npm run build --if-present

      - name: store dist file
        uses: actions/upload-artifact@v2
        with:
          name: dist
          path: dist

      - name: store package file
        uses: actions/upload-artifact@v2
        with:
          name: package
          path: ./package*.json

      - name: store dockerfile file
        uses: actions/upload-artifact@v2
        with:
          name: dockerfile
          path: ./Dockerfile

      - name: store config file
        uses: actions/upload-artifact@v2
        with:
          name: config
          path: config

      - name: store deploy file
        uses: actions/upload-artifact@v2
        with:
          name: deploy
          path: deploy

      - name: store views file
        uses: actions/upload-artifact@v2
        with:
          name: views
          path: views

  docker-push-image:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/download-artifact@v2
        with:
          name: dist
          path: ./src
      - uses: actions/download-artifact@v2
        with:
          name: config
          path: ./config
      - uses: actions/download-artifact@v2
        with:
          name: views
          path: ./views
      - uses: actions/download-artifact@v2
        with:
          name: package
      - uses: actions/download-artifact@v2
        with:
          name: dockerfile
      - name: Build the Docker image
        run: |
          echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin
          docker build -t $IMAGE_NAME:$VERSION .
          docker push $IMAGE_NAME:$VERSION
  
  docker-run:
    needs: docker-push-image
    runs-on: ubuntu-latest
    env:
      DOCKER_REG: heyff12/nest-test
    steps:
      - uses: actions/download-artifact@v2
        with:
          name: deploy
          path: ./deploy
      - name: copy shell to remote server
        uses: alinz/ssh-scp-action@master
        with:
          host: ${{ secrets.REMOTE_HOST }}
          key: ${{ secrets.ACCESS_TOKEN }}
          port: ${{ secrets.REMOTE_PORT }}
          user: ${{ secrets.REMOTE_USER }}
          scp: |
            deploy/* ${{ secrets.REMOTE_USER }}@${{ secrets.REMOTE_HOST }}:~/deploys/nestTest
      - name: deploy on remote server
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.REMOTE_HOST }}
          username: ${{ secrets.REMOTE_USER }}
          key: ${{ secrets.ACCESS_TOKEN }}
          port: ${{ secrets.REMOTE_PORT }}
          command_timeout: 30m
          envs: DOCKER_REG
          script: |
            docker pull $DOCKER_REG
            docker image prune -f
            cd ~/deploys/nestTest
            docker-compose up -d

# 备注

通过docker启动的后端服务,在链接MongoDB容器时,需要替换MongoDB的链接:

mongodb://mongo_db/nest

mongo_db是MongoDB的容器名称

支付宝打赏
支付宝打赏
微信打赏
微信打赏