Quantcast
Channel: Bashタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 2804

mysqlとかのサービス起動を待機するBashコマンド

$
0
0

Dockerコンテナ起動は速いですが、コンテナ内のサービスが起動完了するのに時間がかかり、後続の処理を連続して実行するとDB接続エラーになる事があります。

例えば、mysqlコンテナを起動後、テストを実行したい場合に、mysqlサーバの起動が完了するのを待つ必要があります。

以下のコマンドでは、mysqlを起動後、mysqlの3306ポートをcurlで叩いて未起動の場合は 2秒待機(sleep)を繰り返します(until -; do -; done)。

# Run Mysql
docker run -d-p 3306:3306 -eMYSQL_RANDOM_ROOT_PASSWORD=yes mysql

# Wait for serviceuntil curl localhost:3306 > /dev/null 2>&1;do echo"mysql is unavailable - waiting"&&sleep 2 ;done
echo"mysql is up"# Executing Test# ....

ポートを確認しているだけなので、mysql以外にも応用できます。以下は、InfluxDBの例です。

# Run InfluxDB
docker run -d-p 8086:8086 influxdb

# Wait for serviceuntil curl localhost:8086 > /dev/null 2>&1;do echo"influxdb is unavailable - waiting"&&sleep 2 ;done
echo"influxdb is up"# Executing Test# ....

Viewing all articles
Browse latest Browse all 2804

Trending Articles