docker 启动每一次|关闭只能操作一个容器,而使用docker-compose 方式则可以一次性启动|关闭多个容器,非常方便!
1、安装Docker-Compose
下载docker-compose:
wget https://github.com/docker/compose/releases/download/1.25.0/docker-compose-$(uname -s)-$(uname -m) -O /usr/local/bin/docker-compose
对下载的文件添加可执行权限:
sudo chmod +x /usr/local/bin/docker-compose
创建软连接:
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
测试一下是否安装成功:
docker-compose --version
docker-compose version 1.25.0, build 0a186604
docker-py version: 4.1.0
CPython version: 3.7.4
OpenSSL version: OpenSSL 1.1.0l 10 Sep 2019
2、安装MySQL5.7、Redis
创建挂载目录
#mysql
mkdir -p /var/docker/mysql/data /var/docker/mysql/conf
#redis
mkdir -p /var/docker/redis/data /var/docker/redis/conf
创建mysql挂载的配置文件my.cnf
[client]
port = 3306
socket = /var/lib/mysql/data/mysql.sock
[mysqld]
# 针对5.7版本执行group by字句出错问题解决
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
# 一般配置选项
basedir = /var/lib/mysql
datadir = /var/lib/mysql/data
port = 3306
socket = /var/lib/mysql/data/mysql.sock
lc-messages-dir = /usr/share/mysql # 务必配置此项,否则执行sql出错时,只能显示错误代码而不显示具体错误消息
character-set-server=utf8
back_log = 300
max_connections = 3000
max_connect_errors = 50
table_open_cache = 4096
max_allowed_packet = 32M
#binlog_cache_size = 4M
max_heap_table_size = 128M
read_rnd_buffer_size = 16M
sort_buffer_size = 16M
join_buffer_size = 16M
thread_cache_size = 16
query_cache_size = 64M
query_cache_limit = 4M
ft_min_word_len = 8
thread_stack = 512K
transaction_isolation = REPEATABLE-READ
tmp_table_size = 64M
#log-bin=mysql-bin
long_query_time = 6
server_id=1
innodb_buffer_pool_size = 256M
innodb_thread_concurrency = 16
innodb_log_buffer_size = 16M
创建redis挂载的配置文件redis.conf
vi /var/docker/redis/conf/redis.conf
#内容
#bind 127.0.0.1
protected-mode no
appendonly yes
requirepass 123456
直接在登录后当前用户目录下新建,比如我是( /home/docker-compose-yml)下创建mysql-redis.yml配置文件,内容为
version: '3'
services:
mysql:
image: mysql:5.7.26 # MySQL镜像
container_name: mysql_5.7 # MySQL容器名称
# network_mode: "host" # 如果需要容器使用宿主机IP(内网IP),则可以配置此项
restart: always # 设置容器自启模式
command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci # 设置utf8字符集
environment:
MYSQL_ROOT_PASSWORD: 123456 # MySQL ROOT账号密码
volumes:
- /etc/localtime:/etc/localtime:ro # 设置容器时区与宿主机保持一致
- /var/docker/mysql/data:/var/lib/mysql #挂载 MySQL数据
- /var/docker/mysql/conf/my.cnf:/etc/mysql/my.cnf # 映射数据库配置文件\
ports:
- 3306:3306 # 端口映射
redis:
image: redis:4.0.14 # Redis镜像
container_name: redis_4.0 # Redis容器名称
restart: always
command: redis-server /usr/local/etc/redis/redis.conf --appendonly yes # 表示通过挂载配置文件追加配置
volumes:
- /etc/localtime:/etc/localtime:ro # 设置容器时区与宿主机保持一致
- /var/docker/redis/data:/data #挂载 Redis数据
- /var/docker/redis/conf/redis.conf:/usr/local/etc/redis/redis.conf #挂载 Redis配置
ports:
- 6379:6379 # 端口映射
然后运行如下命令创建MySql和Redis容器:
docker-compose -f mysql-redis.yml up -d
up
选项启动容器,-d
选项以守护模式运行,镜像如果没有,docker会先拉取镜像,有则直接创建并启动容器,注意文件运行前不能有其同名容器,还有须注意防火墙设置
如果要批量停止容器并清除容器,可以使用down命令
docker-compose -f mysql-redis.yml down
如果要重新构建容器,可以使用--build选项
docker-compose -f mysql-redis.yml up --build -d
启动好后,查看Docker容器状态:
docker ps -a | grep -E "mysql|redis"
网友评论