Alexis Bouchez

Deploying an AdonisJS application to Fly.io

10/30/2022

Introduction

This guide is based on my experience deploying AdonisJobs to production on Fly.io. Fly.io is a great platform for deploying Docker images. It's easy to setup and has a useful dashboard for managing your application.

Prerequisites

Before you begin, you should have the following:

  • a Fly.io account, if you don't have one, you can sign up for free here ;
  • a working AdonisJS application.

Install the Fly CLI

The Fly CLI is a command-line tool that allows you to deploy your application to Fly.io. You can install it by running the following command:

curl -L https://fly.io/install.sh | sh

Login to Fly

After installing the Fly CLI, you can login to Fly by running the following command:

fly auth login

Dockerize your application

Fly.io deploys your application as a Docker container. So, you need to create a Dockerfile for your application. You can create a Dockerfile by running the following command:

touch Dockerfile .dockerignore

Then, add the following content to the Dockerfile:

ARG NODE_IMAGE=node:16.13.1-alpine

FROM $NODE_IMAGE AS base
RUN apk --no-cache add dumb-init g++ make py3-pip
RUN mkdir -p /home/node/app && chown node:node /home/node/app
WORKDIR /home/node/app
USER node
RUN mkdir tmp

FROM base AS dependencies
COPY --chown=node:node ./package*.json ./
RUN npm ci
COPY --chown=node:node . .

FROM dependencies AS build
RUN node ace build --production

FROM base AS production
ENV NODE_ENV=production
ENV PORT=$PORT
ENV HOST=0.0.0.0
COPY --chown=node:node ./package*.json ./
RUN npm ci --production
COPY --chown=node:node --from=build /home/node/app/build .
EXPOSE $PORT
CMD [ "dumb-init", "node", "server.js" ]
# Adonis default .gitignore ignores
node_modules
build
coverage
.vscode
.DS_STORE
.env
tmp

# Additional .gitignore ignores (any custom file you wish)
.idea

# Additional good to have ignores for dockerignore
Dockerfile*
docker-compose*
.dockerignore
*.md
.git
.gitignore

Create a Fly application

After creating a Dockerfile, you can create a Fly application by running the following command:

fly apps create <app-name>

Set up your database

Fly.io uses PostgreSQL as the default database. You can create a PostgreSQL database by running the following command:

fly databases create <db-name>

Set up your environment variables

Fly.io uses secrets to configure your application. You can set up your environment variables by running the following command:

fly secrets set PORT=3333 NODE_ENV=production APP_KEY=<app-key> DB_CONNECTION=pg DB_HOST=<db-host> DB_PORT=<db-port> DB_USER=<db-user> DB_PASSWORD=<db-password> DB_DATABASE=<db-name>

Deploy your application

After setting up your environment variables, you can deploy your application by running the following command:

fly deploy

Run migrations

fly ssh console -C "node ace /home/node/app/build/ace migration:run"

Launch your application

After deploying your application, you can launch it by running the following command:

fly launch

Conclusion

In this guide, you learned how to deploy an AdonisJS application to Fly.io.

If you have any questions, feel free to reach me.

Don't forget to follow me on Twitter @alexisbcz for more content like this.