Skip to content

Instantly share code, notes, and snippets.

@martinrisseeuw
Last active August 12, 2022 18:36
Show Gist options
  • Save martinrisseeuw/53cdb5b3f79babe5c2b9af1a6f8997b6 to your computer and use it in GitHub Desktop.
Save martinrisseeuw/53cdb5b3f79babe5c2b9af1a6f8997b6 to your computer and use it in GitHub Desktop.
Adonis deploy on Dokku

Deploying Adonis5 on Dokku

For the deployment of Adonis we're currently using Dokku(http://dokku.viewdocs.io/dokku/). Dokku is a self-hosted application manager similar to Heroku. Follow the Dokku instructions to setup your server.

Preparing your application

Since our app is using Redis & Postgres I will walk you through the setup for this as well. The first step is to create a Dokku application on your server after finishing the Dokku installation. Our application is called api so we run:

dokku apps:create api

After creating the application it's time to setup & link Redis and Postgres:

sudo dokku plugin:install https://github.com/dokku/dokku-postgres.git
sudo dokku plugin:install https://github.com/dokku/dokku-redis.git redis

dokku postgres:create postgres
dokku redis:create redis

/// link services to application
dokku postgres:link postgres api
dokku redis:link redis api

When you have linked the services Dokku adds a couple of env vars to your application:

DATABASE_URL
REDIS_URL

The database URL we will use to connect postgres. In your database.config add this the database_url as env.

pg: {
    client: 'pg',
    connection: Env.get('DATABASE_URL'),
    healthCheck: true,
    debug: false,
},

In your env.ts add:

DATABASE_URL: Env.schema.string(),

Redis is a little bit different since I couldn't get the redis_url to work so I extracted the data from the url add added them in seperated env vars on dokku:

dokku config:set api REDIS_HOST=<host> REDIS_PORT=<port> REDIS_PASSWORD=<password>

Another difficulty I ran into was that the env.ts requires the port to be defined. Dokku takes care of your port automaticaly and will most likely give it port 5000. I added this as env on the dokku application, otherwise TypeScript would complain on deployment about port env not being defined.

deploying application

After setting up your dokku application it's time to prepare Adonis a bit further. Add a Procfile in the root of you project containing:

release: node ./build/ace migration:run --force
web: npm start

Thanks to McSneaky to point me into the right direction concering release.

In your package.json add:

"scripts": {
    "build": "node ace build --production",
    "start": "ENV_SILENT=true node build/server.js",
}

Now everything is prepared add the dokku remote & push your app:

// http://dokku.viewdocs.io/dokku/deployment/application-deployment/#deploy-the-app
# from your local machine
# the remote username *must* be dokku or pushes will fail
git remote add dokku dokku@dokku.me:ruby-getting-started
git push dokku main:master

And your finished, check out the application url that you added [http://dokku.viewdocs.io/dokku/configuration/domains/]

tslib missing

A small issue I had was on deployment, it complained about tslib missing. I solved this by adding eslint-plugin-adonis to de the dependencies instead of devDependencies as short term solution that I found in this article [https://stackoverflow.com/questions/65428723/cannot-find-module-tslib-while-dockerizing-an-adonis-app-v5]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment