Where to put the php artisan migrate command Where to put the php artisan migrate command docker docker

Where to put the php artisan migrate command


This is how I solved it .Created a bash script called run.sh and added the php artisan migrations commands followed by the php serve command.

run.sh

#!/bin/shcd /app  php artisan migrate:fresh --seedphp artisan serve --host=0.0.0.0 --port=$APP_PORT

Added entrypoint to the Dockerfile removing the CMD in the end which will run the commands desired.

copy ./run.sh /tmp    ENTRYPOINT ["/tmp/run.sh"]

Remove the command from the docker-compose.yml


Yes, special script.I try to build deploy and testing throw docker-compose, so i run migrations in script before starting supervisor in docker-service "jobs":

#!/bin/shcd /var/wwwphp artisan migrate --seed/usr/bin/supervisord -n -c /etc/supervisord.conf

And piece from my deploy-docker-compose.yml:

services:    nginx:        depends_on:            - phpfpm ## NOT START BEFORE PHPFPM    phpfpm:        depends_on:             - jobs ## NOT START BEFORE MIGRATION    jobs:        # ....

This schema is not started in production yet;

UPD1

i had to create simple laravel command wait_db_alive:

public function handle(){    $i = 1;    $ret = 1;    while($i <= 10){        echo 'connecting to host:'.config('database.connections.'.config('database.default').'.host').' try '.$i.'..';        try {            DB::connection()->getPdo();            echo 'ok'.PHP_EOL;            $ret = 0;            break;        } catch (\Exception $e) {            echo 'error:' . $e->getMessage() .PHP_EOL;            sleep(1);            $i++;        }    }    return $ret;}

and edit init.sh to

#!/bin/shphp artisan wait_db_alive && php artisan migrate --seed && /usr/bin/supervisord -n -c /etc/supervisord.conf

so, log for jobs:

jobs_1      | connecting to host:db try 1..error:SQLSTATE[HY000] [2002] Connection refusedjobs_1      | connecting to host:db try 2..error:SQLSTATE[HY000] [2002] Connection refusedjobs_1      | connecting to host:db try 3..okjobs_1      | Nothing to migrate.jobs_1      | Seeding: ServicesTableSeeder...jobs_1      | Database seeding completed successfully.jobs_1      | 2020-11-22 05:33:43,653 CRIT Supervisor is running as root.

UPD2

In some cases we need to start container without .env-file, then better init.sh for this:

#!/bin/shphp artisan wait_db_alive && php artisan migrate --seed /usr/bin/supervisord -n -c /etc/supervisord.conf


To my opinion, automate migrate is not a good way when creating container. You can do this after container is up with this one line code manually;

docker exec your_container_name php artisan migrate