docker检查mysql,Docker-compose检查mysql连接是否准备好

I am trying to make sure that my app container does not run migrations / start until the db container is started and READY TO accept connections.

So I decided to use the healthcheck and depends on option in docker compose file v2.

In the app, I have the following

app:

...

depends_on:

db:

condition: service_healthy

The db on the other hand has the following healthcheck

db:

...

healthcheck:

test: TEST_GOES_HERE

timeout: 20s

retries: 10

I have tried a couple of approaches like :

making sure the db DIR is created

test: ["CMD", "test -f var/lib/mysql/db"]

Getting the mysql version:

test: ["CMD", "echo 'SELECT version();'| mysql"]

Ping the admin (marks the db container as healthy but does not seem to be a valid test)

test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]

Does anyone have a solution to this?

解决方案version: "2.1"

services:

api:

build: .

container_name: api

ports:

- "8080:8080"

depends_on:

db:

condition: service_healthy

db:

container_name: db

image: mysql

ports:

- "3306"

environment:

MYSQL_ALLOW_EMPTY_PASSWORD: "yes"

MYSQL_USER: "user"

MYSQL_PASSWORD: "password"

MYSQL_DATABASE: "database"

healthcheck:

test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]

timeout: 20s

retries: 10

The api container will not start until the db container is healthy (basically until mysqladmin is up and accepting connections.)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值