Deploy AdonisJS with SQLite on Fly.io
This guide provides instructions for deploying an AdonisJS with SQLite application on Fly.io.
Deploying an AdonisJS application is no different from deploying a standard Node.js application. You just have to keep a few things in mind:
- You build your TypeScript source to JavaScript, before deploying the app.
- You will have to start the server from the build folder and not the project root. Same is true for running migrations any other Node.js apps.
You can build your project for production by running the following ace command. Learn more about the TypeScript build process.
Pre-requisite
Launch App
fly launch helps us to initialize our application. In every fly launch application it uses a fly.toml file to tell the system how to deploy it.
The fly.toml file can be automatically generated by fly launch command, and prompt question on how to setup your application.
Run: fly launch
- ? Choose an app name (leave blank will default to 'app-name'): here you can enter the name of your application.
- ? Select organization: Personal (personal): if you have other organization you can select them else it automatically select personal.
- ? Choose a region for deployment: I recommend region that is near in your location. So I choose Singapore.
- ? Would you like to setup Postgres or Redis: enter N, because we are using SQLite.
- ? Would you like to deploy now?: enter N, because we need to define our secrets or environments on the application.
After that, fly launch command would generate some files like Dockerfile, docker-entrypoints.js, and fly.toml
We need also to update fly.toml to migrate your migrations by experimental.
app = "your-app" primary_region = "sin" [experimental] cmd = ["start.sh"] entrypoint = ["sh"] [env] DB_CONNECTION = "sqlite" [[mounts]] source = "data" destination = "/data" [http_service] internal_port = 3000 force_https = true auto_stop_machines = true auto_start_machines = true min_machines_running = 0 processes = ["app"]
As you can see we have experimental section on our fly.toml and we need to create start.sh shell script in order to execute our entry point.
On start.sh script add:
#!/bin/sh set -ex node /app/build/ace migration:run --force node /app/build/server.js
Defining environment variables
Before deploying your application, we need to define environment variables through fly secrets set command. Some environment variables are already defined on Dockerfile, and we need to update our DATBASE_URL to "/data/app.db".
ENV CACHE_VIEWS="true" \ DATABASE_URL="/data/app.db" \ DRIVE_DISK="local" \ HOST="0.0.0.0" \ PORT="3000" \ SESSION_DRIVER="cookie"
We need to update SQLite "connections.sqlite.connection.filename" on config/database.ts. You can update like this:
filename: ['test', 'development'].includes(Env.get('NODE_ENV')) ? Application.tmpPath(Env.get('app.db')) : Env.get('DATABASE_URL'),
Also, when we're deploying Adonis application, we should generate APP_KEY because it used for all encryption data, cookie, and sessions. To generate a new APP_KEY, we need to execute node ace generate:key and copy the generated key.
Run fly secrets set command to define your environment variables.
fly secrets set APP_KEY=generated-key SOME_ENV=some-env OTHER_KEYS=other-keys
Deploy your application
When you're done defining environment variables and modified some database config. Finally, you can run the fly deploy command in order to deploy your application to fly.io.
You can monitor your application on fly.io dashboard, and you can check if there's an error or success.
I hope this guide help you to deploy your AdonisJS with SQLite on fly.io. You can check some common issue section that I also encountered during the experiment.
Common Issues
If you're encounter some problem like this:
`error umounting /data: EBUSY: Device or resource busy, retrying in a bit`
Solution: fly apps restart app-name