DEV Community

Cover image for Guide to Free Hosting for Your Full-Stack Spring Boot Application
Philip
Philip

Posted on • Updated on

Guide to Free Hosting for Your Full-Stack Spring Boot Application

Introduction πŸ“œ

When it comes to building your own applications, motivation is often lost because, in the end, your free-time applications will mostly be used by yourself. Even though platforms like GitHub exist, it would be fancier if we just provided a link to our fully working application.

In this article we will learn how to containerize our app with Docker and host both the service and the database on Render for free.

The project πŸ“š

For this example, we will try to host a full stack web application called Book Eater. This is a website where you can search and request refurbished books, as well as offer your own old ones you've been telling yourself you were going to finish for years now. We are not going to analyze the domain structure, although we will inspect the overall architecture of the project.

The application consists of :

  • A frontend with plain HTML, BootstrapCSS and some JS πŸ’»
  • A Spring Boot API πŸƒ
  • A PostgreSQL database 🐘

Image description

Step 1 : Dockerfile

What is a container? πŸ“¦

Containers are virtual environments that package an application and its dependencies, making it easy to run consistently across different computing environments. Imagine them as a virtual machine, but with only the essential software your application needs to run. In our case, the container will have our project files and an installed version of Java.

What is Docker? 🐳

Docker is a platform that uses containers to package applications and their dependencies, ensuring they run consistently across different environments. Imagine we work in a team where some developers use Windows and others use Linux. A common problem that occurs is: "But it works on my machine." A simple solution would be for everyone to use the same machine. That's exactly what Docker does with containers. It simulates the exact same machine for developers to run their code, while still on their main machine.

Lets containerize our project! πŸ“₯

In the source folder of your Spring Boot project (not to be confused with the src folder), create a file called Dockerfile. In this file, we are going to instruct Docker on how to run our application.

Image description

First, we need to install Java to run our Spring Boot app. We can do this by installing a Java image.Images are read-only templates used by containers. They can be anything from simple commands to language versions, database drivers, etc. We install an image in our container with the FROM command. This is also referred to as pulling an image. Images can be found on DockerHub.

Let's pull a Java 17 image..



FROM openjdk:17-jdk-alpine


Enter fullscreen mode Exit fullscreen mode

Next, we need to provide the container with the file we want to run. In our case, this is the project's jar fileπŸ«™. A jar file is like a ZIP file that contains compiled Java code (*.class) or source code (.java). We can generate the jar file for our project with Maven by executing the mvn package command. This will create the target folder, which contains our .jar file.

To copy the jar file into our container, we use the COPY command. We tell Docker that our jar file is located inside the target folder. *jar tells Docker to copy all files with the .jar extension. This is very handy because we can reuse it with any Java project since we only have one jar file in the target folder and can ignore its name. Finally, we give the file a good name, like app.jar.



COPY target/*.jar app.jar


Enter fullscreen mode Exit fullscreen mode

To run a jarfile we use the ENTRYPOINT command. To run a jarfile with java we add the -jar flag.



ENTRYPOINT ["java","-jar","/app.jar"]


Enter fullscreen mode Exit fullscreen mode

Finally, because we run a Spring Boot app, we need to open port 8080 for the Tomcat webserver to be hosted. This is done using the EXPOSE command.



EXPOSE 8080



Enter fullscreen mode Exit fullscreen mode

All in one Dockerfile



FROM openjdk:17-jdk-alpine
COPY target/*.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
EXPOSE 8080


Enter fullscreen mode Exit fullscreen mode

If you are using IntelliJ, you can run the Docker file directly. Docker will install the Java image from DockerHub and run the Spring Boot app. If you are using Eclipse, don't.

Image description

Installation of images:

Image description

Image description

Our Spring Boot project successfully runs in a Docker container πŸ₯³πŸ₯³πŸ₯³.

Step 2 : Hosting our Database πŸ—ƒοΈ

For the hosting of our database we are going to use Render.com. Render is a fully-managed cloud platform where you can host static sites, databases, background workers and more.

Hosted Database on Render

Once you've signed up, select New > PostgreSQL.

Image description

Fill in the form with your desired database name, username, and region. Make sure to select the region closest to you.

Once you are done, go to the connections tab of your database. You will see your hostname, username, and internal and external database URLs. To connect your database with your Spring Boot app, you need to update the datasource parameters in the application.properties file.



spring.jpa.properties.hibernate.transaction.jta.platform=org.hibernate.engine.transaction.jta.platform.internal.AtomikosJtaPlatform
spring.datasource.url=jdbc:postgresql://your_url
spring.datasource.username=your_username
spring.datasource.password=your_password
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create


Enter fullscreen mode Exit fullscreen mode

In the your_url section, you need to copy and paste the External Database URL from the "@" symbol onward (do not include the "@" symbol).

Try running your Spring Boot app now. You'll notice it takes a bit more time to connect to the database. If everything works as expected, change your ddl-auto property to update instead of create. This is because when we redeploy our app, we don't want the database to be recreated, which would cause our current data to be lost! 🚨

Step 3 : Hosting our Spring Boot app βš™οΈπŸ›œ

Render offers a free tier for Web Service Hosting. It also provides a link for your web application so that you and others can access it.

Click New > Web Service. Select Build and deploy from a Git repository, which allows us to directly add and update our project from GitHub.

Once you connect your repository, select Docker as the running environment. In the Root Directory field, enter the name of the project's folder.

We are now ready to deploy our application. Click the Deploy button if the deployment hasn't already started. This will take some time since we are using the free tier and the bandwidth is limited. You will be notified when the deployment is done.

Image description

Each time you push new changes to the repository, Render will automatically re-deploy your latest version.

Congrats πŸŽ‰πŸŽ‰πŸŽ‰

You have successfully containerized and hosted your application and its database for free. Now you can easily share your projects and impress your friends! You have also learned quite a few things about containers, Docker, and images.

Top comments (0)