dmstr / docker-php-yii2

:whale: :cd: Docker PHP image containing extensions and libraries for Yii 2.0 Framework
99 stars 33 forks source link

Is there any good idea for Yii2 migration in Docker? #25

Closed krissss closed 7 years ago

krissss commented 7 years ago

I've been running Yii migrate manually, but how can I configure dockerfile or docker-compose to do it automatically? DB is another container.

krissss commented 7 years ago

I think of two ways:

schmunk42 commented 7 years ago

You can not use yii migrate in the Dockerfile since each step in the build process is completely isolated. Therefore you also do not have a running database in any step, besides the fact that it should be a separate container anyway.

We usually use a custom startup command in production, like so

command: "sh -c 'yii migrate --interactive=0 && docker-run.sh'"

Btw: with docker-compose v2 syntax you do not need to link the db container anymore.

Checkout dmstr/phd5 for a dockerized Yii2 app template with database.

krissss commented 7 years ago

I find a solution: in ENTRYPOINT like docker-entrypoint.sh

if [ ${YII_MIGRATION_DO} == 1 ]; then
    if [ ! -d "/yii2-migrat-flag" ]; then
        php yii migrate --interactive=0
        mkdir /yii2-migrat-flag
    fi
fi

you can modify ENV YII_MIGRATION_DO in Dockerfile to change is need to do that