ZBD | c7746b4 | 2025-05-10 01:42:30 +0800 | [diff] [blame] | 1 | |
ZBD | f60356c | 2025-04-03 20:09:51 +0800 | [diff] [blame] | 2 | services: |
ZBD | b704c9f | 2025-05-23 21:51:39 +0800 | [diff] [blame] | 3 | # 数据库服务 |
ZBD | f60356c | 2025-04-03 20:09:51 +0800 | [diff] [blame] | 4 | mysql: |
ZBD | b704c9f | 2025-05-23 21:51:39 +0800 | [diff] [blame] | 5 | image: mysql:8.0 # 建议使用具体的版本号,而不是 latest |
6 | container_name: mysql_db # 给容器一个易于识别的名称 | ||||
ZBD | f60356c | 2025-04-03 20:09:51 +0800 | [diff] [blame] | 7 | environment: |
ZBD | b704c9f | 2025-05-23 21:51:39 +0800 | [diff] [blame] | 8 | MYSQL_DATABASE: mydatabase |
9 | MYSQL_USER: myuser | ||||
10 | MYSQL_PASSWORD: secret | ||||
11 | MYSQL_ROOT_PASSWORD: verysecret | ||||
ZBD | f60356c | 2025-04-03 20:09:51 +0800 | [diff] [blame] | 12 | ports: |
ZBD | b704c9f | 2025-05-23 21:51:39 +0800 | [diff] [blame] | 13 | - "3306:3306" # 映射数据库端口,方便本地工具连接(可选,仅测试容器连接DB时可省略) |
ZBD | c7746b4 | 2025-05-10 01:42:30 +0800 | [diff] [blame] | 14 | volumes: |
ZBD | b704c9f | 2025-05-23 21:51:39 +0800 | [diff] [blame] | 15 | - mysql-data:/var/lib/mysql # 持久化数据库数据 |
16 | # 添加健康检查,确保数据库服务真正可用 | ||||
17 | healthcheck: | ||||
18 | test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u$$MYSQL_USER", "-p$$MYSQL_PASSWORD"] | ||||
19 | interval: 10s # 每 10 秒检查一次 | ||||
20 | timeout: 5s # 检查超时时间 | ||||
21 | retries: 5 # 重试次数 | ||||
22 | start_period: 30s # 在此期间不计入重试失败次数,给数据库启动时间 | ||||
ZBD | c7746b4 | 2025-05-10 01:42:30 +0800 | [diff] [blame] | 23 | |
ZBD | b704c9f | 2025-05-23 21:51:39 +0800 | [diff] [blame] | 24 | # 应用服务(使用 Dockerfile 的 runner 阶段) |
ZBD | c7746b4 | 2025-05-10 01:42:30 +0800 | [diff] [blame] | 25 | app: |
26 | build: | ||||
ZBD | b704c9f | 2025-05-23 21:51:39 +0800 | [diff] [blame] | 27 | context: . # Dockerfile 所在的上下文路径 |
28 | dockerfile: Dockerfile # Dockerfile 文件名 | ||||
29 | target: runner # 指定构建 Dockerfile 中的 runner 阶段 | ||||
30 | container_name: spring_app | ||||
ZBD | c7746b4 | 2025-05-10 01:42:30 +0800 | [diff] [blame] | 31 | ports: |
ZBD | b704c9f | 2025-05-23 21:51:39 +0800 | [diff] [blame] | 32 | - "8080:8080" # 映射应用端口 |
ZBD | c7746b4 | 2025-05-10 01:42:30 +0800 | [diff] [blame] | 33 | environment: |
ZBD | b704c9f | 2025-05-23 21:51:39 +0800 | [diff] [blame] | 34 | # 应用连接数据库的配置,使用数据库服务的名称作为主机名 |
35 | SPRING_DATASOURCE_URL: jdbc:mysql://mysql:3306/mydatabase?serverTimezone=Asia/Shanghai&createDatabaseIfNotExist=TRUE&useSSL=FALSE&allowPublicKeyRetrieval=TRUE | ||||
ZBD | c7746b4 | 2025-05-10 01:42:30 +0800 | [diff] [blame] | 36 | SPRING_DATASOURCE_USERNAME: myuser |
37 | SPRING_DATASOURCE_PASSWORD: secret | ||||
ZBD | b704c9f | 2025-05-23 21:51:39 +0800 | [diff] [blame] | 38 | # 其他 Spring Boot 配置... |
ZBD | c7746b4 | 2025-05-10 01:42:30 +0800 | [diff] [blame] | 39 | depends_on: |
ZBD | b704c9f | 2025-05-23 21:51:39 +0800 | [diff] [blame] | 40 | mysql: |
41 | condition: service_healthy # 确保数据库服务处于健康状态才启动应用 | ||||
ZBD | c7746b4 | 2025-05-10 01:42:30 +0800 | [diff] [blame] | 42 | |
ZBD | b704c9f | 2025-05-23 21:51:39 +0800 | [diff] [blame] | 43 | |
ZBD | 4b0e05a | 2025-06-08 18:11:26 +0800 | [diff] [blame^] | 44 | |
ZBD | b704c9f | 2025-05-23 21:51:39 +0800 | [diff] [blame] | 45 | |
46 | # 定义数据卷用于持久化数据库数据 | ||||
ZBD | c7746b4 | 2025-05-10 01:42:30 +0800 | [diff] [blame] | 47 | volumes: |
48 | mysql-data: | ||||
ZBD | b704c9f | 2025-05-23 21:51:39 +0800 | [diff] [blame] | 49 |