# Deployment of Java Spring API using Dockerfile

**URL:** <https://community.fly.io/t/deployment-of-java-spring-api-using-dockerfile/6708>\
**Category:** Questions / Help\
**Created:** [August 29, 2022, 11:06am UTC](https://community.fly.io/t/deployment-of-java-spring-api-using-dockerfile/6708 "2022-08-29T11:06:37Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![rogargon](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/rogargon/32/5088_2.png) [@rogargon](https://community.fly.io/u/rogargon)\
**Post date:** [August 29, 2022, 11:06am UTC](https://community.fly.io/t/deployment-of-java-spring-api-using-dockerfile/6708/1 "2022-08-29T11:06:37Z")

</div>

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](https://github.com/UdL-EPS-SoftArch)) 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](http://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:

```auto
/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:

```auto
[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](http://fly.io)? Or is 256MB too low for it?

---

<div class="post-metadata">

**Author:** ![jjjoooeee](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/jjjoooeee/32/42013_2.png) [@jjjoooeee](https://community.fly.io/u/jjjoooeee)\
**Post date:** [September 2, 2022, 4:19pm UTC](https://community.fly.io/t/deployment-of-java-spring-api-using-dockerfile/6708/2 "2022-09-02T16:19:18Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![renanfranca](https://avatars.discourse-cdn.com/v4/letter/r/aca169/32.png) [@renanfranca](https://community.fly.io/u/renanfranca)\
**Post date:** [September 2, 2022, 9:06pm UTC](https://community.fly.io/t/deployment-of-java-spring-api-using-dockerfile/6708/3 "2022-09-02T21:06:06Z")

</div>

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:

```auto
[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](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?

 ![fly_io_deploy_not_working](https://global.discourse-cdn.com/fly1/original/3X/a/4/a425493b7904e85af8dd77b86c59e2b483508d4f.png)

---

<div class="post-metadata">

**Author:** ![renanfranca](https://avatars.discourse-cdn.com/v4/letter/r/aca169/32.png) [@renanfranca](https://community.fly.io/u/renanfranca)\
**Post date:** [September 2, 2022, 9:23pm UTC](https://community.fly.io/t/deployment-of-java-spring-api-using-dockerfile/6708/4 "2022-09-02T21:23:53Z")

</div>

@jjjoooeee could you share your Dockerfile config, please?

---

<div class="post-metadata">

**Author:** ![jjjoooeee](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/jjjoooeee/32/42013_2.png) [@jjjoooeee](https://community.fly.io/u/jjjoooeee)\
**Post date:** [September 2, 2022, 9:39pm UTC](https://community.fly.io/t/deployment-of-java-spring-api-using-dockerfile/6708/5 "2022-09-02T21:39:47Z")

</div>

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

```auto
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

```

---

<div class="post-metadata">

**Author:** ![renanfranca](https://avatars.discourse-cdn.com/v4/letter/r/aca169/32.png) [@renanfranca](https://community.fly.io/u/renanfranca)\
**Post date:** [September 2, 2022, 10:10pm UTC](https://community.fly.io/t/deployment-of-java-spring-api-using-dockerfile/6708/6 "2022-09-02T22:10:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![renanfranca](https://avatars.discourse-cdn.com/v4/letter/r/aca169/32.png) [@renanfranca](https://community.fly.io/u/renanfranca)\
**Post date:** [September 3, 2022, 6:04pm UTC](https://community.fly.io/t/deployment-of-java-spring-api-using-dockerfile/6708/7 "2022-09-03T18:04:06Z")

</div>

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

```auto
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!

---

<div class="post-metadata">

**Author:** ![renanfranca](https://avatars.discourse-cdn.com/v4/letter/r/aca169/32.png) [@renanfranca](https://community.fly.io/u/renanfranca)\
**Post date:** [September 28, 2022, 10:53am UTC](https://community.fly.io/t/deployment-of-java-spring-api-using-dockerfile/6708/8 "2022-09-28T10:53:26Z")

</div>

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](https://renanfranca.github.io/deploy-jhipster-monolithic-angularjs-and-spring-boot-at-fly.io.html)  
I will share with you my experience to publish an angular + spring boot + postgres database solution into [fly.io](https://fly.io/) for FREE. I opened source [the Baby Care App as monolithic](https://github.com/renanfranca/mamazinha-monolithic/tree/publish-to-flydotio) to let you put your hands on the code as I explain the step-by-step.

---

<div class="post-metadata">

**Author:** ![jjjoooeee](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/jjjoooeee/32/42013_2.png) [@jjjoooeee](https://community.fly.io/u/jjjoooeee)\
**Post date:** [February 15, 2023, 2:00am UTC](https://community.fly.io/t/deployment-of-java-spring-api-using-dockerfile/6708/9 "2023-02-15T02:00:51Z")

</div>

as an additional note, things run much better after also enabling swap as per [Swap memory - #2 by OldhamMade](https://community.fly.io/t/swap-memory/2749/2) – was getting memory issues a bit after startup without swap, but seems to be much more stable with swap
