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

本文涉及的产品
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
简介: 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



目录
相关文章
|
4天前
|
SQL 关系型数据库 数据库
国产数据实战之docker部署MyWebSQL数据库管理工具
【10月更文挑战第23天】国产数据实战之docker部署MyWebSQL数据库管理工具
23 3
国产数据实战之docker部署MyWebSQL数据库管理工具
|
21天前
|
JSON JavaScript 测试技术
【Docker项目实战】使用Docker部署PPTist在线演示文稿应用
【10月更文挑战第9天】使用Docker部署PPTist在线演示文稿应用
30 1
【Docker项目实战】使用Docker部署PPTist在线演示文稿应用
|
7天前
|
消息中间件 Linux RocketMQ
在Red Hat Enterprise Linux 9上使用Docker快速安装并部署
通过以上步骤,你可以在Red Hat Enterprise Linux 9上使用Docker快速安装并部署RocketMQ。这种方法不仅简化了安装过程,还提供了一个灵活的环境来管理和扩展消息队列系统。RocketMQ作为一款高性能的分布式消息系统,通过Docker可以实现快速部署和高效管理。
23 2
|
8天前
|
消息中间件 Linux RocketMQ
在Red Hat Enterprise Linux 9上使用Docker快速安装并部署
通过以上步骤,你可以在Red Hat Enterprise Linux 9上使用Docker快速安装并部署RocketMQ。这种方法不仅简化了安装过程,还提供了一个灵活的环境来管理和扩展消息队列系统。RocketMQ作为一款高性能的分布式消息系统,通过Docker可以实现快速部署和高效管理。
17 3
|
10天前
|
关系型数据库 MySQL Linux
基于阿里云服务器Linux系统安装Docker完整图文教程(附部署开源项目)
基于阿里云服务器Linux系统安装Docker完整图文教程(附部署开源项目)
107 2
|
11天前
|
弹性计算 数据库连接 Nacos
阿里云ECS服务器在docker中部署nacos
docker pull nacos 失败,docker部署nacos遇到的问题,nacos数据库连接,nacos端口映射
61 1
|
20天前
|
Web App开发 前端开发 测试技术
【Docker项目实战】使用docker部署tabler后台模版
【10月更文挑战第10天】使用docker部署tabler后台模版
29 0
【Docker项目实战】使用docker部署tabler后台模版
|
16天前
|
运维 Kubernetes 监控
掌握Docker容器化技术:构建、部署与管理的高效实践
【10月更文挑战第14天】掌握Docker容器化技术:构建、部署与管理的高效实践
32 0
|
17天前
|
关系型数据库 数据库 PostgreSQL
在docker上部署postgresSQL主从
通过以上步骤,我们完成了在Docker环境中部署PostgreSQL主从复制的基本配置。请注意,实际生产环境中还需考虑安全性增强(如SSL加密)、监控、自动故障切换等高级配置。此外,根据具体的业务需求和规模,可能还需要考虑使用更专业的解决方案或工具,如Patroni、PgBouncer等,来进一步提升数据库集群的稳定性和效率。
22 0
|
21天前
|
监控 应用服务中间件 网络安全
部署Django应用:使用Gunicorn和Nginx构建高效的生产环境
部署Django应用:使用Gunicorn和Nginx构建高效的生产环境
74 0