Guide to Free Hosting for Your Full-Stack Spring Boot Application
Published: 2024-07-14 12:59:45

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kv2vpqfu7cfgchll5qze.png)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c2wwsnc5ob80772pavbr.png)



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

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

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"]
```

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

```
All in one Dockerfile

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

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ufasa1827s447s88c2xi.png)



Installation of images:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/to1mijp5pkv4mbns8w0u.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u0zevngv6y4vec6sqmou.png)




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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/srpv6vs5lwvxowqijsys.png)



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

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3gzpdrvtl1k0grabqlw7.png)



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.