title: docker entrypoint running in loop
body: <p>I am new to Docker and trying to adapt a setup from Symfony Docker to another app that uses a different Dockerfile.</p>
  <p>I use a docker-entrypoint to run database migrations which, of course, require the database to be up.</p>
  <p>My Dockerfile calls docker-entrypoint but somehow in enters into a loop, running it - the entrypoint code - repeatedly.</p>
  <p>This is the Dockerfile at /docker/php:</p>
  <pre><code>FROM php:8.1-apache AS symfony_php
  
  RUN a2enmod rewrite
  
  RUN apt-get update \
   &amp;&amp; apt-get install -y libpq-dev libzip-dev git libxml2-dev nano wget --no-install-recommends \
   &amp;&amp; docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql \
   &amp;&amp; docker-php-ext-install pdo pdo_pgsql pgsql zip \
   &amp;&amp; apt-get clean \
   &amp;&amp; rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
  
  RUN wget https://getcomposer.org/download/2.3.5/composer.phar \
    &amp;&amp; mv composer.phar /usr/bin/composer &amp;&amp; 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 &amp;&amp; chmod -R 777 ./var/log
  
  RUN composer install --prefer-dist --no-progress --no-interaction
  
  ENTRYPOINT [&quot;docker-entrypoint&quot;]
  </code></pre>
  <p>The entrypoint also at /docker/php</p>
  <pre><code>set -e
  
  if grep -q DATABASE_URL= .env; then
      echo &quot;Waiting for database to be ready...&quot;
      ATTEMPTS_LEFT_TO_REACH_DATABASE=60
      until [ $ATTEMPTS_LEFT_TO_REACH_DATABASE -eq 0 ] || DATABASE_ERROR=$(php bin/console dbal:run-sql -q &quot;SELECT 1&quot; 2&gt;&amp;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 &quot;Still waiting for database to be ready... Or maybe the database is not reachable. $ATTEMPTS_LEFT_TO_REACH_DATABASE attempts left.&quot;
      done
  
      if [ $ATTEMPTS_LEFT_TO_REACH_DATABASE -eq 0 ]; then
          echo &quot;The database is not up or not reachable:&quot;
          echo &quot;$DATABASE_ERROR&quot;
          exit 1
      else
          echo &quot;The database is now ready and reachable&quot;
      fi
  
      if ls -A migrations/*.php &gt;/dev/null 2&gt;&amp;1; then
          php bin/console doctrine:migrations:migrate --no-interaction
      fi
  fi
  
  exec docker-php-entrypoint &quot;$@&quot;
  </code></pre>
  <p>The resulting loop:</p>
  <p><a href="https://i.stack.imgur.com/LftiH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LftiH.png" alt="entrypoint loop" /></a></p>