记录下使用docker-compose构建管理Redis、mysql。
准备工作
- 创建工作目录
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| $ mkdir -p docker docker/mysql docker/mysql/data docker/redis docker/redis/data
$ tree docker docker ├── mysql │ └── data └── redis └── data $ touch docker/mysql/my.cnf docker/redis/redis.conf $ tree docker docker ├── mysql │ ├── data │ └── my.cnf └── redis ├── data └── redis.conf
|
- 准备my.conf 、redis.conf
- my.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| [client] port = 3306 socket = /var/lib/mysql/data/mysql.sock [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' basedir = /var/lib/mysql datadir = /var/lib/mysql/data port = 3306 socket = /var/lib/mysql/data/mysql.sock lc-messages-dir = /usr/share/mysql character-set-server=utf8 back_log = 300 max_connections = 3000 max_connect_errors = 50 table_open_cache = 4096 max_allowed_packet = 32M 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 long_query_time = 6 server_id=1 innodb_buffer_pool_size = 256M innodb_thread_concurrency = 16 innodb_log_buffer_size = 16M
|
- redis.conf 可以去这里下载 官网
1 2 3 4 5 6 7 8
| wget https://raw.githubusercontent.com/redis/redis/5.0/redis.conf
daemonize no
protected-mode no requirepass root
|
编写Docker-Compose文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| version : '3' services: mysql: image: mysql/mysql-server:5.7.18 container_name: mysql5.7.18-dev networks: - net-shop-db ports: - "3306:3306" volumes: - /opt/docker/mysql/my.cnf:/etc/my.cnf - /opt/docker/mysql/data:/var/lib/mysql/data - /opt/docker/mysql/log:/var/log/mysql - /etc/localtime:/etc/localtime:ro restart: always command: [ '--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci', '--lower_case_table_names=1', '--default-time-zone=+8:00'] environment: - TZ=Asia/Shanghai - MYSQL_ROOT_PASSWORD=root redis: image: redis:5.0.5 container_name: redis5.0.6-dev networks: - net-shop-db ports: - "6379:6379" volumes: - /etc/localtime:/etc/localtime:ro - /opt/docker/redis/data:/data - /opt/docker/redis/redis.conf:/etc/redis.conf command: redis-server /etc/redis.conf environment: - TZ=Asia/Shanghai restart: always
networks: net-shop-db: driver: bridge
|
基本用法
注意需要提前启动docker
- 创建并启动容器
docker-compose -f docker-compose.yml up -d
- 批量停止容器并清除容器
docker-compose -f docker-compose.yml down
- 重新构建容器
docker-compose -f docker-compose.yml up --build -d
- 启动失败,可以查看容器日志信息获取帮助
docker logs 容器名词或容器ID # 既docker-compose.yml文件参数container_name指定的值
备注
- docker-compose 命令不存在、未找到命令。直接从github上下载,国内下载巨慢。
1 2 3 4 5 6 7 8
| cd /usr/local/bin/
curl -L https://github.com/docker/compose/releases/download/1.27.4/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
curl -L https://get.daocloud.io/docker/compose/releases/download/1.27.4/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
|
参考
docker-compose 命令不存在、未找到命令
使用 docker-compose 在 Docker 中启动有密码的 Redis 容器
Docker-Compose搭建mysql、redis、zookeeper、rabbitmq、consul、elasticsearch环境
Docker Compose多容器部署
停止、删除所有的docker容器和镜像
宿主机连接docker中的mysql
Docker Compose 安装 on centos7
Docker-Compose搭建mysql、redis、zookeeper、rabbitmq、consul、elasticsearch环境
docker-compose一键安装redis+mysql+rabbitmq