Platform.sh User Documentation

How to Deploy Quarkus on Platform.sh with MongoDB

Try for 30 days
Flexible, version-controlled infrastructure provisioning and development-to-production workflows
Activate your trial

MongoDB with Panache provides active record style entities (and repositories) like you have in Hibernate ORM with Panache. It focuses on helping you write your entities in Quarkus.

To activate MongoDB and then have it accessed by the Quarkus application already in Platform.sh, it is necessary to modify two files.

1. Add the MongoDB service Anchor to this heading

In your service configuration, include MongoDB with a valid supported version:

dbmongo:
    type: mongodb:3.6
    disk: 512

2. Grant access to MongoDb through a relationship Anchor to this heading

In your app configuration, use the service name dbmongo to grant the application access to MongoDB via a relationship:

.platform.app.yaml
relationships:
    mongodb: "mongodb:mongodb"

3. Export connection credentials to the environment Anchor to this heading

Connection credentials for services are exposed to the application container through the PLATFORM_RELATIONSHIPS environment variable from the deploy hook onward. Since this variable is a base64 encoded JSON object of all of your project’s services, you’ll likely want a clean way to extract the information specific to the database into it’s own environment variables that can be used by Quarkus. On Platform.sh, custom environment variables can be defined programmatically in a .environment file using jq to do just that:

export MONGO_PORT=$(echo $PLATFORM_RELATIONSHIPS | base64 --decode | jq -r ".mongodatabase[0].port")
export MONGO_HOST=$(echo $PLATFORM_RELATIONSHIPS | base64 --decode | jq -r ".mongodatabase[0].host")
export QUARKUS_MONGODB_HOSTS="${MONGO_HOST}:${MONGO_PORT}"
export QUARKUS_MONGODB_CREDENTIALS_PASSWORD=$(echo $PLATFORM_RELATIONSHIPS | base64 --decode | jq -r ".mongodatabase[0].password")
export QUARKUS_MONGODB_CREDENTIALS_USERNAME=$(echo $PLATFORM_RELATIONSHIPS | base64 --decode | jq -r ".mongodatabase[0].username")
export QUARKUS_MONGODB_DATABASE=$(echo $PLATFORM_RELATIONSHIPS | base64 --decode | jq -r ".mongodatbase[0].path")
export QUARKUS_HTTP_PORT=$PORT
export JAVA_OPTS="-Xmx$(jq .info.limits.memory /run/config.json)m -XX:+ExitOnOutOfMemoryError"

4. Connect to the service Anchor to this heading

Commit that code and push. The application is ready and connected to a MongoDB instance.

Is this page helpful?