问题描述
我有一个要在ECS(AWS)上部署的dockerized AdonisJs应用程序。我设法部署了映像,但是现在我不知道在部署时如何运行迁移。
通过Udemy课程学习后,我看到有人不得不做同样的事情,但要用laravel。在Dockerfile中,他没有运行CMD ['artisan','serve']
,而是创建了一个start.sh脚本,在其中启动应用程序,将其置于后台,运行迁移,然后将应用程序置于后台。这是脚本:
#!/bin/sh
# turn on bash's job control
set -m
# Start the primary process and put it in the background
php-fpm &
# Start the helper process
php artisan migrate
# now we bring the primary process back into the foreground
# and leave it there
fg %1
我试图对Adonis做同样的事情,这是我的脚本(许多版本之一):
#!/bin/sh
# turn on bash's job control
set -m
# Start the primary process and put it in the background
adonis serve &
# Start the helper process
adonis migration:run
# now we bring the primary process back into the foreground
# and leave it there
fg %1
但是我总是会出错。例如:
- 服务器已启动,但由于adonis无法连接到数据库,因此迁移未运行。我不知道如何调试它,因为如果我正常启动应用程序,Adonis可以完美地连接到数据库。
- (我仅在本地尝试过此操作)服务器启动,迁移运行,但是随后服务器进程没有出现在前台,因此应用程序并未真正启动(
curl localhost
它给了我curl: (7) Failed to connect to localhost port 80: Connection refused
)而且我无法使用ctrl + c停止服务器,我必须找到docker容器并停止该容器。 这是控制台向我显示的内容:
SERVER STARTED
info: serving app on http://0.0.0.0:80
Nothing to migrate
您能帮我创建一个执行此操作的脚本吗?
EDIT1:我注意到即使我仅创建一个“ adonis serve”脚本,它仍然无法正常工作,也许这不是通过脚本启动服务器的正确方法吗?
解决方法
我需要解决的第一个问题是没有终止的迁移命令。原因是我如何启动adonis-scheduler
软件包。我是从start / kernel.js文件启动的。现在,我改为使用以下内容创建了一个scheduler.js
(在开始位置内部):
'use strict'
/*
|--------------------------------------------------------------------------
| Run Scheduler
|--------------------------------------------------------------------------
|
| Run the scheduler on boot of the web sever.
|
*/
const Scheduler = use('Adonis/Addons/Scheduler')
Scheduler.run()
并将其添加到server.js中:
new Ignitor(require('@adonisjs/fold'))
.appRoot(__dirname)
.preLoad('start/scheduler') //code added
.fireHttpServer()
.catch(console.error)
这样做,我的迁移总会终止。
我遇到的第二个问题是不支持“#!/ bin / bash”,因此我需要以“#!/ bin / sh”格式更改脚本。不幸的是,这种格式不支持将作业放到后台,以后再将它们移到前台,因此我只运行迁移,然后启动服务器。这是文件:
#!/bin/sh
# Start the helper process
adonis key:generate
adonis migration:run --force
# Start the primary process
adonis serve