docker部署若依微服务
docker-compose
下载安装
#下载docker-compose文件 下载到/usr/local/bin环境变量下面 这是1.21.1版本 也可以安装其他版本的 curl -L https://github.com/docker/compose/releases/download/1.21.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose # 因为Docker Compose存放在GitHub,可能不太稳定。可以在网盘下载上传上去/usr/local/bin/并改名docker-compose
#给他一个执行权限
chmod +x /usr/local/bin/docker-compose
#查看是否安装成功
docker-compose -version
卸载
# 卸载docker-compose
rm /usr/local/bin/docker-compose
查看若依的docker-compose.yml
打开若依项目下面的docker-compose.yml
docker-compose.yml内容
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-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 depends_on: - ruoyi-gateway links: - ruoyi-gateway ruoyi-gateway: container_name: ruoyi-gateway build: context: ./ruoyi/gateway dockerfile: dockerfile ports: - "8080:8080" depends_on: - ruoyi-redis links: - ruoyi-redis ruoyi-auth: container_name: ruoyi-auth build: context: ./ruoyi/auth dockerfile: dockerfile ports: - "9200:9200" depends_on: - ruoyi-redis links: - ruoyi-redis ruoyi-modules-system: container_name: ruoyi-modules-system build: context: ./ruoyi/modules/system dockerfile: dockerfile ports: - "9201:9201" depends_on: - ruoyi-redis - ruoyi-mysql links: - ruoyi-redis - ruoyi-mysql ruoyi-modules-gen: container_name: ruoyi-modules-gen build: context: ./ruoyi/modules/gen dockerfile: dockerfile ports: - "9202:9202" depends_on: - ruoyi-mysql links: - ruoyi-mysql ruoyi-modules-job: container_name: ruoyi-modules-job build: context: ./ruoyi/modules/job dockerfile: dockerfile ports: - "9203:9203" depends_on: - ruoyi-mysql links: - ruoyi-mysql 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"
怎么修改的docker-compose.yml
第一步:去除微服务以外的
由于mysql,nacos,redis这些我们自己单独安装过所以删除掉docker-compose.yml的这部分:
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-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 depends_on: - ruoyi-gateway links: - ruoyi-gateway
第二步:去除我不需要的微服务
由于我只需要后台管理和代码生成的,所以去除掉如下这部分:
ruoyi-modules-job: container_name: ruoyi-modules-job build: context: ./ruoyi/modules/job dockerfile: dockerfile ports: - "9203:9203" depends_on: - ruoyi-mysql links: - ruoyi-mysql 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"
第三步:去除depends_on和links
可以看到剩下的只有ruoyi-gateway,ruoyi-auth,ruoyi-modules-system,ruoyi-modules-gen几个微服务了,
这几个微服务下的depends_on和links都是mysql和redis的,由于我们的mysql和redis不是通过docker-compose.yml,是我们自己单独装的,所以直接给depends_on和links去掉了
剩余的配置也就如下了:
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"
删除不用的文件
上一节删除了docker-compose.yml中的mysql,nacos,redis等和不用的微服务配置,对应的相关文件也是需要删除的:
标红的都是要删除的,并且ruoyi文件夹里边的不用的微服务也可以删除的,其实由于docker-compose.yml已经删除了不用的微服务的配置,不删除文件也没事,但那时我们还是删除一下。
最终留下的文件如下:
打包一下微服务
生成一下我们需要用到的几个微服务的jar包放到docker/ruoyi下边的各个微服务文件夹下jar文件夹下
以auth微服务为例:
给其其余几个微服务的jar包也都生成好放到对应的文件夹下
修改dockerfile
可以看到每个微服务的文件夹下都有一个dockerfile文件,我么以auth微服务为例:
# 基础镜像 FROM openjdk:8-jre # author MAINTAINER ruoyi # 挂载目录 VOLUME /home/ruoyi # 创建目录 RUN mkdir -p /home/ruoyi # 指定路径 WORKDIR /home/ruoyi # 复制jar文件到路径 COPY ./jar/ruoyi-auth.jar /home/ruoyi/ruoyi-auth.jar # 启动认证服务 ENTRYPOINT ["java","-jar","ruoyi-auth.jar"]
可以看到有一个移动jar包的操作,但是我们由于二开改了jar包的名字,这里需要给配置里jar包改成正确的名字:
# 基础镜像 FROM openjdk:8-jre # author MAINTAINER ruoyi # 挂载目录 VOLUME /home/ruoyi # 创建目录 RUN mkdir -p /home/ruoyi # 指定路径 WORKDIR /home/ruoyi # 复制jar文件到路径 COPY ./jar/bian-auth.jar /home/ruoyi/bian-auth.jar # 启动认证服务 ENTRYPOINT ["java","-jar","bian-auth.jar"]
给其余几个微服务也同样操作
执行docker-compose
给docker文件夹复制到服务器
执行
#进入docker文件夹
cd /docker
#创建微服务镜像
docker-compose build ruoyi-gateway ruoyi-auth ruoyi-modules-system ruoyi-modules-gen
#创建容器并启用
docker-compose up -d ruoyi-gateway ruoyi-auth ruoyi-modules-system ruoyi-modules-gen
这个错误是关闭防火墙后没有重启docker服务:
systemctl restart docker
再执行一下就可以了,执行成功后:
再试下访问http://192.168.237.222:8080,发现访问不通了,再执行下docker ps发现没有启动中的容器了:
查一下全部容器:
查一下启动失败的日志:
docker logs 759f
原来是nacos那台服务器忘记开机了。。。
开机后重启一下容器再试试就可以了:
然后就是给几个容器设置为开机自启动了
sudo docker update 容器ID --restart=always
总结一下
文件目录
docker-compose.yml
version : '3.3' 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"
dockerfile
auth:
# 基础镜像 FROM openjdk:8-jre # author MAINTAINER ruoyi # 挂载目录 VOLUME /home/ruoyi # 创建目录 RUN mkdir -p /home/ruoyi # 指定路径 WORKDIR /home/ruoyi # 复制jar文件到路径 COPY ./jar/bian-auth.jar /home/ruoyi/bian-auth.jar # 启动认证服务 ENTRYPOINT ["java","-jar","bian-auth.jar"]
gateway:
# 基础镜像 FROM openjdk:8-jre # author MAINTAINER ruoyi # 挂载目录 VOLUME /home/ruoyi # 创建目录 RUN mkdir -p /home/ruoyi # 指定路径 WORKDIR /home/ruoyi # 复制jar文件到路径 COPY ./jar/bian-gateway.jar /home/ruoyi/bian-gateway.jar # 启动网关服务 ENTRYPOINT ["java","-jar","bian-gateway.jar"]
gen:
# 基础镜像 FROM openjdk:8-jre # author MAINTAINER ruoyi # 挂载目录 VOLUME /home/ruoyi # 创建目录 RUN mkdir -p /home/ruoyi # 指定路径 WORKDIR /home/ruoyi # 复制jar文件到路径 COPY ./jar/bian-modules-gen.jar /home/ruoyi/bian-modules-gen.jar # 启动代码生成服务 ENTRYPOINT ["java","-jar","bian-modules-gen.jar"]
system:
# 基础镜像 FROM openjdk:8-jre # author MAINTAINER ruoyi # 挂载目录 VOLUME /home/ruoyi # 创建目录 RUN mkdir -p /home/ruoyi # 指定路径 WORKDIR /home/ruoyi # 复制jar文件到路径 COPY ./jar/bian-modules-system.jar /home/ruoyi/bian-modules-system.jar # 启动系统服务 ENTRYPOINT ["java","-jar","bian-modules-system.jar"]