因为PWD的docker挂载的空间比较小内存以及cpu也比较有限,虽然可以启动起来但是会因为cpu不够系统自动随机关闭占用资源比较大的docker服务,所以在搭建的时候我是把项目分成了三大块分别使用三个服务器。
后端一个服务器。前端一个服务器。nacos,mysql,redis一个服务器 启动顺序是nacos,mysql,redis最先启动,再启动后端最后启动前端
结构如下
copy.sh与docker-compose.yml做如下修改
#!/bin/sh
# 复制项目的文件到对应docker路径,便于一键生成镜像。
usage() {
echo "Usage: sh copy.sh"
exit 1
}
# copy sql
echo "begin copy sql "
cp sql/ry_20220613.sql mysql/db
cp sql/ry_config_20220510.sql mysql/db
上面的copy脚本需要主要就是sql文件夹下的sql脚本文件名是否一致
version : '3.8'
services:
ruoyi-nacos:
container_name: ruoyi-nacos
image: nacos/nacos-server
build:
context: ./nacos
environment:
- MODE=standalone
volumes:
- ./nacos/logs/:/home/nacos/logs
- ./nacos/conf/application.properties:/home/nacos/conf/application.properties
ports:
- "8848:8848"
- "9848:9848"
- "9849:9849"
depends_on:
- ruoyi-mysql
ruoyi-mysql:
container_name: ruoyi-mysql
image: mysql:5.7
build:
context: ./mysql
ports:
- "3306:3306"
volumes:
- ./mysql/conf:/etc/mysql/conf.d
- ./mysql/logs:/logs
- ./mysql/data:/var/lib/mysql
command: [
'mysqld',
'--innodb-buffer-pool-size=80M',
'--character-set-server=utf8mb4',
'--collation-server=utf8mb4_unicode_ci',
'--default-time-zone=+8:00',
'--lower-case-table-names=1'
]
environment:
MYSQL_DATABASE: 'ry-cloud'
MYSQL_ROOT_PASSWORD: password
ruoyi-redis:
container_name: ruoyi-redis
image: redis
build:
context: ./redis
ports:
- "6379:6379"
volumes:
- ./redis/conf/redis.conf:/home/ruoyi/redis/redis.conf
- ./redis/data:/data
command: redis-server /home/ruoyi/redis/redis.conf
ruoyi-adminer:
image: adminer
restart: always
ports:
- 8080:8080
后端结构如下
在这里自己做了个build.sh脚本用来直接在服务器上生成jar包
#!/bin/sh
echo "初始化脚本"
#echo "下载源代码"
git clone https://xxxxxx.git拉取代码的位置
echo "打包jar"
cd /root/ruoyi-back-end
docker run -it --rm --name my-maven-project -v "$(pwd)":/root/ruoyi-back-end -w /root/ruoyi-back-end maven:3.3-jdk-8 mvn clean install
cd /root/ruoyi-back-end/docker
echo "给相关脚本赋予权限"
chmod 777 copy.sh
chmod 777 deploy.sh
echo "执行复制脚本"
./copy.sh
docker-compose up
#!/bin/sh
# copy jar
echo "begin copy ruoyi-gateway "
cp ../ruoyi-gateway/target/ruoyi-gateway.jar ./ruoyi/gateway/jar
echo "begin copy ruoyi-auth "
cp ../ruoyi-auth/target/ruoyi-auth.jar ./ruoyi/auth/jar
echo "begin copy ruoyi-visual "
cp ../ruoyi-visual/ruoyi-monitor/target/ruoyi-visual-monitor.jar ./ruoyi/visual/monitor/jar
echo "begin copy ruoyi-modules-system "
cp ../ruoyi-modules/ruoyi-system/target/ruoyi-modules-system.jar ./ruoyi/modules/system/jar
echo "begin copy ruoyi-modules-file "
cp ../ruoyi-modules/ruoyi-file/target/ruoyi-modules-file.jar ./ruoyi/modules/file/jar
echo "begin copy ruoyi-modules-job "
cp ../ruoyi-modules/ruoyi-job/target/ruoyi-modules-job.jar ./ruoyi/modules/job/jar
echo "begin copy ruoyi-modules-gen "
cp ../ruoyi-modules/ruoyi-gen/target/ruoyi-modules-gen.jar ./ruoyi/modules/gen/jar
version : '3.8'
services:
ruoyi-gateway:
container_name: ruoyi-gateway
build:
context: ./ruoyi/gateway
dockerfile: dockerfile
ports:
- "8080:8080"
ruoyi-auth:
container_name: ruoyi-auth
build:
context: ./ruoyi/auth
dockerfile: dockerfile
ports:
- "9200:9200"
ruoyi-modules-system:
container_name: ruoyi-modules-system
build:
context: ./ruoyi/modules/system
dockerfile: dockerfile
ports:
- "9201:9201"
ruoyi-modules-gen:
container_name: ruoyi-modules-gen
build:
context: ./ruoyi/modules/gen
dockerfile: dockerfile
ports:
- "9202:9202"
ruoyi-modules-job:
container_name: ruoyi-modules-job
build:
context: ./ruoyi/modules/job
dockerfile: dockerfile
ports:
- "9203:9203"
ruoyi-modules-file:
container_name: ruoyi-modules-file
build:
context: ./ruoyi/modules/file
dockerfile: dockerfile
ports:
- "9300:9300"
volumes:
- ./ruoyi/uploadPath:/home/ruoyi/uploadPath
ruoyi-visual-monitor:
container_name: ruoyi-visual-monitor
build:
context: ./ruoyi/visual/monitor
dockerfile: dockerfile
ports:
- "9100:9100"
还需要对代码里面的bootstrap.yml做一个批量替换 就是把服务注册还有配置中心的地址换成上面启动好的地址。
接下来就是前端了
与后端一样也做了个build.sh来直接打包
#!/bin/sh
echo "初始化脚本"
#echo "下载源代码"
git clone git clone https://xxxxxx.git拉取代码的位置
echo "前端UI"
cd /root/ruoyi-front-end
docker run -it --rm --name my-running-script -v "$(pwd)":/root/ruoyi-front-end -w /root/ruoyi-front-end node:12 npm install
docker run -it --rm --name my-running-script -v "$(pwd)":/root/ruoyi-front-end -w /root/ruoyi-front-end node:12 npm install html-webpack-plugin
docker run -it --rm --name my-running-script -v "$(pwd)":/root/ruoyi-front-end -w /root/ruoyi-front-end node:12 npm run build:prod
cd /root/ruoyi-front-end/docker
echo "给相关脚本赋予权限"
chmod 777 copy.sh
chmod 777 deploy.sh
echo "执行复制脚本"
./copy.sh
docker-compose up
#!/bin/sh
# copy html
echo "begin copy html "
cp -r ../dist/** ./nginx/html/dist
version : '3.8'
services:
ruoyi-nginx:
container_name: ruoyi-nginx
image: nginx
build:
context: ./nginx
ports:
- "80:80"
volumes:
- ./nginx/html/dist:/home/ruoyi/projects/ruoyi-ui
- ./nginx/conf/nginx.conf:/etc/nginx/nginx.conf
- ./nginx/logs:/var/log/nginx
- ./nginx/conf.d:/etc/nginx/conf.d
与后端一样,前端也需要修改连接后端的地址 vue.config.js,nginx.conf 把带8080的都改成后端的ip地址