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

posted @ 2022-03-06 12:51  RockFane  阅读(154)  评论(0编辑  收藏  举报