Docker Compose:部署SpringBoot应用(含MySQL,Redis,Nacos,RabbitMQ,Nginx)

本文涉及的产品
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
简介: Docker Compose:部署SpringBoot应用(含MySQL,Redis,Nacos,RabbitMQ,Nginx)

目录

前言

Docker Compose安装、常用命令、模板文件

使用Docker Compose的步骤

使用Docker Compose部署SpringBoot应用

1. 运行配置要求

2. 容器化SpringBoot应用

3. SpringBoot依赖服务脚本

devecimag-docker-compose-env.yml

参数变量.env

MySQL 数据库脚本执行

1. Navicat

2. Docker命令

4. SpringBoot应用服务脚本

devecimag-docker-compose-app.yml

5. 启动docker-compose

6. 使用Docker可视化工具: Portainer查看部署结果

备注:

/mydata/mysql/data/my.cnf

/mydata/nginx/nginx.conf

异常处理

RabbitMQ: /var/lib/rabbitmq/.erlang.cookie must be accessible by owner only

MinIO: Console endpoint is listening on a dynamic port , please use --console-address



前言

阅读本文前,建议先熟悉一下文章, 了解Docker基本概念, 如何容器化Springboot应用, 如何使用idea方便快速连接Docker服务器

  1. 准备虚拟机: 在win10上使用VMware Pro16 安装 CentOS 7.9 设置静态IP 可以访问外网
  2. 安装Docker和容器化Springboot引用:第一个docker化的java应用
  3. Docker可视化工具: Portainer的安装和使用 (界面上几乎能够操作常见的Docker命令)
  4. Idea:连接远程主机SFTP服务实现文件上传和下载   (便于更新Dockerfile文件)
  5. Idea:连接Docker服务器 (便于查看镜像,容器日志等)
  6. 构建和推送镜像: docker-maven-plugin:自动构建Maven多模块的Docker镜像,并推送到Docker Registry或阿里云


Docker Compose安装、常用命令、模板文件

具体参考: https://glory.blog.csdn.net/article/details/113365376

Compose 项目是 Docker 官方的开源项目,负责实现对 Docker 容器集群的快速编排。

Compose 定位是 「定义和运行多个 Docker 容器的应用(Defining and running multi-container Docker applications)」,其前身是开源项目 Fig。

Compose 恰好满足了这样的需求。它允许用户通过一个单独的 docker-compose.yml 模板文件(YAML 格式)来定义一组相关联的应用容器为一个项目(project)。

Docker Compose是一个用于定义和运行多个docker容器应用的工具。使用Compose你可以用YAML文件来配置你的应用服务,然后使用一个命令,你就可以部署你配置的所有服务了。


使用Docker Compose的步骤

  1. 使用Dockerfile定义应用程序环境,一般需要修改初始镜像行为时才需要使用;
  2. 使用docker-compose.yml定义需要部署的应用程序服务,以便执行脚本一次性部署;
  3. 使用docker-compose up命令将所有应用服务一次性部署起来。


使用Docker Compose部署SpringBoot应用

1. 运行配置要求

CenterOS7.6版本,推荐4G以上内存。

具体搭建参考: 准备虚拟机: 在win10上使用VMware Pro16 安装 CentOS 7.9 设置静态IP 可以访问外网


2. 容器化SpringBoot应用

具体参考: 构建和推送镜像: docker-maven-plugin:自动构建Maven多模块的Docker镜像,并推送到Docker Registry或阿里云


3. SpringBoot依赖服务脚本


devecimag-docker-compose-env.yml

#用于部署运行所依赖的服务
version: '3'
services:
  # 1.MySQL数据库
  mysql:
    image: mysql:${MYSQL_VERSION}
    container_name: mysql
    command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD} #设置root帐号密码
      TZ: Asia/Shanghai
    ports:
      - "${MYSQL_HOST_PORT}:3306"
    volumes:
      - ${MYSQL_DATA_DIR}:/var/lib/mysql #数据文件挂载
      - ${MYSQL_CONF_DIR}:/etc/mysql/conf.d #配置文件挂载
      - ${MYSQL_CONF_MY_FILE}:/etc/mysql/my.cnf #配置文件挂载
      - ${MYSQL_CONF_MYSQL_FILE}:/etc/mysql/mysql.cnf #配置文件挂载
      - ${MYSQL_LOG_DIR}:/var/log/mysql #日志文件挂载
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone/timezone:/etc/timezone:ro
  # 2.Redis数据库
  redis:
    image: redis:${REDIS_VERSION}
    container_name: redis
    environment:
      TZ: Asia/Shanghai
    command: redis-server --appendonly yes
    restart: always
    volumes:
      - ${REDIS_DATA_DIR}:/data #数据文件挂载
      - ${REDIS_CONF_FILE}:/etc/redis/redis.conf #配置文件挂载
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone/timezone:/etc/timezone:ro
    ports:
      - "${REDIS_HOST_PORT}:6379"
  # 3.RabbitMQ消息队列
  rabbitmq:
    image: rabbitmq:${RABBITMQ_VERSION}
    container_name: rabbitmq
    #privileged: true
    restart: always
    environment:
      TZ: Asia/Shanghai
    volumes:
      # - ${RABBITMQ_DATA_DIR}:/var/lib/rabbitmq #数据文件挂载
      #- ${RABBITMQ_CONF_DIR}:/etc/rabbitmq #配置文件挂载
      - ${RABBITMQ_LOG_DIR}:/var/log/rabbitmq #日志文件挂载
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone/timezone:/etc/timezone:ro
    ports:
      - "${RABBITMQ_HOST_PORT}:5672"
      - "${RABBITMQ_MAG_PORT}:15672"
  # 4.Nacos注册中心和配置中心
  nacos:
    image: nacos/nacos-server:${NACOS_VERSION}
    container_name: nacos
    ports:
      - 8848:8848
    restart: always
    environment:
      - "TZ=Asia/Shanghai"
      - "PREFER_HOST_MODE=${PREFER_HOST_MODE}"
      - "MODE=${MODE}"
      - "SPRING_DATASOURCE_PLATFORM=${SPRING_DATASOURCE_PLATFORM}"
      - "MYSQL_SERVICE_HOST=${MYSQL_SERVICE_HOST}"
      - "MYSQL_SERVICE_DB_NAME=${MYSQL_SERVICE_DB_NAME}"
      - "MYSQL_SERVICE_PORT=${MYSQL_SERVICE_PORT}"
      - "MYSQL_SERVICE_USER=${MYSQL_SERVICE_USER}"
      - "MYSQL_SERVICE_PASSWORD=${MYSQL_SERVICE_PASSWORD}"
    volumes:
      - ${NACOS_LOG_DIR}:/home/nacos/logs:rw #日志文件挂载
      - ${NACOS_CONF_DIR}:/home/nacos/init.d #配置文件挂载
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone/timezone:/etc/timezone:ro
    depends_on:
      - mysql
  # 5.MinIO服务器
  # https://raw.githubusercontent.com/minio/minio/master/docs/orchestration/docker-compose/docker-compose.yaml
  minio:
    image: minio/minio:RELEASE.2021-07-30T00-02-00Z
    container_name: minio
    ports:
      - "9000:9000"
      - "9009:9009"
    restart: always
    command: server /data --console-address ":9009"
    environment:
      MINIO_ROOT_USER: minio
      MINIO_ROOT_PASSWORD: minio123 #大于等于8位
    logging:
      options:
        max-size: "50M" # 最大文件上传限制
        max-file: "10"
      driver: json-file
    volumes:
      - ${MINIO_DATA_DIR}:/data # 映射文件路径
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone/timezone:/etc/timezone:ro
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
      interval: 30s
      timeout: 20s
      retries: 3
  # 6.Nginx服务器
  nginx:
    image: nginx:${NGINX_VERSION}
    container_name: nginx
    restart: always
    ports:
      - "80:80"
      - "7930:7930"
      - "9001:9001"
      - "9002:9002"
    #定义挂载点
    volumes:
      - ${NGINX_CONF_FILE}:/etc/nginx/nginx.conf
      - ${NGINX_CONF_DIR}:/etc/nginx/conf.d
      - ${NGINX_LOG_DIR}:/var/log/nginx
      - ${NGINX_HTML_DIR}:/etc/nginx/html


参数变量.env

################################################
###   devicemag environment config file     ###
################################################
#################### MySQL #####################
MYSQL_VERSION=5.7
MYSQL_HOST_PORT=3306
MYSQL_ROOT_PASSWORD=123456
MYSQL_DATA_DIR=/mydata/mysql/data/db
MYSQL_CONF_DIR=/mydata/mysql/data/conf
MYSQL_CONF_MY_FILE=/mydata/mysql/data/my.cnf
MYSQL_CONF_MYSQL_FILE=/mydata/mysql/data/mysql.cnf
MYSQL_LOG_DIR=/mydata/mysql/log
#################### Redis #####################
REDIS_VERSION=5
REDIS_HOST_PORT=6379
REDIS_DATA_DIR=/mydata/redis/data
REDIS_CONF_FILE=/mydata/redis/conf/redis.conf
#################### RabbitMQ #####################
RABBITMQ_VERSION=3.7.15-management
RABBITMQ_HOST_PORT=5672
RABBITMQ_MAG_PORT=15672
RABBITMQ_DATA_DIR=/mydata/rabbitmq/data
RABBITMQ_CONF_DIR=/mydata/rabbitmq/conf
RABBITMQ_LOG_DIR=/mydata/rabbitmq/log
#################### NACOS #####################
NACOS_VERSION=1.2.1
PREFER_HOST_MODE=hostname
MODE=standalone
SPRING_DATASOURCE_PLATFORM=mysql
#此处要主要的是要写mysql服务的host,不能写127.0.0.1;直接写mysql即可
MYSQL_SERVICE_HOST=mysql
MYSQL_SERVICE_DB_NAME=smartmag-config
MYSQL_SERVICE_PORT=3306
MYSQL_SERVICE_USER=root
MYSQL_SERVICE_PASSWORD=123456
NACOS_LOG_DIR=/mydata/nacos/log
NACOS_CONF_DIR=/mydata/nacos/conf
#################### MINIO #####################
MINIO_DATA_DIR=/mydata/minio/data
#################### NGINX #####################
NGINX_VERSION=1.16.1-alpine
NGINX_PROXY_FILE=/mydata/nginx/proxy.conf
NGINX_CONF_FILE=/mydata/nginx/nginx.conf
NGINX_HTML_DIR=/mydata/nginx/html
NGINX_LOG_DIR=/mydata/nginx/log
NGINX_CONF_DIR=/mydata/nginx/conf.d
#################### BIGUNION #####################
BIGUNION_VERSION=1.0.0

MySQL 数据库脚本执行

1. Navicat

通过mysql客户端工具, 连接数据库执行脚本


2. Docker命令

需要创建test数据库并创建一个可以远程访问的对象reader。

将test.sql文件拷贝到mysql容器的/目录下:

docker cp /mydata/test.sql mysql:/

进入mysql容器并执行如下操作:

#进入mysql容器
docker exec -it mysql /bin/bash
#连接到mysql服务
mysql -uroot -proot --default-character-set=utf8
#创建远程访问用户
grant all privileges on *.* to 'reader' @'%' identified by '123456';
#创建mall数据库
create database testcharacter set utf8;
#使用test数据库
use test;
#导入test.sql脚本
source /test.sql;


4. SpringBoot应用服务脚本

devecimag-docker-compose-app.yml

#用于部署业务服务
version: '3'
services:
  #  认证模块
  devicemag-auth:
    image: registry.cn-beijing.aliyuncs.com/fly_jt/devicemag-auth:1.0.0
    container_name: devicemag-auth
    restart: always
    ports:
      - 8050:8050
    volumes:
      - /mydata/devicemag/log/devicemag-auth:/var
      - /etc/localtime:/etc/localtime
    environment:
      - 'TZ="Asia/Shanghai"'
#    depends_on:
#      - mysql
#      - redis
#      - nacos
    external_links:
      - redis:redis #可以用redis这个域名访问redis服务
      - mysql:db #可以用db这个域名访问mysql服务
      - nacos:nacos #可以用nacos这个域名访问nacos服务
  #  网关模块
  devicemag-gateway:
    image: registry.cn-beijing.aliyuncs.com/fly_jt/devicemag-gateway:1.0.0
    container_name: devicemag-gateway
    restart: always
    ports:
      - 8080:8080
    volumes:
      - /mydata/devicemag/log/devicemag-gateway:/var
      - /etc/localtime:/etc/localtime
    environment:
      - 'TZ="Asia/Shanghai"'
#    depends_on:
#      - mysql
#      - redis
#      - nacos
    external_links:
      - redis:redis #可以用redis这个域名访问redis服务
      - mysql:db #可以用db这个域名访问mysql服务
      - nacos:nacos #可以用nacos这个域名访问nacos服务
  # 系统模块
  devicemag-system:
    image: registry.cn-beijing.aliyuncs.com/fly_jt/devicemag-system:1.0.0
    container_name: devicemag-system
    restart: always
    ports:
      - 8070:8070
    volumes:
      - /mydata/devicemag/log/devicemag-system:/var
      - /etc/localtime:/etc/localtime
    environment:
      - 'TZ="Asia/Shanghai"'
#    depends_on:
#      - mysql
#      - redis
#      - nacos
    external_links:
      - redis:redis #可以用redis这个域名访问redis服务
      - mysql:db #可以用db这个域名访问mysql服务
      - nacos:nacos #可以用nacos这个域名访问nacos服务
  # 文件模块
  devicemag-file:
    image: registry.cn-beijing.aliyuncs.com/fly_jt/devicemag-file:1.0.0
    container_name: devicemag-file
    restart: always
    ports:
      - 8060:8060
    volumes:
      - /mydata/devicemag/log/devicemag-file:/var
      - /etc/localtime:/etc/localtime
    environment:
      - 'TZ="Asia/Shanghai"'
#    depends_on:
#      - mysql
#      - redis
#      - nacos
    external_links:
      - redis:redis #可以用redis这个域名访问redis服务
      - mysql:db #可以用db这个域名访问mysql服务
      - nacos:nacos #可以用nacos这个域名访问nacos服务
  # 核心模块
  devicemag-core:
    image: registry.cn-beijing.aliyuncs.com/fly_jt/devicemag-core:1.0.0
    container_name: devicemag-core
    restart: always
    ports:
      - 8090:8090
    volumes:
      - /mydata/devicemag/log/devicemag-core:/var
      - /etc/localtime:/etc/localtime
    environment:
      - 'TZ="Asia/Shanghai"'
#    depends_on:
#      - mysql
#      - redis
#      - rabbitmq
#      - nacos
    external_links:
      - redis:redis #可以用redis这个域名访问redis服务
      - mysql:db #可以用db这个域名访问mysql服务
      - nacos:nacos #可以用nacos这个域名访问nacos服务
      - rabbitmq:rabbit #可以用rabbit这个域名访问rabbitmq服务


5. 启动docker-compose

上传完后在当前目录下执行如下命令

docker-compose -f devecimag-docker-compose-env.yml up -d
docker-compose -f devecimag-docker-compose-app.yml up -d
[root@localhost devicemag]# docker-compose -f devecimag-docker-compose-env.yml  up -d
Building with native build. Learn about native build in Compose here: https://docs.docker.com/go/compose-native-build/
WARNING: Found orphan containers (devicemag-gateway, devicemag-core, devicemag-file, devicemag-system, devicemag-auth) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up.
Recreating redis ... 
mysql is up-to-date
rabbitmq is up-to-date
Recreating redis ... done
[root@localhost devicemag]# docker-compose -f devecimag-docker-compose-app.yml  up -d
Building with native build. Learn about native build in Compose here: https://docs.docker.com/go/compose-native-build/
WARNING: Found orphan containers (redis, nacos, mysql, rabbitmq) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up.
Recreating devicemag-system  ... done
Recreating devicemag-file    ... done
Recreating devicemag-auth    ... done
Recreating devicemag-gateway ... done
Recreating devicemag-core    ... done


6. 使用Docker可视化工具: Portainer查看部署结果

使用 Docker可视化工具: Portainer查看部署结果

使用 Docker可视化工具: Portainer查看部署结果


备注:

/mydata/mysql/data/my.cnf

# Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.0,
# as published by the Free Software Foundation.
#
# This program is also distributed with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation.  The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have included with MySQL.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License, version 2.0, for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
[mysqld]
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'


/mydata/nginx/nginx.conf

#user  nobody;
worker_processes  2;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
    #access_log  logs/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    #gzip  on;
  server {
        listen       9001;
        server_name       localhost;
        location / {
            root   /etc/nginx/html/teacher;
            index  index.html index.htm;
          }
      }
    server {
        listen       9002;
        server_name       localhost;
        location / {
                root   /etc/nginx/html/student;
                index  index.html index.htm;
                }
        }
  server {
        listen       7930;
        server_name  localhost;
    location  /{
            root   /etc/nginx/html/bigunionresource;
      autoindex on;
      add_header Access-Control-Allow-Origin *;
      add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
      add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';    
      if ($request_method = 'OPTIONS') {
        return 204;
      }
        } 
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;
    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}


异常处理

RabbitMQ: /var/lib/rabbitmq/.erlang.cookie must be accessible by owner only

解决办法参考: https://glory.blog.csdn.net/article/details/119250404


MinIO: Console endpoint is listening on a dynamic port , please use --console-address

解决办法参考: https://glory.blog.csdn.net/article/details/119250223



目录
相关文章
|
9天前
|
NoSQL 算法 Redis
docker高级篇(大厂进阶):安装redis集群
docker高级篇(大厂进阶):安装redis集群
61 24
|
1月前
|
Java Linux Docker
什么是 Docker?如何将 Spring Boot 应用程序部署到 Docker?
什么是 Docker?如何将 Spring Boot 应用程序部署到 Docker?
43 3
|
1月前
|
负载均衡 应用服务中间件 nginx
基于Nginx和Consul构建自动发现的Docker服务架构——非常之详细
通过使用Nginx和Consul构建自动发现的Docker服务架构,可以显著提高服务的可用性、扩展性和管理效率。Consul实现了服务的自动注册与发现,而Nginx则通过动态配置实现了高效的反向代理与负载均衡。这种架构非常适合需要高可用性和弹性扩展的分布式系统。
37 4
|
1月前
|
负载均衡 应用服务中间件 nginx
基于Nginx和Consul构建自动发现的Docker服务架构——非常之详细
通过使用Nginx和Consul构建自动发现的Docker服务架构,可以显著提高服务的可用性、扩展性和管理效率。Consul实现了服务的自动注册与发现,而Nginx则通过动态配置实现了高效的反向代理与负载均衡。这种架构非常适合需要高可用性和弹性扩展的分布式系统。
55 3
|
1月前
|
前端开发 Java Docker
使用Docker容器化部署Spring Boot应用程序
使用Docker容器化部署Spring Boot应用程序
|
1月前
|
Java Docker 微服务
利用Docker容器化部署Spring Boot应用
利用Docker容器化部署Spring Boot应用
51 0
|
2月前
|
Java Docker 微服务
SpringBoot微服务打包Docker镜像
SpringBoot微服务打包Docker镜像
84 11
|
2月前
|
NoSQL Linux Redis
Docker学习二(Centos):Docker安装并运行redis(成功运行)
这篇文章介绍了在CentOS系统上使用Docker安装并运行Redis数据库的详细步骤,包括拉取Redis镜像、创建挂载目录、下载配置文件、修改配置以及使用Docker命令运行Redis容器,并检查运行状态和使用Navicat连接Redis。
346 3
|
2月前
|
存储 NoSQL 大数据
大数据-51 Redis 高可用方案CAP-AP 主从复制 一主一从 全量和增量同步 哨兵模式 docker-compose测试
大数据-51 Redis 高可用方案CAP-AP 主从复制 一主一从 全量和增量同步 哨兵模式 docker-compose测试
45 3
|
2月前
|
消息中间件 NoSQL Kafka
Flink-10 Flink Java 3分钟上手 Docker容器化部署 JobManager TaskManager Kafka Redis Dockerfile docker-compose
Flink-10 Flink Java 3分钟上手 Docker容器化部署 JobManager TaskManager Kafka Redis Dockerfile docker-compose
72 4