<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Philip</title>
    <description>The latest articles on DEV Community by Philip (@philipathanasopoulos).</description>
    <link>https://dev.to/philipathanasopoulos</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1779548%2Fe9c87d94-ddf7-4761-9070-e66051ab065b.jpeg</url>
      <title>DEV Community: Philip</title>
      <link>https://dev.to/philipathanasopoulos</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/philipathanasopoulos"/>
    <language>en</language>
    <item>
      <title>Create a simple and fast search bar using cosine similarity in React ⚛️🔍</title>
      <dc:creator>Philip</dc:creator>
      <pubDate>Mon, 07 Oct 2024 08:37:51 +0000</pubDate>
      <link>https://dev.to/philipathanasopoulos/create-a-simple-and-fast-search-bar-using-cosine-similarity-in-react-10a6</link>
      <guid>https://dev.to/philipathanasopoulos/create-a-simple-and-fast-search-bar-using-cosine-similarity-in-react-10a6</guid>
      <description>&lt;h2&gt;
  
  
  Introduction 📜
&lt;/h2&gt;

&lt;p&gt; 
In this article, we are going to explore cosine similarity and how to use it to implement a very simple yet effective search bar. Although the topic includes a bit of math, the implementation will be as simplistic as possible, yet we are still going to cover exactly how things work under the hood.
&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem 🤔
&lt;/h2&gt;

&lt;p&gt;
If you are here, you are probably wondering, "How do I search for things in the first place?".
The answer to this question lies in a field of computer science known as &lt;b&gt;Data Retrieval&lt;/b&gt;. We are going to use one of the most fundamental methods used in data retrieval which, while not perfect, is surprisingly easy to implement and yields satisfying results.
&lt;/p&gt;

&lt;h2&gt;
  
  
  Cosine Similarity 🧮
&lt;/h2&gt;

&lt;p&gt;Cosine similarity is a measure used to calculate how similar two vectors are. In simple terms, it is the cosine of the angle formed by the two vectors in space. Consider the following vectors:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvven1agkj4uzk69i3m8e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvven1agkj4uzk69i3m8e.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example, the cosine similarity between vectors A and B is the cosine of angle α, which is cos(26.57°) = 0.8943&lt;br&gt;
This means that the vectors are quite similar.&lt;/p&gt;

&lt;p&gt;We can also use this method to calculate the similarity of words.&lt;br&gt;
Imagine we had words that could only have the letters A and B.&lt;/p&gt;

&lt;p&gt;Word 1: "aaab"&lt;br&gt;&lt;br&gt;
Word 2: "abb"&lt;br&gt;&lt;br&gt;
Users input word: "aab"&lt;/p&gt;

&lt;p&gt;The x-Axis will represent the amount of As and y-Axis the amount of Bs in each word.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsxtbc6dmjtkqskj687p1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsxtbc6dmjtkqskj687p1.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At first glance, we can already see that our input "aab" is much closer to "aaab" than to "abb". Let's go ahead and calculate the cosine of each angle:&lt;br&gt;
cos(8.13°) = 0.98&lt;br&gt;&lt;br&gt;
cos(36.86°) = 0.80&lt;/p&gt;

&lt;p&gt;This is how we can represent words in a 2D space. But this by itself is not useful. In our world, we use more than just "a" and "b" characters. The English alphabet alone has 26 base characters. Imagine we wanted to support special characters as well, or even multiple languages.&lt;br&gt;
Here we will need to use the general formula for cosine similarity:&lt;/p&gt;

&lt;p&gt;Here, vectors A and B are n-dimensional. Note that they should have the same dimensions.&lt;/p&gt;

&lt;p&gt;We will compare the following words using this formula:&lt;/p&gt;

&lt;p&gt;Word 1 : "banana"&lt;br&gt;&lt;br&gt;
Word 2 : "apple"&lt;br&gt;&lt;br&gt;
User input: "bana"&lt;/p&gt;

&lt;p&gt;For each word, we will create a 26-dimensional vector. Each slot represents the frequency of the n-th letter of the alphabet in a given word.  &lt;/p&gt;

&lt;p&gt;"banana": [3,1,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0]&lt;br&gt;&lt;br&gt;
"apple":    [1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,2,0,0,0,0,0,0,0,0,0,0]&lt;br&gt;&lt;br&gt;
"bana":      [2,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0]&lt;/p&gt;

&lt;p&gt;cos(bana,banana) = 0.982&lt;br&gt;&lt;br&gt;
cos(bana,apple) = 0.309&lt;/p&gt;

&lt;p&gt;Therefore, the word "bana" is more closely related to "banana" than to "apple".&lt;/p&gt;
&lt;h2&gt;
  
  
  Enough math yapping, time to code 💻
&lt;/h2&gt;

&lt;p&gt;We are going to create a cosine similarity search bar in React.&lt;br&gt;
Let's start:&lt;/p&gt;

&lt;p&gt;Create a new React app&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npx create-react-app search-bar-app&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Create a new SearchBar.tsx file&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxjjw7oq3rfr6nb8mdlrw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxjjw7oq3rfr6nb8mdlrw.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is our main component.&lt;br&gt;
Add the &lt;code&gt;SearchBar&lt;/code&gt; component inside &lt;code&gt;App.js&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import logo from './logo.svg';
import './App.css';
import { SearchBar } from './SearchBar.tsx';

function App() {
  return (
    &amp;lt;div className="App"&amp;gt;
      &amp;lt;header className="App-header"&amp;gt;
        &amp;lt;div&amp;gt;
          &amp;lt;SearchBar/&amp;gt;
        &amp;lt;/div&amp;gt;
      &amp;lt;/header&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From your terminal, install the compute-cosine-similarity package&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i compute-cosine-similarity
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's start building our component.  &lt;/p&gt;

&lt;p&gt;We need 3 variables: one for the query given by the user, one for the results to be displayed back to them, and finally the list of searchable items.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [query, setquery] = useState&amp;lt;string&amp;gt;("");
const [results, setResults] = useState&amp;lt;string[]&amp;gt;([]);
const items = [
        'Apples',
        'Bananas',
        'Carrots',
        'Dairy Milk',
        'Eggs',
        'Flour',
        'Grapes',
        'Honey',
        'Ice Cream',
        'Juice',
        'Kale',
        'Lettuce',
        'Milk',
        'Nuts',
        'Oranges',
        'Pasta',
        'Quinoa',
        'Rice',
        'Spinach',
        'Tomatoes',
        'Udon Noodles',
        'Vanilla Extract',
        'Watermelon',
        'Yogurt',
        'Zucchini'
    ];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Time for our component's logic.  &lt;/p&gt;

&lt;p&gt;When a user provides new input, we have to calculate the cosine similarity between the input and every word in our list. Then we return the list of items ordered by cosine similarity in decreasing order.  &lt;/p&gt;

&lt;p&gt;To calculate the similarity, for each word we construct an array of size 26. Each position in the array represents a letter of the English alphabet. For each character of a word, if the character is an English character, we increase the value of the character's position by 1.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function isEnglishAlphabet(character: string): boolean {
            return /^[a-zA-Z]$/.test(character);
}

function calculateCosineSimilarity(firstWord:string, secondWord:string):number {
            const firstWordMatrix = new Array(26).fill(0);
            const secondWordMatrix = new Array(26).fill(0);

            for (let character of firstWord) {
                if (isEnglishAlphabet(character)) {
                    const index = character.toLowerCase().charCodeAt(0) - 97;
                    firstWordMatrix[index]++;
                }
            }

            for (let character of secondWord) {
                if (isEnglishAlphabet(character)) {
                    const index = character.toLowerCase().charCodeAt(0) - 97;
                    secondWordMatrix[index]++;
                }
            }

            console.log(cosineSimilarity(firstWordMatrix, secondWordMatrix))
            return cosineSimilarity(firstWordMatrix, secondWordMatrix) || 0;
        }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice how we access the position of the letter in the matrix. If it is an English character, we get its ASCII value and subtract 97, which is the ASCII value of the letter "a".  &lt;/p&gt;

&lt;p&gt;We can now use this method on every item to return the results to the user. For each item, we create a pair of its name and cosine similarity with the user input. Then we filter out the items that are less than 0.5 similar and finally order by similarity.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function search():void {
            let newResults = items.map(item =&amp;gt;({
                name: item,
                similarity: calculateCosineSimilarity(query,item)
            }))
            .filter(item =&amp;gt; item.similarity &amp;gt; 0.5)
            .sort((a, b) =&amp;gt; b.similarity - a.similarity)
            .map(item =&amp;gt; item.name);

            setResults(newResults);
        }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can gather all the methods above in a single &lt;code&gt;useEffect&lt;/code&gt; that runs whenever the user input changes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;useEffect(() =&amp;gt; {

        function isEnglishAlphabet(character: string): boolean {
            return /^[a-zA-Z]$/.test(character);
        }

        function calculateCosineSimilarity(firstWord:string, secondWord:string):number {
            const firstWordMatrix = new Array(26).fill(0);
            const secondWordMatrix = new Array(26).fill(0);

            for (let character of firstWord) {
                if (isEnglishAlphabet(character)) {
                    const index = character.toLowerCase().charCodeAt(0) - 97;
                    firstWordMatrix[index]++;
                }
            }

            for (let character of secondWord) {
                if (isEnglishAlphabet(character)) {
                    const index = character.toLowerCase().charCodeAt(0) - 97;
                    secondWordMatrix[index]++;
                }
            }

            console.log(cosineSimilarity(firstWordMatrix, secondWordMatrix))
            return cosineSimilarity(firstWordMatrix, secondWordMatrix) || 0;
        }

        function search():void {
            let newResults = items.map(item =&amp;gt;({
                name: item,
                similarity: calculateCosineSimilarity(query,item)
            }))
            .filter(item =&amp;gt; item.similarity &amp;gt; 0.5)
            .sort((a, b) =&amp;gt; b.similarity - a.similarity)
            .map(item =&amp;gt; item.name);

            setResults(newResults);
        }

        search();
    }, [query])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For the final step, we need to create the view of our component. We'll use a simple input that holds the value of "query" and whenever the value inside changes, the query will be updated. This will trigger the &lt;code&gt;useEffect&lt;/code&gt; we created. We will map the results in a list just below the search bar.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;return (
        &amp;lt;div&amp;gt;
            &amp;lt;input 
                type="text" 
                placeholder="Search..." 
                value={query} 
                onChange={(e) =&amp;gt; setquery(e.target.value)} 
            /&amp;gt;
            &amp;lt;ul&amp;gt;
                {results.map((item, index) =&amp;gt; (
                    &amp;lt;li key={index}&amp;gt;{item}&amp;lt;/li&amp;gt;
                ))}
            &amp;lt;/ul&amp;gt;
        &amp;lt;/div&amp;gt;
    );
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Final result ✨🔍
&lt;/h2&gt;

&lt;p&gt;Go ahead and type something into the search bar. The top results should be very relevant to what you are looking for!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw0ig1cwdpqb073lrn8ud.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw0ig1cwdpqb073lrn8ud.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Full search bar component code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useEffect, useState } from 'react';
import cosineSimilarity from 'compute-cosine-similarity';


export const SearchBar = () =&amp;gt; {
    const [query, setquery] = useState&amp;lt;string&amp;gt;("");
    const [results, setResults] = useState&amp;lt;string[]&amp;gt;([]);
    const items = [
        'Apples',
        'Bananas',
        'Carrots',
        'Dairy Milk',
        'Eggs',
        'Flour',
        'Grapes',
        'Honey',
        'Ice Cream',
        'Juice',
        'Kale',
        'Lettuce',
        'Milk',
        'Nuts',
        'Oranges',
        'Pasta',
        'Quinoa',
        'Rice',
        'Spinach',
        'Tomatoes',
        'Udon Noodles',
        'Vanilla Extract',
        'Watermelon',
        'Yogurt',
        'Zucchini'
    ];

    useEffect(() =&amp;gt; {

        function isEnglishAlphabet(character: string): boolean {
            return /^[a-zA-Z]$/.test(character);
        }

        function calculateCosineSimilarity(firstWord:string, secondWord:string):number {
            const firstWordMatrix = new Array(26).fill(0);
            const secondWordMatrix = new Array(26).fill(0);

            for (let character of firstWord) {
                if (isEnglishAlphabet(character)) {
                    const index = character.toLowerCase().charCodeAt(0) - 97;
                    firstWordMatrix[index]++;
                }
            }

            for (let character of secondWord) {
                if (isEnglishAlphabet(character)) {
                    const index = character.toLowerCase().charCodeAt(0) - 97;
                    secondWordMatrix[index]++;
                }
            }

            console.log(cosineSimilarity(firstWordMatrix, secondWordMatrix))
            return cosineSimilarity(firstWordMatrix, secondWordMatrix) || 0;
        }

        function search():void {
            let newResults = items.map(item =&amp;gt;({
                name: item,
                similarity: calculateCosineSimilarity(query,item)
            }))
            .filter(item =&amp;gt; item.similarity &amp;gt; 0.5)
            .sort((a, b) =&amp;gt; b.similarity - a.similarity)
            .map(item =&amp;gt; item.name);

            setResults(newResults);
        }

        search();
    }, [query])

    return (
        &amp;lt;div&amp;gt;
            &amp;lt;input 
                type="text" 
                placeholder="Search..." 
                value={query} 
                onChange={(e) =&amp;gt; setquery(e.target.value)}
            /&amp;gt;
            &amp;lt;ul&amp;gt;
                {results.map((item, index) =&amp;gt; (
                    &amp;lt;li key={index}&amp;gt;{item}&amp;lt;/li&amp;gt;
                ))}
            &amp;lt;/ul&amp;gt;
        &amp;lt;/div&amp;gt;
    );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Guide to Free Hosting for Your Full-Stack Spring Boot Application</title>
      <dc:creator>Philip</dc:creator>
      <pubDate>Sun, 14 Jul 2024 12:59:45 +0000</pubDate>
      <link>https://dev.to/philipathanasopoulos/guide-to-free-hosting-for-your-full-stack-spring-boot-application-4fak</link>
      <guid>https://dev.to/philipathanasopoulos/guide-to-free-hosting-for-your-full-stack-spring-boot-application-4fak</guid>
      <description>&lt;h2&gt;
  
  
  Introduction 📜
&lt;/h2&gt;

&lt;p&gt;Ever wanted to host your applications for free?&lt;br&gt;
In this article we will learn how to containerize your Spring Boot app with Docker and host both the service and the database on Render.&lt;/p&gt;
&lt;h2&gt;
  
  
  The project 📚
&lt;/h2&gt;

&lt;p&gt;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 analyse the domain structure, although we will inspect the overall architecture of the project.&lt;/p&gt;

&lt;p&gt;The application consists of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A frontend with HTML, Bootstrap CSS and JS &lt;/li&gt;
&lt;li&gt;A Spring Boot API 🍃&lt;/li&gt;
&lt;li&gt;A PostgreSQL database 🐘&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd5rj1cx69gsjbmyaoecv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd5rj1cx69gsjbmyaoecv.png" alt="Image description" width="800" height="1061"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 1 : Dockerfile
&lt;/h2&gt;
&lt;h2&gt;
  
  
  What is a container? 📦
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;
&lt;h2&gt;
  
  
  What is Docker? 🐳
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;
&lt;h2&gt;
  
  
  Lets containerize our project! 📥
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc2wwsnc5ob80772pavbr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc2wwsnc5ob80772pavbr.png" alt="Image description" width="454" height="222"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Let's pull a Java 17 image..&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM openjdk:17-jdk-alpine
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;COPY target/*.jar app.jar
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To run a jarfile we use the ENTRYPOINT command. To run a jarfile with java we add the -jar flag.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ENTRYPOINT ["java","-jar","/app.jar"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;EXPOSE 8080

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All in one Dockerfile&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM openjdk:17-jdk-alpine
COPY target/*.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
EXPOSE 8080
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fufasa1827s447s88c2xi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fufasa1827s447s88c2xi.png" alt="Image description" width="424" height="144"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Installation of images:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fto1mijp5pkv4mbns8w0u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fto1mijp5pkv4mbns8w0u.png" alt="Image description" width="800" height="397"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu0zevngv6y4vec6sqmou.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu0zevngv6y4vec6sqmou.png" alt="Image description" width="800" height="423"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our Spring Boot project successfully runs in a Docker container 🥳🥳🥳.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2 : Hosting our Database 🗃️
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Hosted Database on Render&lt;/p&gt;

&lt;p&gt;Once you've signed up, select New &amp;gt; PostgreSQL.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsrpv6vs5lwvxowqijsys.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsrpv6vs5lwvxowqijsys.png" alt="Image description" width="341" height="404"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Fill in the form with your desired database name, username, and region. Make sure to select the region closest to you.&lt;/p&gt;

&lt;p&gt;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.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;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! 🚨&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3 : Hosting our Spring Boot app ⚙️🛜
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Click New &amp;gt; Web Service. Select Build and deploy from a Git repository, which allows us to directly add and update our project from GitHub.&lt;/p&gt;

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

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3gzpdrvtl1k0grabqlw7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3gzpdrvtl1k0grabqlw7.png" alt="Image description" width="800" height="385"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Each time you push new changes to the repository, Render will automatically re-deploy your latest version.&lt;/p&gt;

&lt;h2&gt;
  
  
  Congrats 🎉🎉🎉
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
