2

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
yifei3212
  • 821
  • 1
  • 9
  • 28
  • what do you expect ? – LinPy Aug 29 '19 at 03:57
  • 1
    Before I added the entrypoint script, the PHP container would keep running. I'm pretty new to Docker, so any information would help. Thanks! – yifei3212 Aug 29 '19 at 04:00
  • 1
    @LinPy I think my questions would be, why it keeps running before I added CMD, also why it stops running after I added them? – yifei3212 Aug 29 '19 at 04:22
  • how was your file before you add the entrypoint script? could you post it to the question ? – LinPy Aug 29 '19 at 04:23
  • I expect that you must do something like `php /my/script.php` at the end of your script – LinPy Aug 29 '19 at 04:25
  • @LinPy I just added, COPY docker/entrypoint.sh /usr/local/bin/entrypoint.sh, CMD ["/usr/local/bin/entrypoint.sh"], to the Dockerfile – yifei3212 Aug 29 '19 at 04:47
  • This is a well written question and I like your Docker file and compose set up. However the problem is a common one and there are many duplicates here on SO: [example](https://stackoverflow.com/questions/42218957/dockerfile-cmd-instruction-will-exit-the-container-just-after-running-it/42219138), [example](https://stackoverflow.com/questions/28212380/why-docker-container-exits-immediately), [example](https://stackoverflow.com/questions/54599413/php-docker-container-exits-for-no-reason). – Don't Panic Aug 29 '19 at 08:36

1 Answers1

2

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"]

Adiii
  • 54,482
  • 7
  • 145
  • 148