尝试在docker堆栈上部署laravel应用程序。我是什么 困惑或无法弄清楚我在哪里可以运行这个php工匠 migrate:fresh以生成mysql中所需的表。
服务和任务运行良好
搬运工-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:
以下是生成图像的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
尝试按照评论添加到Dockerfile,但没有解决问题
尝试将docker-compose添加为命令:php artisan migrate:fresh too
以前在詹金斯这样做是为了让它工作现在不想通过詹金斯
docker-compose up -d --force-recreate --build
#Running commands on already running service
docker-compose exec -T app php artisan migrate:fresh --seed --force
答案 0 :(得分:4)
这就是我解决它的方法。修复了一个名为 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
将入口点添加到Docker文件中,最后删除 CMD ,这将运行所需的命令。
copy ./run.sh /tmp
ENTRYPOINT ["/tmp/run.sh"]
从docker-compose.yml
中删除命令答案 1 :(得分:0)
是,特殊脚本。 我尝试构建部署并测试抛出docker-compose,因此在docker-service“ jobs”中启动管理程序之前,我先在脚本中运行迁移:
#!/bin/sh
cd /var/www
php artisan migrate --seed
/usr/bin/supervisord -n -c /etc/supervisord.conf
然后从我的deploy-docker-compose.yml中片段:
services:
nginx:
depends_on:
- phpfpm ## NOT START BEFORE PHPFPM
phpfpm:
depends_on:
- jobs ## NOT START BEFORE MIGRATION
jobs:
# ....
此架构尚未在生产中启动;
UPD1
我不得不创建简单的laravel命令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;
}
并将init.sh编辑为
#!/bin/sh
php artisan wait_db_alive && php artisan migrate --seed && /usr/bin/supervisord -n -c /etc/supervisord.conf
因此,登录作业:
jobs_1 | connecting to host:db try 1..error:SQLSTATE[HY000] [2002] Connection refused
jobs_1 | connecting to host:db try 2..error:SQLSTATE[HY000] [2002] Connection refused
jobs_1 | connecting to host:db try 3..ok
jobs_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
在某些情况下,我们需要启动不带.env文件的容器,然后为此需要更好的init.sh:
#!/bin/sh
php artisan wait_db_alive && php artisan migrate --seed
/usr/bin/supervisord -n -c /etc/supervisord.conf
答案 2 :(得分:0)
在我看来,创建容器时自动迁移不是一个好方法。您可以在容器手动完成这一行代码后执行此操作;
docker exec your_container_name php artisan migrate
答案 3 :(得分:-1)
运行所有迁移你需要在你的容器内,因为你需要用docker exec -it name_of_ur_server bash
运行你的容器,这里你需要在你的docker-compose.yml文件所在的目录中使用终端。
之后,使用docker-compose.yml文件的相同位置,将此命令运行到ur容器内:
cd app
当你将在你的容器内时,转到容器内的你的共享应用程序,我认为这是app php artisan migrate
。
:
SELECT Title
, Date
, Url
, Url_Hash
FROM form_attributes a
LEFT
JOIN filings f
ON f.Url_Hash = a.Unique_Hash
ORDER
BY f.Date ASC