Deploy a NextJS Application

Getting an application running on Fly is essentially working out how to package it as a deployable image. Once packaged it can be deployed to the Fly infrastructure to run on the global application platform.

In this guide we'll learn how to deploy a NextJS application on Fly.

You can deploy your NextJS app on Fly with minimal effort, our CLI will do the heavy lifting. You can use your existing NextJS app or you can create one using the tutorial then come back here to deploy your app.

Generate the NextJS App

If you just want to see how Fly deployment works, follow these steps.

First, install flyctl, your Fly app command center, and sign up to Fly if you haven't already.

Now let's launch your NextJS app.

cd hello-next
fly launch
Creating app in /Users/me/hello-next
Scanning source code
Detected a NextJS app
? App Name (leave blank to use an auto-generated name): hello-next
? Select organization: flyio (flyio)
? Select region: mad (Madrid, Spain)
Created app hello-next in organization soupedup
? Would you like to deploy now? Yes
==> Validating app configuration
--> Validating app configuration done
Services
TCP 80/443 ⇢ 8080
Remote builder fly-builder-little-glitter-8329 ready
...
1 desired, 1 placed, 1 healthy, 0 unhealthy [health checks: 2 total, 2 passing]
--> v0 deployed successfully

That's it! Run fly open to see your deployed app in action.

Try a few other commands:

  • fly logs - Tail your application logs
  • fly status - App deployment details
  • fly deploy - Deploy the application after making changes

What About Build Time Environment Variables?

If you're an NextJS user you might know that it supports exposing environment variables to the browser using variables with name starting with NEXT_PUBLIC_.

For our build system to understand that you need to tweak two sections we generate for you, fly.toml and our Dockerfile.

Search for [build.args] on your fly.toml and add the variables you need there.

[build.args]
  NEXT_PUBLIC_EXAMPLE="This is the value"
  NEXT_PUBLIC_OTHER="Other value"

Then go to your Dockerfile and add ARG instructions before your yarn build like this:

ARG NEXT_PUBLIC_EXAMPLE
ARG NEXT_PUBLIC_OTHER

# Your yarn build should come after those ARGs