Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker container exits with code 0 after running composer install

I have a docker file which looks like this:

FROM php:7.0-apache
MAINTAINER "Moritz Buettner"

RUN apt-get update && apt-get install -y libpq-dev >/dev/null 2>&1\
    && docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql >/dev/null 2>&1\
    && docker-php-ext-install pdo pdo_pgsql pgsql mbstring >/dev/null 2>&1

RUN curl -sS https://getcomposer.org/installer | php \
      && chmod +x composer.phar && mv composer.phar /usr/local/bin/composer

RUN apt-get install -y git >/dev/null 2>&1
RUN apt-get install -y zip >/dev/null 2>&1
RUN apt-get install -y unzip >/dev/null 2>&1

COPY api /var/www
RUN a2enmod rewrite && rm -r /var/www/html && ln -sf /var/www/public /var/www/html
RUN chmod -R 777 /var/www

RUN mkdir -m 777 -p /var/www/public/export-files/csv

CMD bash -c "cd /var/www && composer install --prefer-dist --no-scripts --no-autoloader"

When building the image and running the container, composer installs everything as expected, but once it finished the container exits with code 0.

Output of docker-compose up:

[... composer installing stuff ...]
inventar-api | Generating autoload files
inventar-api | > Illuminate\Foundation\ComposerScripts::postInstall
inventar-api | > php artisan optimize
inventar-api | Generating optimized class loader
inventar-api | The compiled class file has been removed.
inventar-api exited with code 0

then i tried to restart the container manually but it keeps shutting down instantly.

Do you have an suggestion on why this happens?

EDIT: Before adding composer to the docker file everything worked well.

like image 287
Moritz Büttner Avatar asked Jul 05 '18 11:07

Moritz Büttner


People also ask

What is the cause of exit code 0 for a container?

Exit code 0 indicates that the specific container does not have a foreground process attached. This exit code is the exception to all the other exit codes to follow.

Why does my docker container exit?

This happens if you run a foreground container (using docker run ), and then press Ctrl+C when the program is running. When this happens, the program will stop, and the container will exit. The container has been stopped using docker stop : You can manually stop a container using the docker stop command.

How do I stop docker compose container from exiting?

If you want to force Compose to stop and recreate all containers, use the --force-recreate flag. If the process encounters an error, the exit code for this command is 1 . If the process is interrupted using SIGINT (ctrl + C) or SIGTERM , the containers are stopped, and the exit code is 0 .


1 Answers

When the Dockerfile says

CMD bash -c "cd /var/www && composer install --prefer-dist --no-scripts --no-autoloader"

Then when you launch the container, Docker runs that command, and when that command completes, the container exits. Since it says "exit status 0", the command ran successfully.

Perhaps you mean to run composer install as a RUN step inside the Dockerfile, and have a CMD that actually launches your service.

like image 63
David Maze Avatar answered Sep 22 '22 15:09

David Maze