# docker-compose(dev)


# 前置准备

1、Docker环境的部署

资料:https://www.runoob.com/docker/ubuntu-docker-install.html (opens new window)

2、docker-compose安装

资料:https://www.runoob.com/docker/docker-compose.html (opens new window)

# 一键启动

cd ${根目录}/deploy/docker-compose

docker-compose -f docker-compose-dev.yaml up #启动

docker-compose -f docker-compose-dev.yaml up -d # 后台启动

docker-compose -f docker-compose-dev.yaml down # 停止并删除容器

docker-compose -f /docker-compose-dev.yaml stop # 停止容器

1
2
3
4
5
6
7
8
9
10

# 文件说明:docker-compose-dev.yaml

version: "3"

# 声明一个名为network的networks,subnet为network的子网地址,默认网关是177.7.0.1
networks:
  network:
    ipam:
      driver: default
      config:
        - subnet: '177.7.0.0/16' #占用的网段

services:
  web:
    image: node:latest
    container_name: gsadmin-web
    hostname: gsadmin-web           #可以通过容器名访问
    restart: always
    ports:
      - '8081:8080'
    depends_on:
      - server
    working_dir: /web       # 如果docker 设置了workdir 则此处不需要设置
    #若网络不太好,请自行换源,如下
    #command: bash -c "yarn config set registry https://registry.npm.taobao.org --global && yarn install && yarn serve"
    command: bash -c "npm install && npm run serve"
    volumes: #目录挂载
      - ../../web/scui:/web
    networks: #声明网络
      network:
        ipv4_address: 177.7.0.11

  server:
    image: golang:1.17 
    container_name: "gsadmin-server"
    hostname: gsadmin-server
    restart: always
    ports:
      - '8080:8080'
    depends_on: #依赖
      - mysql
    volumes:
      - ../../service:/service
    working_dir: /service    # 如果docker 设置了workdir 则此处不需要设置 
    command: bash -c "go env -w GOPROXY=https://goproxy.cn,direct && go mod tidy && go run main.go"
    links:
      - mysql
    networks:
      network:
        ipv4_address: 177.7.0.12

  mysql:
    image: mysql:8.0.21    # 如果您是 arm64 架构:如 MacOS 的 M1,请修改镜像为 image: mysql/mysql-server:8.0.21
    container_name: gsadmin-mysql
    hostname: gsadnub-mysql
    command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci --default-authentication-plugin=mysql_native_password
    restart: always
    ports:
      - "3306:3306"  # host物理直接映射端口为13306
    environment:
      MYSQL_ROOT_PASSWORD: '123456' # root管理员用户密码
      MYSQL_DATABASE: 'gsadmin' # 初始化启动时要创建的数据库的名称
      MYSQL_USER: 'gsadmin'
      MYSQL_PASSWORD: '123456'
    volumes:
      - /home/docker/mysql:/var/lib/mysql
    networks:
      network:
        ipv4_address: 177.7.0.13

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

# 初始化数据

  • 初始化数据 {项目目录}/service/databases/*.sql 执行里面的sql

# 其它问题

  • 前端的项目注意修改端口和容器映射端口保持一致

  • 前端反向代理位置注意修改地址

  • 服务端数据配置

  • 服务端口要和容器相同