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_URL to a postgres:// 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 PORT variable. The web process must listen on that port.
  • Heroku ignores the USER in 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.
Use a Cedar-generation app

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#

  1. Create a folder for the project:

    $ mkdir fusionauth-heroku
    $ cd fusionauth-heroku
    
  2. Create a file named heroku-start.sh with 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.sh
    

    The script reads DATABASE_URL at every start, because Heroku changes the URL when Heroku rotates the database credentials or fails over. The script adds sslmode=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.

  3. Create a file named Dockerfile with 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.

  4. Create a file named heroku.yml with the following content:

    heroku.yml
    build:
      docker:
        web: Dockerfile
    
  5. Commit the files to a Git repository:

    $ git init
    $ git add .
    $ git commit -m "Deploy FusionAuth to Heroku"
    

Deploy to Heroku#

  1. Create a Heroku app that builds from Docker. Replace <app-name> with a name for the app:

    $ heroku create <app-name> --stack container
    

    The command also adds a Git remote named heroku to the repository.

  2. Add a Heroku Postgres database to the app:

    $ heroku addons:create heroku-postgresql:essential-0 --app <app-name>
    

    The essential-0 plan is the smallest Heroku Postgres plan. For the other plans, see Heroku Postgres Plans.

  3. Set the FusionAuth configuration:

    $ heroku config:set SEARCH_TYPE=database FUSIONAUTH_APP_RUNTIME_MODE=production --app <app-name>
    

    SEARCH_TYPE=database uses the database for user search, so the app doesn't need Elasticsearch or OpenSearch. For more information, see Search.

  4. Push the repository to Heroku:

    $ git push heroku HEAD:main
    

    Heroku builds the image and starts a web dyno.

  5. 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.memory in Configuration.

  6. 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.