centos用户可将以下代码cp到shell文件执行
#!/bin/bash
<<!
判断服务是否已安装
参数 是否必须
服务名 是
返回值 1/0(是否存在)
!
function is_exist(){
ret=`$1 --version`
echo $ret
if [ -n "$ret" ]
then
return 1
else
return 0
fi
}
yum remove docker docker-common docker-selinux docker-engine
is_exist docker
if [ $? = 0 ]
then
# 更新yum
yum update -y
# 安装依赖
yum install -y yum-utils device-mapper-persistent-data lvm2
# 添加docker源
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
# 安装docker
yum install -y docker-ce
# 安装命令补全
yum install -y bash-completion
fi
systemctl start docker
systemctl enable docker
is_exist docker-compose
if [ $? = 0 ]
then
# 安装docker-compose
yum -y install epel-release
yum -y install python-pip
yum -y install python-devel
yum -y groupinstall 'Development Tools'
yum clean all && rm -rf /var/cache/yum/* && rm -rf /tmp/*
# 安装docker-compose
pip install --no-cache-dir docker-compose
fi
配置docker加速器:https://mp.csdn.net/console/editor/html/82224386
2.创建并进入工作目录 如:工作目录为/docker_dbs
mkdir /docker_dbs && cd /docker_dbs
3.编写docker-compose配置文件 创建配置文件docker-compose.yml输入以下内容
vi docker-compose.yml
version: "3"
services:
mysql:
image: mysql:${MYSQL_VERSION}
container_name: mysql_${MYSQL_VERSION}
ports:
- ${REAL_MYSQL_PORT}:3306
volumes:
- ${MYSQL_DIR}/data:/var/lib/mysql
- ${MYSQL_DIR}/my.cnf:/etc/mysql/my.cnf
- ${MYSQL_DIR}/logs:/logs
environment:
TZ: Asia/Shanghai
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_ROOT_HOST: ${MYSQL_ROOT_HOST}
logging:
driver: "json-file"
options:
max-size: "200m"
max-file: "3"
restart: always
redis:
image: redis:${REDIS_VERSION}
container_name: redis_${REDIS_VERSION}
ports:
- ${REAL_REDIS_PORT}:6379
volumes:
- ${REDIS_DIR}/data:/data
- ${REDIS_DIR}/redis.conf:/etc/redis/redis.conf
- ${REDIS_DIR}/logs:/logs
logging:
driver: "json-file"
options:
max-size: "200m"
max-file: "3"
restart: always
command: ["redis-server", "/etc/redis/redis.conf"]
4.配置docker-compose配置文件所用的变量 创建.env文件输入以下内容(可根据自己需求进行配置)
.env 的文件目录为docker-compose 同级 或者可以使用 ln 应用过来
vi .env
MYSQL_VERSION=5.7
MYSQL_DIR=./mysql
REAL_MYSQL_PORT=3306
MYSQL_ROOT_PASSWORD=123456
MYSQL_ROOT_HOST=%
REDIS_VERSION=latest
REDIS_DIR=./redis
REAL_REDIS_PORT=6379
5.创建mysql和redis所需的挂在目录及配置文件
创建挂在目录
mkdir -p {mysql,redis}/{data,logs}
编写mysql配置(可根据自己需求进行配置) 创建mysql/my.cnf进行配置(根据自己需求进行配置)
vi mysql/my.cnf
[mysqld]
default-storage-engine=INNODB
default-time-zone='+8:00'
innodb_rollback_on_timeout='ON'
max_connections=500
innodb_lock_wait_timeout=500
character-set-server=utf8mb4
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
编写mysql配置(可根据自己需求进行配置) 创建redis/redis.conf进行配置(根据自己需求进行配置)
vi redis/redis.conf
daemonize no
port 6379
bind 0.0.0.0
appendonly yes
# 检查配置是否正确
docker-compose config
# 启动服务:docker-compose up [-d 后台启动] [service 启动的服务]
# 后台启动mysql
docker-compose up -d mysql
# 后台启动redis
docker-compose up -d redis
更多操作:docker-compose -h 或查看官方文档:https://docs.docker.com/compose/reference/