4

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
7
  • what do you expect ? Commented Aug 29, 2019 at 3: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! Commented Aug 29, 2019 at 4: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? Commented Aug 29, 2019 at 4:22
  • how was your file before you add the entrypoint script? could you post it to the question ? Commented Aug 29, 2019 at 4:23
  • I expect that you must do something like php /my/script.php at the end of your script Commented Aug 29, 2019 at 4:25

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

@yife did this help?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.