Deploy FusionAuth to Heroku
On this page
You can run the FusionAuth Docker image on Heroku with a Heroku Postgres database. The image needs a small wrapper on Heroku for the following reasons:
- Heroku Postgres sets
DATABASE_URLto apostgres://URL. FusionAuth reads the same variable, but expects a JDBC URL, with the username and password in separate variables. - Heroku assigns the HTTP port at runtime in the
PORTvariable. The web process must listen on that port. - Heroku ignores the
USERin a Dockerfile and runs the container as a different non-root user. By default, that user can't write to the FusionAuth install directory.
Prerequisites#
- A Heroku account with a payment method. Heroku Postgres and the dyno sizes that FusionAuth needs aren't free.
- The Heroku CLI, logged in to your account.
- Git.
Heroku builds Docker images from heroku.yml only in Cedar-generation apps. Fir-generation apps don't support Docker builds. Apps in the Common Runtime run on Cedar. For more information, see Heroku Generations.
Create the Project#
-
Create a folder for the project:
$ mkdir fusionauth-heroku $ cd fusionauth-heroku -
Create a file named
heroku-start.shwith the following content:heroku-start.sh #!/bin/sh set -e # heroku sets DATABASE_URL to postgres://user:password@host:port/dbname url="${DATABASE_URL#*://}" export DATABASE_USERNAME="${url%%:*}" url="${url#*:}" export DATABASE_PASSWORD="${url%%@*}" export DATABASE_URL="jdbc:postgresql://${url#*@}?sslmode=require" export DATABASE_ROOT_USERNAME="$DATABASE_USERNAME" export DATABASE_ROOT_PASSWORD="$DATABASE_PASSWORD" # the heroku user may have no writable home directory export HOME=/usr/local/fusionauth exec env "fusionauth-app.http.port=$PORT" /usr/local/fusionauth/fusionauth-app/bin/start.shThe script reads
DATABASE_URLat every start, because Heroku changes the URL when Heroku rotates the database credentials or fails over. The script addssslmode=require, because Heroku Postgres accepts only SSL connections.The script also sets the Heroku database user as the root database user. FusionAuth then runs in Silent Mode, which creates the database schema on the first start and applies database migrations after an update.
-
Create a file named
Dockerfilewith the following content:Dockerfile FROM fusionauth/fusionauth-app:1.71.1 USER root COPY heroku-start.sh /usr/local/fusionauth/heroku-start.sh # heroku ignores USER and runs the container as a different user RUN chmod -R a+rwX /usr/local/fusionauth /opt/openjdk/conf/security && chmod a+x /usr/local/fusionauth/heroku-start.sh USER fusionauth CMD ["/usr/local/fusionauth/heroku-start.sh"]The FusionAuth start script writes to the install directory and to the Java security directory, so the Dockerfile makes both directories writable for any user.
-
Create a file named
heroku.ymlwith the following content:heroku.yml build: docker: web: Dockerfile -
Commit the files to a Git repository:
$ git init $ git add . $ git commit -m "Deploy FusionAuth to Heroku"
Deploy to Heroku#
-
Create a Heroku app that builds from Docker. Replace
<app-name>with a name for the app:$ heroku create <app-name> --stack containerThe command also adds a Git remote named
herokuto the repository. -
Add a Heroku Postgres database to the app:
$ heroku addons:create heroku-postgresql:essential-0 --app <app-name>The
essential-0plan is the smallest Heroku Postgres plan. For the other plans, see Heroku Postgres Plans. -
Set the FusionAuth configuration:
$ heroku config:set SEARCH_TYPE=database FUSIONAUTH_APP_RUNTIME_MODE=production --app <app-name>SEARCH_TYPE=databaseuses the database for user search, so the app doesn't need Elasticsearch or OpenSearch. For more information, see Search. -
Push the repository to Heroku:
$ git push heroku HEAD:mainHeroku builds the image and starts a web dyno.
-
Change the web dyno size to Standard-2X:
$ heroku ps:type web=standard-2x --app <app-name>FusionAuth uses a 512 MB Java heap by default, and Basic and Standard-1X dynos have 512 MB of memory in total. A Standard-2X dyno has 1 GB. For the heap setting, see
fusionauth-app.memoryin Configuration. -
Open the app:
$ heroku open --app <app-name>
Confirm the Deployment#
The app opens the FusionAuth setup wizard. Complete the setup wizard to create an admin user. For more information, see First-Time Setup.
When the app doesn't load, read the logs:
$ heroku logs --tail --app <app-name>
The log line HTTP server listening on port shows that FusionAuth started and listens on the Heroku port.
Proxy Headers#
The Heroku router handles HTTPS and forwards requests to FusionAuth over HTTP. The router sets X-Forwarded-Proto, X-Forwarded-Port, and X-Forwarded-For, and keeps the original Host header. For more information, see Proxy Setup.
Update FusionAuth#
To update FusionAuth, change the version in the FROM line of the Dockerfile. Commit the change, and push the repository to Heroku again. Silent Mode applies the database migrations when the new dyno starts.
FusionAuth applies the database migrations before FusionAuth listens on the Heroku port. Heroku restarts a web dyno that doesn't listen on the port within 60 seconds. When an update fails with an R10 error, see Boot Timeout.
Related
Learn how to provision an AKS cluster in Microsoft Azure.
Learn how to provision an EKS cluster in AWS.
Learn how to deploy FusionAuth in a Kubernetes Cluster.
Learn how to provision a GKE cluster in Google Cloud (GCP).