Make app container wait for DB
This commit is contained in:
parent
f08b986e67
commit
6e058f3d31
3 changed files with 34 additions and 0 deletions
31
wait-for-db.php
Normal file
31
wait-for-db.php
Normal file
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
$conn = mysqli_connect(
|
||||
getenv('DB_HOST'),
|
||||
getenv('DB_USERNAME'),
|
||||
getenv('DB_PASSWORD'),
|
||||
getenv('DB_DATABASE'),
|
||||
getenv('DB_PORT')
|
||||
);
|
||||
|
||||
$counter = 10;
|
||||
$count = 0;
|
||||
while (!$conn) {
|
||||
echo("Waiting for Database... $count / $counter\n");
|
||||
sleep(2);
|
||||
$conn = mysqli_connect(
|
||||
getenv('DB_HOST'),
|
||||
getenv('DB_USERNAME'),
|
||||
getenv('DB_PASSWORD'),
|
||||
getenv('DB_DATABASE'),
|
||||
getenv('DB_PORT')
|
||||
);
|
||||
$count++;
|
||||
if($count == $counter) {
|
||||
echo("Database did not respond after $counter tries, giving up\n");
|
||||
die(1);
|
||||
}
|
||||
}
|
||||
|
||||
echo "Database is up\n";
|
||||
?>
|
Loading…
Add table
Add a link
Reference in a new issue