Make app container wait for DB

pull/1/head
chris 2020-05-30 12:46:57 +02:00
parent f08b986e67
commit 6e058f3d31
3 changed files with 34 additions and 0 deletions

View File

@ -20,6 +20,7 @@ RUN set -xe;\
FROM reg.zknt.org/zknt/debian-php FROM reg.zknt.org/zknt/debian-php
COPY --from=builder /var/www /var/www COPY --from=builder /var/www /var/www
COPY entrypoint.sh /entrypoint.sh COPY entrypoint.sh /entrypoint.sh
COPY wait-for-db.php /wait-for-db.php
RUN apt-install php-curl php-zip php-bcmath php-intl php-mbstring php-xml optipng pngquant jpegoptim gifsicle ffmpeg php-imagick php-gd php-redis php-mysql &&\ RUN apt-install php-curl php-zip php-bcmath php-intl php-mbstring php-xml optipng pngquant jpegoptim gifsicle ffmpeg php-imagick php-gd php-redis php-mysql &&\
a2enmod rewrite &&\ a2enmod rewrite &&\
sed -i 's/AllowOverride None/AllowOverride All/g' /etc/apache2/apache2.conf sed -i 's/AllowOverride None/AllowOverride All/g' /etc/apache2/apache2.conf

View File

@ -3,6 +3,8 @@ set -xeo pipefail
cp -r storage.skel/* storage/ cp -r storage.skel/* storage/
php /wait-for-db.php
if [[ ! -e storage/.docker.init ]]; if [[ ! -e storage/.docker.init ]];
then then
echo "Fresh installation, initializing database..." echo "Fresh installation, initializing database..."

31
wait-for-db.php 100644
View 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";
?>