laravel 将 php artisan migrate 命令放在哪里
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48850813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Where to put the php artisan migrate command
提问by Tara Prasad Gurung
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.
试图在 docker stack 上部署 laravel 应用程序。我感到困惑或无法弄清楚的是我可以在哪里运行这个 php artisan migrate:fresh 来生成 mysql 中所需的表。
The services and the task are running well
服务和任务运行良好
docker-compose.yml
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
这是生成图像的 dockerfile
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
尝试按照评论添加到 Dockerfile 但没有解决问题
Tried adding on docker-compose as command: php artisan migrate:fresh too
尝试添加 docker-compose 作为命令:php artisan migrate:fresh too
Previously was doing this in jenkins to make it work Now dont want it via jenkins
以前在 jenkins 中执行此操作以使其正常工作现在不希望通过 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
回答by Tara Prasad Gurung
This is how I solved it .Created a bash script called run.shand added the php artisan migrations commands followed by the php serve command.
这就是我解决它的方法。创建了一个名为run.sh的 bash 脚本并添加了 php artisan migrations 命令,然后是 php serve 命令。
run.sh
运行文件
#!/bin/sh
cd /app
php artisan migrate:fresh --seed
php artisan serve --host=0.0.0.0 --port=$APP_PORT
Added entrypointto the Dockerfile removing the CMDin the end which will run the commands desired.
添加到 Dockerfile 的入口点,最后删除CMD,它将运行所需的命令。
copy ./run.sh /tmp
ENTRYPOINT ["/tmp/run.sh"]
Remove the command from the docker-compose.yml
从 docker-compose.yml 中删除命令
回答by Thamer
to run all migrate u need to be inside ur container, for that u need to run ur containers with docker-compose up
, here u need to be with terminal in the directory where ur docker-compose.yml file is.
要运行所有迁移,你需要在你的容器内,因为你需要运行你的容器docker-compose up
,在这里你需要在你的 docker-compose.yml 文件所在的目录中使用终端。
after that, and with the same place of docker-compose.yml file, run this command to be inside ur container :
之后,在 docker-compose.yml 文件的同一个地方,运行这个命令到你的容器中:
docker exec -it name_of_conainer bash
and when u will be inside ur container, go to ur shared app inside the container, I think here is the app so cd app
.
当你进入你的容器时,转到容器内的共享应用程序,我认为这里是应用程序cd app
。
after all of this run :
在所有这些运行之后:
php artisan migrate
php artisan migrate