从零玩转Docker之docker-compose快捷部署中间件-dockercompose2
搭建 Nacos v2.2.2
mkdir -p /root/docker/nacos/logs
mkdir -p /root/docker/nacos/application.properties
mkdir -p /root/docker/nacos/data
docker run --name nacos-standalone-mysql -e MODE=standalone \
-e SPRING_DATASOURCE_PLATFORM=mysql \
-e MYSQL_SERVICE_HOST=xxxxx \
-e MYSQL_SERVICE_PORT=666 \
-e MYSQL_SERVICE_DB_NAME=nacos_config \
-e MYSQL_SERVICE_USER=root \
-e MYSQL_SERVICE_PASSWORD=xxxxxxx \
-v /root/docker/nacos/logs:/home/nacos/logs \
-v /root/docker/nacos/application.properties:/home/nacos/conf/application.properties \
-v /root/docker/nacos/data:/home/nacos/data \
-p 8949:8848 -d nacos/nacos-server:v2.2.2
CockerCompose
version: '3'
services:
nacos:
image: nacos/nacos-server:v2.0.4
container_name: nacos
ports:
- 8848:8848
- 9848:9848
- 9849:9849
environment:
TZ: Asia/Shanghai
MODE: standalone
SPRING_DATASOURCE_PLATFORM: mysql
MYSQL_SERVICE_HOST: 数据库ip
MYSQL_SERVICE_PORT: 3306
MYSQL_SERVICE_USER: 数据库用户
MYSQL_SERVICE_PASSWORD: 数据库密码
MYSQL_SERVICE_DB_NAME: 数据库名
MYSQL_SERVICE_DB_PARAM: characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
volumes:
- /root/docker/nacos/logs/:/home/nacos/logs
搭建Minio 分布式文件管理
Docker部署
docker run -d -p 9000:9000 -p 9001:9001 --name minio \
-e "MINIO_ROOT_USER=admin" \
-e "MINIO_ROOT_PASSWORD=admin123" \
-v /root/minio/data:/data \
-v /root/minio/config:/root/.minio \
minio/minio server --console-address ":9001" /data
DockerCompose部署
单机编排
version: '3'
services:
minio:
image: minio/minio
hostname: "minio2"
ports:
- 9010:9010 # api 端口
- 9011:9011 # 控制台端口
environment:
MINIO_ACCESS_KEY: admin #管理后台用户名
MINIO_SECRET_KEY: admin123 #管理后台密码,最小8个字符
volumes:
- /root/minio/data:/data #映射当前目录下的data目录至容器内/data目录
- /root/minio/config:/root/.minio/ #映射配置目录
command: server --console-address ':9011' /data #指定容器中的目录 /data
privileged: true
restart: always
集群编排
version: '3'
# starts 4 docker containers running minio server instances.
# using nginx reverse proxy, load balancing, you can access
# it through port 9000.
services:
minio1:
image: minio/minio
hostname: minio1
volumes:
- data1-1:/data1
- data1-2:/data2
expose:
- "9000"
- "9001"
environment:
MINIO_ROOT_USER: minio
MINIO_ROOT_PASSWORD: minio123
command: server --console-address ":9001" http://minio{1...4}/data{1...2}
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
interval: 30s
timeout: 20s
retries: 3
minio2:
image: minio/minio
hostname: minio2
volumes:
- data2-1:/data1
- data2-2:/data2
expose:
- "9000"
- "9001"
environment:
MINIO_ROOT_USER: minio
MINIO_ROOT_PASSWORD: minio123
command: server --console-address ":9001" http://minio{1...4}/data{1...2}
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
interval: 30s
timeout: 20s
retries: 3
minio3:
image: minio/minio
hostname: minio3
volumes:
- data3-1:/data1
- data3-2:/data2
expose:
- "9000"
- "9001"
environment:
MINIO_ROOT_USER: minio
MINIO_ROOT_PASSWORD: minio123
command: server --console-address ":9001" http://minio{1...4}/data{1...2}
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
interval: 30s
timeout: 20s
retries: 3
minio4:
image: minio/minio
hostname: minio4
volumes:
- data4-1:/data1
- data4-2:/data2
expose:
- "9000"
- "9001"
environment:
MINIO_ROOT_USER: minio
MINIO_ROOT_PASSWORD: minio123
command: server --console-address ":9001" http://minio{1...4}/data{1...2}
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
interval: 30s
timeout: 20s
retries: 3
nginx:
image: nginx:1.19.2-alpine
hostname: nginx
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
ports:
- "9000:9000"
- "9001:9001"
depends_on:
- minio1
- minio2
- minio3
- minio4
## By default this config uses default local driver,
## For custom volumes replace with volume driver configuration.
volumes:
data1-1:
data1-2:
data2-1:
data2-2:
data3-1:
data3-2:
data4-1:
data4-2:
搭建Redis7.x 分布式缓存
设置挂载redis配置文件地址
mkdir -p docker/redis
mkdir docker/data
- data用来存储持久化文件
- conf放置配置文件
把redis.conf放在/docker/redis目录下
redis.conf 配置
# 修改连接为所有ip
bind 0.0.0.0
# 允许外网访问
protected-mode no
port 6379
timeout 0
# RDB存储配置
save 900 1
save 300 10
save 60 10000
rdbcompression yes
dbfilename dump.rdb
# 数据存放位置
dir /data
# 开启aof配置
appendonly yes
appendfsync everysec
appendfilename "appendonly.aof"
# 设置密码
requirepass "123456"
Docker部署
sudo docker run \
-p 6379:6379 \
--name redis \
-v /docker/redis/redis.conf:/etc/redis/redis.conf \
-v /docker/redis/data:/data \
--restart=always \
-d redis:7.0.4 redis-server /etc/redis/redis.conf
docker run \
-p 6379:6379 \ docker与宿主机的端口映射
--name redis \ redis容器的名字
-v /docker/redis/redis.conf:/etc/redis/redis.conf \ 挂载redis.conf文件
-v /docker/redis/data:/data \ 挂在redis的持久化数据
--restart=always \ 设置redis容器随docker启动而自启动
-d redis:7.0.4 redis-server /etc/redis/redis.conf \ 指定redis在docker中的配置文件路径,后台启动redis
redis latest
mkdir -p /root/docker/redis/redis.conf
mkdir -p /root/docker/redis/data
docker run -p 6392:6392 --name redis-6392 \
-v /root/docker/redis/redis.conf:/etc/redis/redis.conf \
-v /root/docker/redis/data:/data \
-d redis:latest redis-server /etc/redis/redis.conf --appendonly yes
DockerCompose
version: '3'
services:
redis:
# 镜像名
image: redis:6.2.0
# 容器名
container_name: redis
# 重启策略
restart: always
# 端口映射
ports:
- 6379:6379
environment:
# 设置环境变量 时区上海 编码UTF-8
TZ: Asia/Shanghai
LANG: en_US.UTF-8
volumes:
# 配置文件
- /root/redis/conf/redis.conf:/redis.conf:rw
# 数据文件
- /root/redis/data:/data:rw
搭建RabbitMQ 消息队列
Docker 部署
创建文件夹
mkdir -p /root/docker/rabbitmq/log
mkdir -p /root/docker/rabbitmq/data
分配权限(最重要的一步)
为所有目录包括子目录分配读写权限 没有写权限无法存储 会报错无法启动
chmod -R 777 /root/docker/rabbitmq
docker run --name rabbitmq -d -p 15672:15672 -p 5672:5672 \
-v /root/docker/rabbitmq/data:/data \
-e RABBITMQ_DEFAULT_USER=admin \
-e RABBITMQ_DEFAULT_PASS=123456 rabbitmq:management
参数说明:
- -d:表示在后台运行容器;
- -p:将容器的端口 5672(应用访问端口)和 15672 (控制台Web端口号)映射到主机中;
- -e:指定环境变量:
- RABBITMQ_DEFAULT_VHOST:默认虚拟机名;
- RABBITMQ_DEFAULT_USER:默认的用户名;
- RABBITMQ_DEFAULT_PASS:默认的用户密码;
- -v 映射容器文件夹
- --hostname:指定主机名(RabbitMQ 的一个重要注意事项是它根据所谓的 节点名称 存储数据,默认为主机名);
- --name rabbitmq:设置容器名称;
- rabbitmq:容器使用的镜像名称;
设置开机自启动 (可选)
docker update rabbitmq --restart=always
启动 rabbitmq_management (RabbitMQ后台管理)
// 进入容器内部 我这里使用容器名字进入 也可以使用容器id
docker exec -it rabbitmq /bin/bash
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// 开启web后台管理界面
rabbitmq-plugins enable rabbitmq_management
延迟队列插件
注意: 插件版本最好和rabbitmq一致
进入web端后左上角有显示当前安装的RabbitMQ版本
安装完成后用工具将插件文件上传到服务器上
将刚刚上传的插件拷贝到容器内plugins目录下
docker cp /root/docker/rabbitmq/plugins/rabbitmq_delayed_message_exchange-3.11.1.ez rabbitmq:/plugins
启动延迟队列
rabbitmq-plugins enable rabbitmq_delayed_message_exchange
重新启动rabbitmq容器
docker restart rabbitmq
访问查看
Docker-Compose 部署
创建文件夹
mkdir -p /root/docker/rabbitmq/log
mkdir -p /root/docker/rabbitmq/data
分配权限(最重要的一步)
为所有目录包括子目录分配读写权限 没有写权限无法存储 会报错无法启动
chmod -R 777 /root/docker/rabbitmq
安装搭建
由于需要内置插件 这里我们使用自定义 Dockerfile 方式
FROM rabbitmq:3.11.13-management
MAINTAINER Yang BuYi
# 添加插件到指定目录 可按照此方式自行扩展其他插件
ADD ./rabbitmq_delayed_message_exchange-3.11.1.ez /plugins
# 开启插件
RUN rabbitmq-plugins enable rabbitmq_delayed_message_exchange
ENTRYPOINT ["rabbitmq-server"]
docker-compose 编排
version: '3'
services:
rabbitmq:
image: rabbitmq:3.11.13
container_name: rabbitmq
# 使用 Dockerfile 构建
build:
context: ./rabbitmq
environment:
# 控制台账号密码
RABBITMQ_DEFAULT_USER: admin
RABBITMQ_DEFAULT_PASS: 123456
ports:
- "15672:15672" # 管理界面端口
- "5672:5672" # api 端口
volumes:
- /root/docker/rabbitmq/log:/var/log/rabbitmq
- /root/docker/rabbitmq/data:/var/lib/rabbitmq
network_mode: "host"
执行安装
docker-compose up -d rabbitmq
搭建Mysql8.0
持久化地址
mkdir -p /root/mysql/data /root/mysql/init /root/mysql/conf
- data 数据存储目录 (如果mysql初始化错误 需要将文件夹内部全部删除)
- init 文件夹可以放置 .sh .sql 文件 在初始化容器时会执行
- conf 文件夹 放置 my.cnf 配置文件 (touch my.cnf) 可自定义编写mysql配置
DockerCompose部署
version: '3'
services:
mysql8.0:
# 镜像名
image: mysql:8.0.21
# 容器名(以后的控制都通过这个)
container_name: mysql8.0
# 重启策略
restart: always
environment:
# 时区上海
TZ: Asia/Shanghai
# root 密码
MYSQL_ROOT_PASSWORD: root
# 初始化数据库(后续的初始化sql会在这个库执行)
MYSQL_DATABASE: nacos_config
# 初始化用户(不能是root 会报错, 后续需要给新用户赋予权限)
MYSQL_USER: nacos
# 用户密码
MYSQL_PASSWORD: nacos
# 映射端口
ports:
- 3306:3306
volumes:
# 数据挂载
- /root/mysql/data/:/var/lib/mysql/
# 配置挂载
- /root/mysql/conf/:/etc/mysql/conf.d/
# 初始化目录挂载
- /root/mysql/init/:/docker-entrypoint-initdb.d/
command:
# 将mysql8.0默认密码策略 修改为 原先 策略 (mysql8.0对其默认策略做了更改 会导致密码无法匹配)
--default-authentication-plugin=mysql_native_password
--character-set-server=utf8mb4
--collation-server=utf8mb4_general_ci
--explicit_defaults_for_timestamp=true
--lower_case_table_names=1
执行
--compatibility 兼容性命令
docker-compose --compatibility up -d
如果果初始化失败 需要执行 停止容器 删除镜像 删除数据文件
docker-compose stop
docker-compose rm mysql8.0
rm -rf /root/mysql/data/*
Docker 部署
持久化地址
mkdir -p /data/mysql/conf
mkdir -p /data/mysql/data
mkdir -p /data/mysql/logs
- 创建my.cnf文件,放在 /data/mysql/conf 目录中,注意配置文件中的端口号、字符集、时区
vi /data/mysql/conf/my.cnf
[client]
port = 3308
default-character-set = utf8mb4
[mysql]
port = 3308
default-character-set = utf8mb4
[mysqld]
# bind-address = 0.0.0.0
# port = 3306
max_connections=10000
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
# 设置时区和字符集
# default-time-zone='+8:00'
character-set-client-handshake=FALSE
init_connect='SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci'
gtid-mode=ON
enforce-gtid-consistency = ON
docker run --restart=always --name mysql8.0
-v /data/mysql/conf:/etc/mysql -v /data/mysql/data:/var/lib/mysql
-v /data/mysql/log:/var/log -v /data/mysql/mysql-files:/var/lib/mysql-files
-p 3308:3306 -e MYSQL_ROOT_PASSWORD='123456' -d mysql:8.0
Mysql5.7
docker pull mysql:5.7
mkdir -p /data/mysql/conf
mkdir -p /data/mysql/data
mkdir -p /data/mysql/logs
docker run -d -p 3308:3306 -v /data/mysql/conf:/etc/mysql/conf.d -v /data/mysql/data:/var/lib/mysql -v /data/mysql/log:/var/log -e MYSQL_ROOT_PASSWORD=123456 --name mysql5.7 mysql:5.7
其他问题
- 解决时间显示问题
[root@yangs ~]# docker exec -it mysql8.0 bash
root@a498923c0bd3:/# date
Tue Aug 3 06:44:32 UTC 2023
root@dwdadwda:/# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
root@dwdadwda:/# exit
[root@yangs /]# docker restart mysql8.0 -- 重启镜像
[root@yangs /]# docker exec -it mysql8.0 bash
root@dwdadwda:/# date
Tue Aug 3 14:45:35 CST 2023
- 解决无法登录问题: 工具连接登录mysql
docker exec -it mysql8.0 bash
更改密码加密规则
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
刷新权限
FLUSH PRIVILEGES;
- 出现:[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 解决
不重启mysql的解决】使用 select @@global.sql_mode; 查询功能依赖检测功能。如果启用了ONLY_FULL_GROUP_BY SQL模式(默认情况下),MySQL将拒绝选择列表,HAVING条件或ORDER BY列表的查询引用在GROUP BY子句中既未命名的非集合列,也不在功能上依赖于它们。(5.7.5之前,MySQL没有检测到功能依赖关系,默认情况下不启用。
mysql> select @@global.sql_mode;
+----------------------------------------------------------------------------------------------------+
| @@global.sql_mode |
+----------------------------------------------------------------------------------------------------+
| ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION |
+----------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
去掉ONLY_FULL_GROUP_BY,重新设置值
mysql> set @@global.sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
Query OK, 0 rows affected (0.00 sec)
mysql> select @@global.sql_mode;
+----------------------------------------------------------------------------------------------------+
| @@global.sql_mode |
+----------------------------------------------------------------------------------------------------+
| STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION |
+----------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
重启mysql的解决方案 修改my.cnf文件,在[mysqld] 下方添加以下规则,并重启mysql容器。同时重启mysql连接工具、连接程序重启。
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION