Where to put the php artisan migrate command
Solution 1
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/sh
cd /app
php artisan migrate:fresh --seed
php 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
Solution 2
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
Related videos on Youtube
Tara Prasad Gurung
A full time DevOps Engineer. SRE Enthusiast. On my free time you can find me hosting DevOps Kathmandu Meetups to uplift Devops community in Kathmandu. #DEVOPS #Kubernetes.
Updated on July 09, 2022Comments
-
Tara Prasad Gurung almost 2 years
Trying to deploy the laravel application on docker stack .What I am confused or not able to figure out is where can I run this php artisan migrate:fresh to generate the tables required in mysql.
The services and the task are running well
docker-compose.yml
version: '3.3' networks: smstake: ipam: config: - subnet: 10.0.10.0/24 services: db: image: mysql:5.7 networks: - smstake ports: - "3306:3306" volumes: - db_data:/var/lib/mysql environment: MYSQL_ROOT_PASSWORD: password MYSQL_DATABASE: smstake MYSQL_USER: root MYSQL_PASSWORD: password deploy: mode: replicated placement: constraints: - node.role == manager app: image: smstake:latest ports: - 8000:80 networks: - smstake command: docker-compose exec app php artisan migrate --seed deploy: mode: replicated replicas: 1 placement: constraints: - node.role == manager volumes: db_data:
Here is the dockerfile with which the image is generated
FROM alpine ENV \ APP_DIR="/app" \ APP_PORT="80" # the "app" directory (relative to Dockerfile) containers your Laravel app... COPY app/ $APP_DIR # or we can make the volume in compose to say use this directory RUN apk update && \ apk add curl \ php7 \ php7-opcache \ php7-openssl \ php7-pdo \ php7-json \ php7-phar \ php7-dom \ php7-curl \ php7-mbstring \ php7-tokenizer \ php7-xml \ php7-xmlwriter \ php7-session \ php7-ctype \ php7-mysqli \ php7-pdo \ php7-pdo_mysql\ && rm -rf /var/cache/apk/* RUN curl -sS https://getcomposer.org/installer | php -- \ --install-dir=/usr/bin --filename=composer RUN cd $APP_DIR && composer install WORKDIR $APP_DIR RUN chmod -R 775 storage RUN chmod -R 775 bootstrap #CMD php artisan migrate:fresh CMD php artisan serve --host=0.0.0.0 --port=$APP_PORT
Tried adding to the Dockerfile as is commented but didn't solve the problem
Tried adding on docker-compose as command: php artisan migrate:fresh too
Previously was doing this in jenkins to make it work Now dont want it via jenkins
docker-compose up -d --force-recreate --build #Running commands on already running service docker-compose exec -T app php artisan migrate:fresh --seed --force
-
Tara Prasad Gurung about 6 yearsyou are absolutely correct but how do I implement that in docker-compose.yml or dockerfile as I am using the docker stack to run the services in docker swarm mode
-
Thamer about 6 yearsu can connect ur docker stack with ssh params
-
Tara Prasad Gurung about 6 yearslike in docker swarm the app may be running in any nodes available right cant ssh to particular nodes and ssh that might not be the solutions
-
CBaker almost 6 yearsThis approach isn't working for me. I'm running into the exact same problem. It appears as though the database is not up and running when run.sh is actually ran. Any thoughts?
-
Simon Woodside over 5 yearsI guess that the main caveat to this is that it's going to run the migrations every time you start the container. That might not be desired.
-
Simon Woodside over 5 years
docker-compose exec app php artisan migrate
-
Tara Prasad Gurung over 5 years@CBaker may be you need to post your Docerfile or docker-compose or some more details so that we can we get more information and tackle your issue.
-
thanosasimo over 4 yearsSame issue as CBaker. It's ok if the db volume already exists, but if you run it on a fresh build, mysql is not up and running.
-
Tara Prasad Gurung over 4 years@thanosasimo nowadays I am doing that from CI server(Jenkins) in the build or deploy pipeline
-
Fernando Torres almost 3 yearssame problem of @CBaker any solutions on 2021?