Hello!
I have a Nest.js application running with docker and Postgres database
I am using the multistage build for my docker image for smaller image size and it deploys correctly.
But I have to run my migrations, also written in typescript, for that, I used the following in my fly.toml:
[deploy]
release_command = "./release_command.sh"
my release_command.sh file was this:
npm i \
npx knex migrate:latest --knexfile ./database/config/knexfile.ts
I have to install all packages for running migration due to typescript dependencies, my production image has only the bundled files and removes all dev dependencies.
When I run this command, it seems that the VM running the migration restarts (or crashes for some reason) when I run fly deploy:
==> Release command detected: ./release_command.sh
--> This release will not be available until the release command succeeds.
Starting instance
Pulling container image
Starting instance
Configuring virtual machine
Pulling container image
Unpacking image
Preparing kernel init
Configuring firecracker
Starting virtual machine
Starting init (commit: 249766e)...
Setting up swapspace version 1, size = 536866816 bytes
Preparing to run: `docker-entrypoint.sh ./release_command.sh` as root
2022/09/12 12:34:34 listening on [fdaa:0:9bfa:a7b:2cc3:cf16:837:2]:22 (DNS: [fdaa::3]:53)
Starting instance
Configuring virtual machine
Pulling container image
Unpacking image
Preparing kernel init
Configuring firecracker
Starting virtual machine
Starting init (commit: 249766e)...
Setting up swapspace version 1, size = 536866816 bytes
UUID=c80b440a-de44-498b-b716-817c9d4c024f
Preparing to run: `docker-entrypoint.sh ./release_command.sh` as root
2022/09/12 12:34:34 listening on [fdaa:0:9bfa:a7b:2cc3:cf16:837:2]:22 (DNS: [fdaa::3]:53)
up to date, audited 156 packages in 2s
run `npm fund` for details
Unpacking image
Preparing kernel init
Configuring firecracker
Starting virtual machine
Starting init (commit: 249766e)...
Setting up swapspace version 1, size = 536866816 bytes
UUID=c80b440a-de44-498b-b716-817c9d4c024f
Preparing to run: `docker-entrypoint.sh ./release_command.sh` as root
2022/09/12 12:34:34 listening on [fdaa:0:9bfa:a7b:2cc3:cf16:837:2]:22 (DNS: [fdaa::3]:53)
up to date, audited 156 packages in 2s
18 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
up to date, audited 156 packages in 2s
18 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
Failed to load external module ts-node/register
Failed to load external module typescript-node/register
Failed to load external module typescript-register
Failed to load external module typescript-require
Failed to load external module sucrase/register/ts
Failed to load external module @babel/register
Working directory changed to /database/config
Cannot use import statement outside a module
/database/config/knexfile.ts:1
import { Knex } from 'knex';
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Object.compileFunction (node:vm:360:18)
at wrapSafe (node:internal/modules/cjs/loader:1048:15)
at Module._compile (node:internal/modules/cjs/loader:1083:27)
at Module._extensions..js (node:internal/modules/cjs/loader:1173:10)
at Module.load (node:internal/modules/cjs/loader:997:32)
at Module._load (node:internal/modules/cjs/loader:838:12)
at Module.require (node:internal/modules/cjs/loader:1021:19)
at require (node:internal/modules/cjs/helpers:103:18)
at importFile (/node_modules/knex/lib/migrations/util/import-file.js:11:7)
at async openKnexfile (/node_modules/knex/bin/cli.js:36:16)
Starting clean up.
Error release command failed, deployment aborted
This release_command.sh does work when I run the docker image locally.
I had made a test making my production image being bundled with the devDependencies (basically my development stage in the dockerfile) and it ran the migrations successfully, but the image size was about 400mb, so this approach is not a good option for me.
Does anyone have any idea what I can do?