Help in deploy a typescript / typeorm api

Hey all, im trying to deploy an api with typeorm then run my migrations, but i dont understand how to do that

Dockerfile:

FROM node:lts-alpine

WORKDIR /app

COPY package*.json ./

RUN npm install\

&& npm install typescript -g

COPY . .

RUN npm run tsc

EXPOSE 3000

#here im trying the migrations that im already generated
CMD [“npm”, “run”, “migration:run”]

ENTRYPOINT [ “npm”, “start” ]

fly.toml:

app = “api”
kill_signal = “SIGINT”
kill_timeout = 5
processes =

[env]
HOST = “db.internal”
DBPORT = “5432”
USERNAME = “postgres”
PASSWORD = “password”
DB = “db”

[experimental]
allowed_public_ports =
auto_rollback = true

[[services]]
http_checks =
internal_port = 8080
processes = [“app”]
protocol = “tcp”
script_checks =
[services.concurrency]
hard_limit = 25
soft_limit = 20
type = “connections”

[[services.ports]]
force_https = true
handlers = [“http”]
port = 80

[[services.ports]]
handlers = [“tls”, “http”]
port = 443

[[services.tcp_checks]]
grace_period = “1s”
interval = “15s”
restart_limit = 0
timeout = “2s”

scripts from package.json:

“scripts”: {
“tsc”: “tsc”,
“start”: “node ./build/Server.js”,
“migration:generate”: “typeorm-ts-node-commonjs -d ./src/DataSource.ts migration:generate ./src/migrations/default”,
“migration:run”: “typeorm-ts-node-commonjs -d ./src/DataSource.ts migration:run”,
“test”: “echo "Error: no test specified" && exit 1”
},

in my code i have this:

export default new DataSource({
type: ‘postgres’,
host: process.env.HOST,
port: parseInt(process.env.DBPORT || “5432”),
username: process.env.USERNAME,
password: process.env.PASSWORD,
database: process.env.DB,
entities: [${__dirname}/**/entities/*{ts,js}],
migrations: [${__dirname}/**/migrations/*{ts,js}],
});

What am i missing ? :thinking:

It looks like you’re confused about how Docker uses CMD and ENTRYPOINT (which makes sense, it’s confusing)

First things first: I suggest running migrations as release command: App Configuration (fly.toml) · Fly Docs

That’s a setting in your fly.toml file.

Second thing: here’s cmd vs entrypoint: docker - What is the difference between CMD and ENTRYPOINT in a Dockerfile? - Stack Overflow

The way you have it setup now, fhe container will be started with the nonsense command `npm start npm run migration:run‘

(basically entrypoint + cmd smooshed together)

1 Like

thanks i will read this articles and try again

Adding to this, commands that you want to run once prior to deployment are generally best run as a release step. See App Configuration (fly.toml) · Fly Docs

1 Like

fly logs -a api output:

2022-11-14T02:33:38Z runner[bd83fea0] gru [info]Starting instance
2022-11-14T02:33:38Z runner[bd83fea0] gru [info]Configuring virtual machine
2022-11-14T02:33:38Z runner[bd83fea0] gru [info]Pulling container image
2022-11-14T02:33:39Z runner[bd83fea0] gru [info]Unpacking image
2022-11-14T02:33:39Z runner[bd83fea0] gru [info]Preparing kernel init
2022-11-14T02:33:40Z runner[bd83fea0] gru [info]Configuring firecracker
2022-11-14T02:33:40Z runner[bd83fea0] gru [info]Starting virtual machine
2022-11-14T02:33:40Z app[bd83fea0] gru [info]Starting init (commit: 81d5330)…
2022-11-14T02:33:40Z app[bd83fea0] gru [info]Setting up swapspace version 1, size = 536866816 bytes
2022-11-14T02:33:40Z app[bd83fea0] gru [info]UUID=5712cf3b-66fa-4b2d-8cb1-dc67d747cce8
2022-11-14T02:33:40Z app[bd83fea0] gru [info]Preparing to run: docker-entrypoint.sh npm run migration:run as root
2022-11-14T02:33:40Z app[bd83fea0] gru [info]2022/11/14 02:33:40 listening on [fdaa:0:cf21:a7b:70:bd83:fea0:2]:22 (DNS: [fdaa::3]:53)
2022-11-14T02:33:41Z app[bd83fea0] gru [info]> api-v2@1.0.0 migration:run
2022-11-14T02:33:41Z app[bd83fea0] gru [info]> typeorm-ts-node-commonjs -d ./src/DataSource.ts migration:run
2022-11-14T02:33:45Z app[bd83fea0] gru [info]query: SELECT * FROM current_schema()
2022-11-14T02:33:45Z app[bd83fea0] gru [info]query: CREATE EXTENSION IF NOT EXISTS “uuid-ossp”
2022-11-14T02:33:45Z app[bd83fea0] gru [info]query: SELECT version();
2022-11-14T02:33:45Z app[bd83fea0] gru [info]query: SELECT * FROM “information_schema”.“tables” WHERE “table_schema” = ‘public’ AND “table_name” = ‘migrations’
2022-11-14T02:33:45Z app[bd83fea0] gru [info]query: SELECT * FROM “migrations” “migrations” ORDER BY “id” DESC
2022-11-14T02:33:45Z app[bd83fea0] gru [info]No migrations are pending
2022-11-14T02:33:46Z app[bd83fea0] gru [info]Starting clean up.
2022-11-14T02:33:52Z runner[b11d2426] gru [info]Starting instance
2022-11-14T02:33:53Z runner[b11d2426] gru [info]Configuring virtual machine
2022-11-14T02:33:53Z runner[b11d2426] gru [info]Pulling container image
2022-11-14T02:33:54Z runner[b11d2426] gru [info]Unpacking image
2022-11-14T02:33:54Z runner[b11d2426] gru [info]Preparing kernel init
2022-11-14T02:33:54Z runner[b11d2426] gru [info]Configuring firecracker
2022-11-14T02:33:55Z runner[b11d2426] gru [info]Starting virtual machine
2022-11-14T02:33:55Z app[b11d2426] gru [info]Starting init (commit: 81d5330)…
2022-11-14T02:33:55Z app[b11d2426] gru [info]Preparing to run: docker-entrypoint.sh npm start as root
2022-11-14T02:33:55Z app[b11d2426] gru [info]2022/11/14 02:33:55 listening on [fdaa:0:cf21:a7b:70:b11d:2426:2]:22 (DNS: [fdaa::3]:53)
2022-11-14T02:33:55Z app[b11d2426] gru [info]> api-v2@1.0.0 start
2022-11-14T02:33:55Z app[b11d2426] gru [info]> node ./build/Server.js
2022-11-14T02:33:56Z app[b11d2426] gru [info]Listening on port 3000
2022-11-14T02:33:57Z app[b11d2426] gru [info]Data source has been initialized !

Thannnks, the migrations are working , but now im getting this error, when the app is being deployed:

I don’t know anything about typeorm, but apparently it can run on top of a number of back ends. The easiest path is If you pick postgresql and configure it to run against a postgresql instance on fly: Fly Postgres · Fly Docs.

The next easiest path may be to launch your own database as a separate app. Some people do that with Use a MySQL Database · Fly Docs.

Finally, if you want to run using only one fly application, you will need to define a volume: Volumes · Fly Docs; put your database on that volume; and then create a small script that will do both migration:run and start, and change your CMD or ENTRYPOINT to run that script instead.

1 Like

i see, well, im running my postgres in a different app, the migrations actually works but the deployment dont, this is so wierd and i cant even see the logs, now im stuck in the deployment

Then after a few minutes

Oh this guide helps me to fix it: Troubleshooting your Deployment · Fly Docs

I just set my port in dockerfile to expose 8080 and in my app i was listenning to 3000 then i change to 8080 !

Everything is fine now, thanks for the help :star_struck:

1 Like