docker-compose部署MySQL/Redis

配置文件

Compose 模板文件支持动态读取主机的系统环境变量和当前目录下的 .env 文件中的变量。

.env 文件

1
2
3
4
5
6
7
# redis
REDIS_DIR=./redis

# mysql
MYSQL_DIR=./mysql
MYSQL_ROOT_HOST=%
MYSQL_ROOT_PASSWORD=hSMZYpAGbNwVmERI

docker-compose.yaml

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
version: "3"

networks:
hyperf:
driver: bridge

services:
redis:
container_name: redis
image: redis
restart: always
volumes:
- ${REDIS_DIR}/data:/data
- ${REDIS_DIR}/redis.conf:/usr/local/etc/redis/redis.conf
ports:
- "6379:6379"
networks:
- hyperf
# command: redis-server --requirepass ZpzyQrpUclFoikLV
command: redis-server /usr/local/etc/redis/redis.conf
tty: true

mysql:
container_name: mysql
image: mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: hyperf
MYSQL_USER: hyperf
MYSQL_PASSWORD: hyperf
volumes:
# 外部数据卷
- ${MYSQL_DIR}/data:/var/lib/mysql
# 外部初始化文件,文件名必须以 .sh 或 .sql 结尾
- ${MYSQL_DIR}/init:/docker-entrypoint-initdb.d
# 外部配置文件
# - ${MYSQL_DIR}/conf.d:/etc/mysql/conf.d
ports:
- "3306:3306"
networks:
- hyperf
command:
- --character-set-server=utf8mb4
- --collation-server=utf8mb4_unicode_ci
tty: true

hyperf:
container_name: hyperf
image: hyperf/hyperf
volumes:
- ../wwwroot:/wwwroot
ports:
- "9501:9501"
- "9502:9502"
- "9200:9200"
networks:
- hyperf
# command: php /wwwroot/bin/hyperf.php start
tty: true

参考资料

  1. compose file
  2. How to Create a MySql Instance with Docker Compose
  3. 使用docker-compose配置mysql数据库并且初始化用户
坚持原创技术分享,您的支持将鼓励我继续创作!