Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker php-fpm container exit with code 0 after running Symfony console commands

Tags:

php

docker

I'm learning docker and was trying to set up the environment for a Symfony project. It works well until I added the migration and fixtures creation commands to entrypoint.sh. Now the php container will exit with code 0 after running all the commands in entrypoint.sh. Can someone help me with this?

Here is my setup:

docker-compose.yaml

version: "3.7"

services:
    nginx:
        image: nginx:alpine
        ports:
            - ${NGINX_HOST_PORT}:80
        volumes:
            - ./public:/var/www/symfony/public
            - ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
        depends_on:
            - php

    php:
        build:
            context: .
            dockerfile: docker/php/Dockerfile
        volumes:
            - symfony:/var/www/symfony:delegated
        depends_on:
            - mysql
            - redis
            - composer
        environment:
            REDIS_HOST: redis
            REDIS_PORT: 6379

    redis:
        image: redis:alpine

    composer:
        image: composer:latest
        command: install
        volumes:
            - symfony:/app

    mysql:
        image: mysql:latest
        command: --default-authentication-plugin=mysql_native_password
        ports:
            - ${MYSQL_HOST_PORT}:3306
        environment:
            MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
            MYSQL_DATABASE: ${MYSQL_DATABASE}

volumes:
    symfony:
    #        driver: local
    #        driver_opts:
    #            type: none
    #            o: bind
    #            device: ${PWD}:${PWD}
        driver: local
        driver_opts:
            type: nfs
            o: addr=host.docker.internal,rw,nolock,hard,nointr,nfsvers=3
            device: ${PWD}:${PWD}

Dockerfile for php container:

FROM php:7.3-fpm-alpine

ENV APP_DEPENDENCIES \
    curl \
    vim \
    git

ENV PHP_EXTENSIONS \
    pdo_mysql

ENV PECL_EXTENSIONS \
    xdebug \
    redis

RUN apk add --no-cache ${APP_DEPENDENCIES} ${PHPIZE_DEPS} && \
    docker-php-ext-install ${PHP_EXTENSIONS} && \
    pecl install ${PECL_EXTENSIONS} && \
    docker-php-ext-enable ${PECL_EXTENSIONS}

COPY ./ /var/www/symfony
COPY docker/entrypoint.sh /usr/local/bin/entrypoint.sh

CMD ["/usr/local/bin/entrypoint.sh"]

entrypoint.sh:

#!/bin/sh

APP_PATH="/var/www/symfony"

echo "applying db migrations"
${APP_PATH}/bin/console doctrine:migrations:migrate first
${APP_PATH}/bin/console doctrine:migrations:migrate
${APP_PATH}/bin/console doctrine:fixtures:load
like image 266
yifei3212 Avatar asked Sep 19 '25 22:09

yifei3212


1 Answers

First thing, The container will die when it executes the entrypoint script as there is no process the keep the container running in entrypoint. There should always be a process that will keep running so the container will not die.

Second thing, you are not starting php-fpm in the entrypoint so as the result your container will not start PHP in the container.

update your entrypoint.

#!/bin/sh
APP_PATH="/var/www/symfony"
echo "all params $@"
echo "applying db migrations"
${APP_PATH}/bin/console doctrine:migrations:migrate first
${APP_PATH}/bin/console doctrine:migrations:migrate
${APP_PATH}/bin/console doctrine:fixtures:load
echo "starting php-fpm"
exec $@

update CMD in the Dockerfile

CMD ["/usr/local/bin/entrypoint.sh","php-fpm","-F"]

like image 138
Adiii Avatar answered Sep 22 '25 11:09

Adiii