Deployment of Java Spring API using Dockerfile

We have been using Heroku in the classroom to deploy a project developed by all the students together in each course. The project is different each year (examples at UdL-EPS-SoftArch · GitHub) and includes a Java Spring backend and an Angular frontend. The repositories include GitHub Actions to do CI/CD.

We are willing to migrate the CD part to fly.io using your free resources’ availability. For the moment, we have deployed the Spring API and attached it to a Postgres database for persistence. However, despite we limit the Spring JVM memory size using the following command:

/bin/sh -c java $JAVA_OPTS -Dspring.profiles.active=$PROFILE -jar app.jar

With JAVA_OPTS = “-Xmx214m -Xms128m”, the application is being killed continuously showing the following error:

[info] [ 17.753141] Out of memory: Killed process 520 (java) total-vm:2262688kB, anon-rss:201740kB, file-rss:0kB, shmem-rss:0kB, UID:0 pgtables:676kB oom_score_adj:0

This approach has been working with Heroku and their 512MB limit. Has anyone been able to run Spring on fly.io? Or is 256MB too low for it?

1 Like

I’ve had some luck with using the openj9 jvm based docker image (eg ibm-semeru-runtimes:open-17-jre-focal) for jvm apps to make them (barely) fit in 256MB, where a hotspot based jvm (eg eclipse-temurin:17-jre-jammy) continuously hit the memory limit. fwiw I’m running with -XX:MaxRAM=70m which appeared to give the minimum heap for my app to start up successfully, and still get an average mem usage near the limit (currently @ 227 MB/232 MB)

1 Like

I am facing the exactly same problem! I am trying to deploy a docker image of my application which is an angular + spring boot. My app only startup when I defined JAVA_OPS = " -Xmx214m -Xms128m” as you did. But my application which is using java 11 failed to startup complete because out of memory, showing the following message:

[info] [ 194.978636] Out of memory: Killed process 515 (java) total-vm:2224432kB, anon-rss:201256kB, file-rss:0kB, shmem-rss:0kB, UID:1000 pgtables:660kB oom_score_adj:0

I read in another forum post (https://community.fly.io/t/oom-errors-on-free-tier-on-a-bare-application/2621/2) that the " total-vm:2224432kB" meaning which is needed to startup, so is it need 2GB RAM?

@jjjoooeee could you share your Dockerfile config, please?

it’s pretty barebones, just running a shaded jar:

FROM ibm-semeru-runtimes:open-17-jre-focal

ARG CURRENT_VERSION

ADD server/target/server-${CURRENT_VERSION}-shaded.jar /app/server.jar

ENV PORT 8080
EXPOSE 8080

CMD java -XX:MaxRAM=70m -jar /app/server.jar
1 Like

Thank you very much for your reply! As soon I try it out I will share the results with you :nerd_face:

It works as you said! My dockerfile to make it works:

FROM ibm-semeru-runtimes:open-11-jre-focal
MAINTAINER https://renanfranca.github.io/about.html
COPY target/baby-0.0.1-SNAPSHOT.jar baby-0.0.1-SNAPSHOT.jar
ENV _JAVA_OPTIONS="-XX:MaxRAM=70m"
CMD java $_JAVA_OPTIONS -Dspring.profiles.active=$SPRING_PROFILES_ACTIVE -Dspring.datasource.url=$SPRING_DATASOURCE_URL -Dspring.liquibase.url=$SPRING_LIQUIBASE_URL -Dspring.datasource.username=$SPRING_DATASOURCE_USERNAME -Dspring.datasource.password=$SPRING_DATASOURCE_PASSWORD -jar baby-0.0.1-SNAPSHOT.jar

I had a hard time because of the database URL connection! The postgres url server was wrong if you use it with java springboot and I didn’t pay attention into it. So I change the prefix postgres to PostgreSQL: jdbc:postgresql://example-postgres.internal:5432/databasename

I can’t thank you enough!

I wrote a blog post about the process of publishing an angular + spring boot solution at flyio:

Deploy jhipster monolithic (angular + spring boot) at fly.io for FREE
I will share with you my experience to publish an angular + spring boot + postgres database solution into fly.io for FREE. I opened source the Baby Care App as monolithic to let you put your hands on the code as I explain the step-by-step.

2 Likes

as an additional note, things run much better after also enabling swap as per Swap memory - #2 by OldhamMade – was getting memory issues a bit after startup without swap, but seems to be much more stable with swap

1 Like