Skip to content

Docs: Add Fly.io deployment guide #982

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,18 @@ Manual guides for deployment on various platforms, for One-click and docker plea
</InstallLink>
</div>

<div>
<InstallLink link="deployment/hosting-guides/fly.html">
<template #icon>
<svg id="SvgjsSvg1001" width="288" height="288" xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svgjs="http://svgjs.com/svgjs"><defs id="SvgjsDefs1002"></defs><g id="SvgjsG1008"><svg xmlns="http://www.w3.org/2000/svg" width="288" height="288" fill-rule="evenodd" viewBox="0 0 32 32"><path fill="url(#A)" d="M16.06 26.56c.038.01.077.017.112.033s.066.042.098.063l.006.006.032.028.055.05.238.222.43.432.442.495.266.337.235.34.186.317.136.292c.07.172.12.354.138.54l.006.103v.1l-.01.154c-.02.206-.066.4-.143.6a1.96 1.96 0 0 1-.2.372 2 2 0 0 1-.22.266 2.01 2.01 0 0 1-.206.182 2.08 2.08 0 0 1-.18.126l-.155.1c-.3.162-.653.253-1 .288l-.16.012-.178.004a3.24 3.24 0 0 1-.367-.022c-.203-.024-.404-.067-.597-.134a2.45 2.45 0 0 1-.477-.222c-.16-.098-.3-.215-.438-.352l-.23-.295c-.063-.1-.118-.205-.162-.314a2.09 2.09 0 0 1-.141-.57l-.012-.184.002-.147a1.65 1.65 0 0 1 .04-.267 2.18 2.18 0 0 1 .088-.284 3.09 3.09 0 0 1 .123-.276l.18-.32.22-.325.375-.474.427-.47.4-.4.172-.16.14-.126a.56.56 0 0 1 .21-.097zM16.244.004l.275.007.122.007.747.06 1.03.144.075.017 1 .246.282.083.32.1.4.146.302.126.47.22.263.133.532.3.163.098.726.524.177.154.5.452.43.456.118.13.503.662.076.12.388.648.1.207.22.482.228.562.076.218.243.877.2 1.185.033.368.03.53.002.077-.01.592-.007.1-.03.233-.167.945-.025.115-.384 1.186-.357.855-.454.9-.814 1.405-.42.633-.507.75-.366.496-.713.945-.5.6-.723.873-.505.585-.785.876-.494.537-1.495 1.546-.783.762-.433.407-.027.025-.033.03-.043.03-.03.014-.085.028-.204-.01-.018-.006-.1-.07-.014-.013-.015-.013-.058-.054-.173-.162-1.434-1.4-.444-.456-1.135-1.208-.27-.293-1.368-1.584-.383-.47-1.21-1.552-.12-.158-.916-1.335-.248-.397-.666-1.12-.2-.347-.575-1.2-.1-.235-.33-.92L6 11.45l-.093-.378-.08-.413-.063-.462-.024-.28-.01-.527.002-.1.014-.284.115-1.15.027-.148.26-1.148.006-.02.043-.128.183-.53.056-.134.297-.65.202-.378.286-.468.285-.416.198-.26.233-.278.255-.28.208-.2.548-.478.4-.332.484-.325.344-.22.73-.382 1.088-.447.922-.267.362-.08.498-.1.337-.05.52-.064.35-.03.704-.038.26-.003h.063l.224.005zm-.6 1.63l-.3.045c-.154.03-.306.07-.455.12a3.48 3.48 0 0 0-.467.199l-.292.168c-.705.448-1.244 1.12-1.64 1.847l-.188.37-.196.454-.215.6-.2.763-.186.98-.133 1.26-.033.804.018.924a13.86 13.86 0 0 0 .337 2.137c.205.886.472 1.758.773 2.616l1.28 3.178 1.888 3.8.05.093V1.633l-.024.002zm2.665.05a9.82 9.82 0 0 1 1.382.39c.64.234 1.257.54 1.823.92.768.516 1.44 1.172 1.96 1.938a7.25 7.25 0 0 1 .759 1.449c.292.75.46 1.547.53 2.348l.033.632-.012.588c-.035.44-.127.872-.25 1.294a10.06 10.06 0 0 1-.358 1.018l-.57 1.217a23.02 23.02 0 0 1-1.56 2.54c-.7 1.005-1.457 1.97-2.247 2.906l-2.582 2.844-.114.117 1.775-3.598.862-2.06.804-2.316.462-1.777a14.25 14.25 0 0 0 .251-1.554l.048-.684.003-.626-.03-.8c-.065-1.074-.23-2.144-.545-3.174l-.228-.656-.208-.488c-.404-.873-.97-1.7-1.733-2.285l-.256-.186z"></path><defs><linearGradient id="A" x1="5.74" x2="81.448" y1="0" y2="32" gradientUnits="userSpaceOnUse"><stop offset="0" stop-color="#ffffff" class="stopColor109cf8 svgShape"></stop><stop offset="1" stop-color="#ffffff" class="stopColor935ee9 svgShape"></stop></linearGradient></defs></svg></g></svg>
</template>
<template #title>Fly.io</template>
<template #description>
Manual step by step guide for deploying on Fly.io
</template>
</InstallLink>
</div>

<div>
<InstallLink link="deployment/hosting-guides/google-app-engine.html">
<template #icon>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,356 @@
---
title: Fly.io Deployment - Strapi Developer Docs
description: Learn in this guide how to deploy your Strapi application on Fly.io.
canonicalUrl: https://docs.strapi.io/developer-docs/latest/setup-deployment-guides/deployment/hosting-guides/fly.html
---

# Fly.io

This is a step-by-step guide for deploying a Strapi project on [Fly.io](https://fly.io/), using the [Postgres on Fly](https://fly.io/docs/reference/postgres/#about-postgres-on-fly) integration as persistent storage.

In this guide you are going to:

- Create a Strapi project
- Create a Fly project
- Setup and deploy a Postgres app on Fly
- Deploy the Strapi project on Fly
- Change the Strapi project and re-deploy on Fly

## Pre-requirements

::: caution
To follow this guide, you will need [an existing Fly.io account](https://fly.io/app/sign-up). For the first sign-up, Fly will require you to register your credit card. This guide will use only free-tier features of Fly, so your card is not going to be charged by following this.
:::

### Install the Fly CLI

Download and install the Fly CLI for your operating system. For more configuration and information, follow [Introducing Flyctl • Fly Docs](https://fly.io/docs/flyctl/).

<code-group>
<code-block title="MacOS">
```sh
brew install flyctl
```
</code-block>

<code-block title="Linux">
```sh
curl -L https://fly.io/install.sh | sh
```
</code-block>

<code-block title="Windows">
```sh
curl -L https://fly.io/install.sh | sh
```
</code-block>
</code-group>

The `flyctl` installation will create the symlink to `fly`, so in terminal both `flyctl` and `fly` are interchangeable.

### Login to Fly

Login to your Fly account. Follow the instructions and return to the command line.

```bash
fly auth login
```

## Create the Strapi project

### Generate the project

Create a [new Strapi project](/developer-docs/latest/getting-started/quick-start.md) using `create-strapi-app`.

<code-group>
<code-block title="npm">
```sh
npx create-strapi-app@latest strapi-fly --quickstart
```
</code-block>

<code-block title="yarn">
```sh
yarn create strapi-app strapi-fly --quickstart
```
</code-block>
</code-group>

::: tip
When you use `--quickstart` to create a Strapi project, a **SQLite database** will be used locally in the `development environment`. We will configure the `production` environment to use Postgres later in this guide.
:::

### Create the Dockerfile

Fly uses Dockerfiles to deploy its projects. Since [Strapi doesn't generate a Dockerfile](https://github.com/strapi/strapi-docker) for the project, you will have to create it. Create a new file `Dockerfile` in the root of your project.

<code-group>
<code-block title="yarn">
```Dockerfile
# Dockerfile

FROM node:16

# Installing libvips-dev for sharp Compatability
RUN apt-get update && apt-get install libvips-dev -y

# Set environment to production
ENV NODE_ENV=production

# Copy the configuration files
WORKDIR /opt/
COPY ./package.json ./yarn.lock ./
ENV PATH /opt/node_modules/.bin:$PATH

# Install dependencies
RUN yarn install

# Copy the application files
WORKDIR /opt/app
COPY ./ .

# Build the Strapi application
RUN yarn build

# Expose the Strapi port
EXPOSE 1337

# Start the Strapi application
CMD ["yarn", "start"]
```
</code-block>

<code-block title="npm">
```Dockerfile
# Dockerfile

FROM node:16

# Installing libvips-dev for sharp Compatability
RUN apt-get update && apt-get install libvips-dev -y

# Set environment to production
ENV NODE_ENV=production

# Copy the configuration files
WORKDIR /opt/
COPY ./package.json ./package-lock.json ./
ENV PATH /opt/node_modules/.bin:$PATH

# Install dependencies
RUN npm install

# Copy the application files
WORKDIR /opt/app
COPY ./ .

# Build the Strapi application
RUN yarn build

# Expose the Strapi port
EXPOSE 1337

# Start the Strapi application
CMD ["npm", "start"]
```
</code-block>
</code-group>

[Simen Daehlin](https://twitter.com/eventyret) wrote a full guide on using Strapi with Docker at [Docker with Strapi V4](https://blog.dehlin.dev/docker-with-strapi-v4).

### Create the .dockerignore file

One the root of your project, create another file called `.dockerignore`. This will indicate what files should be ignored during the Docker build.

```text
# .dockerignore

.tmp/
.cache/
.git/
build/
node_modules/
data/
```

## Create the Fly project

Time to [create a new Fly project](https://fly.io/docs/flyctl/launch/) inside of the Strapi root folder.

```sh
fly launch
```

Follow the instructions on the screen, until the **Would you like to setup a Postgresql database now?** is prompted.

```text
? App Name (leave blank to use an auto-generated name):
Automatically selected personal organization
? Select region: ams (Amsterdam, Netherlands)
Created app bold-haze-733 in organization personal
Wrote config file fly.toml
```

### Setup the Postgres project

You will be prompted **Would you like to setup a Postgresql database now?**. If you select *Yes*, Fly will create a separate Postgres project and attach it to your Strapi project. If you have a separate Postgres instance or you want to configure it later, select *No* and read further [Postgres on Fly • Fly Docs](https://fly.io/docs/reference/postgres/#creating-a-postgres-app).

Once you select *Yes* and select your preferred configuration, the Postgres app is being deployed.

```text
? Would you like to setup a Postgresql database now? Yes
For pricing information visit: https://fly.io/docs/about/pricing/#postgresql-clusters
? Select configuration: Development - Single node, 1x shared CPU, 256MB RAM, 1GB disk
Creating postgres cluster bold-haze-733-db in organization personal
Postgres cluster bold-haze-733-db created
```

::: tip
Explore how you can use the free resources on Fly at [About Free Postgres on Fly • Fly Docs](https://fly.io/docs/reference/postgres/#about-free-postgres-on-fly)
:::

Once the Postgres cluster is deployed, you will be prompted with the configuration details. Make sure to save these, since you won't be able to revisit them.

```text
Username: postgres
Password: 9904f659e488d9bb0572491ef3693c8c5df4345b8e136aed
Hostname: bold-haze-733-db.internal
Proxy Port: 5432
PG Port: 5433
Save your credentials in a secure place, you won't be able to see them again!
```

Moreover, Fly will provide you with the credentials that can be used internally in your app. Read more about connecting to Postgres within Fly vs. outside Fly in [Connecting to Postgres • Fly Docs](https://fly.io/docs/reference/postgres/#connecting-to-postgres).

### Attach Postgres to the Strapi app

Fly will automatically attach the Postgres cluster to the Strapi that you just created.

```text
Postgres cluster bold-haze-733-db is now attached to bold-haze-733
The following secret was added to bold-haze-733:
DATABASE_URL=postgres://bold_haze_733:ppx33flwj9JyM81@top2.nearest.of.bold-haze-733-db.internal:5432/bold_haze_733
Postgres cluster bold-haze-733-db is now attached to bold-haze-733
```

Fly automatically adds the `DATABASE_URL` in the environment secrets of the app. The [Fly secrets](https://fly.io/docs/reference/secrets/) are available to the app when it's running, but to deploy it from your machine, you need to make it available locally first. Edit the `.env` file of your Strapi project to include the exact same variable.

```sh
# .env

DATABASE_URL=postgres://bold_haze_733:ppx33flwj9JyM81@top2.nearest.of.bold-haze-733-db.internal:5432/bold_haze_733
```

::: tip
If you connect to your Strapi database externally, read more about managing Postgres or connecting from outside Fly at [Postgres on Fly • Fly Docs](https://fly.io/docs/reference/postgres/).
:::

When prompted **Would you like to deploy now?**, select *No*. You need to configure the production environment first.

## Configure the production environment

Before deploying the Strapi app, you will need to configure it to be production-ready.

### Configure Fly

The Fly CLI generated the configuration file `fly.toml`. This file should point the port that Strapi is using. Go to the `fly.toml` file and change the `internal_port` row (line 17) to `1337`.

```toml
[[services]]
http_checks = []
internal_port = 1337
processes = ["app"]
protocol = "tcp"
script_checks = []
```

### Configure Strapi

Because you are using Postgres, you will need to install the `pg-connection-string` package to split `DATABASE_URL` and `pg` as a client.

<code-group>
<code-block title="yarn">
```sh
yarn add pg-connection-string pg
```
</code-block>

<code-block title="npm">
```sh
npm i pg-connection-string pg
```
</code-block>
</code-group>

Inside of the `./config` directory, you need to setup a database config file for the production environment. Create a new file `./config/env/production/database.js`. This will connect Strapi to the production Postgres database you just created.

```js
// ./config/env/production/database.js

const parse = require("pg-connection-string").parse;
const config = parse(process.env.DATABASE_URL);

module.exports = () => ({
connection: {
client: "postgres",
connection: {
host: config.host,
port: config.port,
database: config.database,
user: config.user,
password: config.password,
ssl: false,
},
debug: false,
},
});
```

::: caution
This will configure the production environment to use Postgres. The local development config will remain SQLite and can be found in `./config/database.js`. If you want to configure it, read more about [Database configuration - Strapi Developer Docs](/developer-docs/latest/setup-deployment-guides/configurations/required/databases.html#configuration-structure).
:::

## Deploy to fly

Time to deploy your Strapi application to Fly.

```sh
fly deploy
```

This will build the Dockerfile with your project and deploy it to Fly. Follow the URL generated by Fly to get to your admin dashboard.

From here, continue using Strapi as you would normally do, or follow the [Quick Start Guide - Strapi Developer Docs](/developer-docs/latest/getting-started/quick-start.md).

::: caution
Strapi does **not** allow you to use the Content-type Builder in production. To update the content types, you will have to make the changes locally and re-deploy them to Fly.
:::

## Update the Strapi project

Even if you deployed your Strapi app to production, you can still use your local environment as development environment. In order to make the local changes available to production you will need to re-deploy the Fly app.

Following [Quick Start Guide - Strapi Developer Docs](/developer-docs/latest/getting-started/quick-start.md), start by running Strapi in development mode.

<code-group>
<code-block title="yarn">
```sh
yarn develop
```
</code-block>

<code-block title="npm">
```sh
npm run develop
```
</code-block>
</code-group>

After you make the changes and you are sure they work, re-deploy your app.

```sh
fly deploy
```