Let Grails fly in the cloud

13 February 2023

This week I wanted to load test a grails application under real conditions. So I needed a simple way to deploy a grails application to a real server (not my notebook).

grails fly.dio

So, I asked my followers on twitter and mastodon, what cloud service I should use.

I quickly got a response from Patrick Baumgartner to use fly.io as a service.

A quick check showed me that fly.io seems to be really a simple service with a good free tier.

Let’s see how you can deploy your grails app to fly.io …​

Setup a Grails Starter

First, go to https://start.grails.org/ and download a starter app. Since I didn’t modify the defaults, a simple

curl -O https://start.grails.org/myapp.zip
unzip myapp.zip

also does the trick. You can now create some simple domain objects as described in the quick start guide "https://guides.grails.org/creating-your-first-grails-app/guide/index.html[Creating your first Grails Application]" - but you don’t have to. The basic application template will be enough to test the deployment to fly.io .

Now, modify the app to create a fat-jar file instead of a .war . Remove the reference to the war plugin from the build.gradle config (line 19):

//apply plugin:"war"

In order to avoid having to set up a database, I also modified the production configuration to use an in-memory database. In grails-app/conf/application.yml, I copied the development datasource over to the production one (line 122):

        dbCreate: create-drop
        url: jdbc:h2:mem:devDb;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE

…​ and deploy to fly.io

Next, create a simple Dockerfile with the following content:

FROM openjdk:14-ea-alpine3.10


COPY *.jar application.jar

CMD ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app/application.jar"]
yes, this is not perfect. It is just a quick hack.

Now, everything is prepared for the last few steps. To test your grails app, execute the following steps on the command line:

./grailsw package
cp build/libs/myapp-0.1.jar .
docker build --tag="grails-sample/complete:0.1" .
docker run -p 8080:8080 grails-sample/complete:0.1

This will test-run your app locally as docker image.

To deploy it to fly.io, execute

./grailsw package
cp build/libs/myapp-0.1.jar .
fly launch
fly apps list

Now, this is the part where you have to leave the free tier, but it is still cheap. If you go to your monitoring dashboard at fly.io, you will notice that your app crashed because of low memory (256MB). So, scale it up to 1GB (don’t have to redeploy) and it will run flawless :-)

Hope this little guide will help you with your own deployment.

for feedback and discussion: Twitter, Mastodon