Docker-compose检查mysql连接是否准备好 [英] Docker-compose check if mysql connection is ready

查看:1517
本文介绍了Docker-compose检查mysql连接是否准备好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图确保我的应用程序容器不运行迁移/启动,直到db容器启动并准备接受连接。



所以我决定使用健康检查,并取决于docker撰写文件v2中的选项。



在应用程序中,我有以下

  app:
...
depends_on:
db:
条件:service_healthy
/ pre>

另一方面,db具有以下healthcheck

  db:
...
healthcheck:
测试:TEST_GOES_HERE
超时:20s
重试次数:10
pre>

我尝试了几种方法,如:


  1. 确保db DIR被创建

    test:[CMD,test -f var / lib / mysql / db]
    / li>
  2. 获取mysql版本:

    test:[CMD,echoSELECT version();'| mysql]

  3. Pi管理员(将数据库容器标记为健康但似乎不是有效的测试)

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

有没有人有解决方案?

解决方案

 版本:2.1
服务:
api :
build:。
container_name:api
ports:
- 8080:8080
depends_on:
db:
条件: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 ]
超时:20s
重试次数:10

api容器不会开始直到db容器健康(基本上直到mysqladmin启动并接受连接。)


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 :

  1. making sure the db DIR is created test: ["CMD", "test -f var/lib/mysql/db"]
  2. Getting the mysql version: test: ["CMD", "echo 'SELECT version();'| mysql"]
  3. 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.)

这篇关于Docker-compose检查mysql连接是否准备好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆