1. 程式人生 > 其它 >VirtualBox Ubuntu20.04 使用 docker 內網部署 easy-mock

VirtualBox Ubuntu20.04 使用 docker 內網部署 easy-mock

一、前言

1.1 案例前置

  • 已安裝docker
  • 已安裝docker-compose

二、配置

2.1 新建專案目錄並進入

root@ubuntu:/home/rockfane# mkdir easymock && cd easymock

2.2 新建 production.json docker-compose.yml

root@ubuntu:/home/rockfane/easymock# touch production.json docker-compose.yml

2.3 修改 production.json

root@ubuntu:/home/rockfane/easymock# vim production.json
{
  "port": 7300,
  "host": "0.0.0.0",
  "pageSize": 30,
  "proxy": false,
  "db": "mongodb://rockfane:rockfanePass@mongodb/easymock",
  "unsplashClientId": "",
  "redis": {
    "keyPrefix": "[RockFane MockServer]",
    "port": 6379,
    "host": "redis",
    "password": "",
    "db": 0
  },
  "blackList": {
    "projects": [], // projectId, e.g."5a4495e16ef711102113e500"
    "ips": [] // ip, e.g. "127.0.0.1"
  },
  "rateLimit": { // https://github.com/koajs/ratelimit
    "max": 1000,
    "duration": 1000
  },
  "jwt": {
    "expire": "14 days",
    "secret": "shared-secret"
  },
  "upload": {
    "types": [".jpg", ".jpeg", ".png", ".gif", ".json", ".yml", ".yaml"],
    "size": 5242880,
    "dir": "../public/upload",
    "expire": {
      "types": [".json", ".yml", ".yaml"],
      "day": -1
    }
  },
  "ldap": {
    "server": "", // Set server to enable LDAP login. e.g. "ldap://localhost:389" or "ldaps://localhost:389"(use SSL)
    "bindDN": "", // Username,e.g. "cn=admin,dc=example,dc=com"
    "password": "",
    "filter": {
      "base": "", // Base where we can search for users,e.g. "dc=example,dc=com"
      "attributeName": "" // e.g. "mail" or "email" etc.
    }
  },
  "fe": {
    "copyright": "",
    "storageNamespace": "rockfane-mock_",
    "timeout": 25000,
    "publicPath": "/mock/"
  }
}

2.4 修改 docker-compose.yml

root@ubuntu:/home/rockfane/easymock# vim docker-compose.yml
version: '3'
services:
  redis:
    image: redis:4.0.6
    command: redis-server --appendonly yes
    volumes:
      # ./data/redis redis 資料檔案存放地址,根據需要修改為本地地址
      - './data/redis:/data'
    restart: always
    ports:
      - 6379:6379
    networks:
      - easymock-network
  mongodb:
    image: mongo:3.4.1
    environment:
      MONGO_INITDB_ROOT_USERNAME: rockfane
      MONGO_INITDB_ROOT_PASSWORD: rockfanePass
    volumes:
      # ./data/db 資料庫檔案存放地址,根據需要修改為本地地址
      - './data/db:/data/db'
    restart: always
    ports:
      - 27017:27017
    networks:
      - easymock-network
  web:
    image: easymock/easymock:latest
    command: /bin/bash -c "npm start"
    volumes:
      # 日誌地址,根據需要修改為本地地址
      - './logs:/home/easy-mock/logs'
      # 配置地址,請使用本地配置地址替換
      - './production.json:/home/easy-mock/config/production.json'
    restart: always
    ports:
      - 7300:7300
    networks:
      - easymock-network
networks:
  easymock-network:
    driver: bridge

2.5 執行 docker-compose up -d

root@ubuntu:/home/rockfane/easymock# docker-compose up -d
[+] Running 3/3
 ⠿ Container easymock-redis-1    Started                                                                           1.8s
 ⠿ Container easymock-mongodb-1  Started                                                                           2.1s
 ⠿ Container easymock-web-1      Started                                                                           2.1s

2.6 檢視 docker容器

root@ubuntu:/home/rockfane/easymock# docker ps -a
CONTAINER ID   IMAGE                      COMMAND                  CREATED              STATUS          PORTS                                           NAMES
c27acefede95   mongo:3.4.1                "/entrypoint.sh mong…"   About a minute ago   Up 58 seconds   0.0.0.0:27017->27017/tcp, :::27017->27017/tcp   easymock-mongodb-1
b438a13ee39a   redis:4.0.6                "docker-entrypoint.s…"   About a minute ago   Up 58 seconds   0.0.0.0:6379->6379/tcp, :::6379->6379/tcp       easymock-redis-1
56c6d096e1ca   easymock/easymock:latest   "/bin/bash -c 'npm s…"   About a minute ago   Up 58 seconds   0.0.0.0:7300->7300/tcp, :::7300->7300/tcp       easymock-web-1

所有容器正常執行

三、驗證

3.1 測試專案訪問<只有自己可以訪問>

http://192.168.10.2:7300

3.2 配置埠對映, 實現區域網也能訪問

需要以管理員身份開啟 PowerShell

PS C:\Users\RockFane> netsh interface portproxy add v4tov4 listenaddress=192.168.1.84 listenport=7300 connectaddress=192.168.10.2 connectport=7300 protocol=tcp
  • listenaddress 物理機ip
  • listenport 物理機埠
  • connectaddress 虛擬機器ip
  • connectport 虛擬機器埠

3.3 檢視對映列表

PS C:\Users\RockFane> netsh interface portproxy show all

偵聽 ipv4:                 連線到 ipv4:

地址            埠        地址            埠
--------------- ----------  --------------- ----------
192.168.2.1     7300        192.168.10.2    7300

3.4 測試區域網訪問<區域網可以訪問>

http://192.168.1.84:7300