0

I am new to Docker and trying to adapt a setup from Symfony Docker to another app that uses a different Dockerfile.

I use a docker-entrypoint to run database migrations which, of course, require the database to be up.

My Dockerfile calls docker-entrypoint but somehow in enters into a loop, running it - the entrypoint code - repeatedly.

This is the Dockerfile at /docker/php:

FROM php:8.1-apache AS symfony_php

RUN a2enmod rewrite

RUN apt-get update \
 && apt-get install -y libpq-dev libzip-dev git libxml2-dev nano wget --no-install-recommends \
 && docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql \
 && docker-php-ext-install pdo pdo_pgsql pgsql zip \
 && apt-get clean \
 && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

RUN wget https://getcomposer.org/download/2.3.5/composer.phar \
  && mv composer.phar /usr/bin/composer && chmod +x /usr/bin/composer


COPY docker/php/apache.conf /etc/apache2/sites-enabled/000-default.conf
COPY docker/php/php.ini /usr/local/etc/php/conf.d/app.ini

COPY docker/php/docker-entrypoint.sh /usr/local/bin/docker-entrypoint
RUN chmod +x /usr/local/bin/docker-entrypoint

ENV SYMFONY_PHPUNIT_VERSION=9

COPY . /var/www

WORKDIR /var/www

RUN mkdir -p var/cache var/log
RUN chmod -R 777 ./var/cache && chmod -R 777 ./var/log

RUN composer install --prefer-dist --no-progress --no-interaction

ENTRYPOINT ["docker-entrypoint"]

The entrypoint also at /docker/php

set -e

if grep -q DATABASE_URL= .env; then
    echo "Waiting for database to be ready..."
    ATTEMPTS_LEFT_TO_REACH_DATABASE=60
    until [ $ATTEMPTS_LEFT_TO_REACH_DATABASE -eq 0 ] || DATABASE_ERROR=$(php bin/console dbal:run-sql -q "SELECT 1" 2>&1); do
        if [ $? -eq 255 ]; then
            # If the Doctrine command exits with 255, an unrecoverable error occurred
            ATTEMPTS_LEFT_TO_REACH_DATABASE=0
            break
        fi
        sleep 1
        ATTEMPTS_LEFT_TO_REACH_DATABASE=$((ATTEMPTS_LEFT_TO_REACH_DATABASE - 1))
        echo "Still waiting for database to be ready... Or maybe the database is not reachable. $ATTEMPTS_LEFT_TO_REACH_DATABASE attempts left."
    done

    if [ $ATTEMPTS_LEFT_TO_REACH_DATABASE -eq 0 ]; then
        echo "The database is not up or not reachable:"
        echo "$DATABASE_ERROR"
        exit 1
    else
        echo "The database is now ready and reachable"
    fi

    if ls -A migrations/*.php >/dev/null 2>&1; then
        php bin/console doctrine:migrations:migrate --no-interaction
    fi
fi

exec docker-php-entrypoint "$@"

The resulting loop:

entrypoint loop

BernardA
  • 1,391
  • 19
  • 48
  • 1
    It loops because your container exits. What does `docker-php-entrypoint` do? – tkausl Aug 01 '22 at 13:39
  • Not sure, if anything. As said I adapted this script. I tried taken it out, but it still loops. – BernardA Aug 01 '22 at 14:49
  • `I tried taken it out, but it still loops.` What did you put in its place then? Once your script exits, the container exits. I guess you want to call the entry point of the base image after your code finishes. – tkausl Aug 01 '22 at 14:52
  • As said, I am adapting this and I do not presume to know what is going on. The original code can be found on this link, if you care to take a look https://github.com/dunglas/symfony-docker/blob/main/Dockerfile – BernardA Aug 01 '22 at 15:42

1 Answers1

0

Adding this to my entrypoint solved the issue:

/usr/sbin/apache2ctl -D FOREGROUND

It seems that the entrypoint interrupts the server process and placing the above snippet forces the server back up.

EDIT:

Even better:

Change Dockerfile as below:

ENTRYPOINT ["docker-entrypoint"]

CMD ["apachectl", "-D", "FOREGROUND"]

And on entrypoint.sh keep as per the original post:

 exec docker-php-entrypoint "$@"
BernardA
  • 1,391
  • 19
  • 48