Dockerizing Spring Boot Applications
Docker has emerged as a crucial technology in contemporary software development for containerizing apps, guaranteeing consistency across environments, and streamlining deployment. The steps to Dockerize a basic Java program and a Spring Boot application with a MySQL database included will be explained in this blog. After learning the fundamentals of Docker, we will go over practical examples of how to Dockerize Java and Spring Boot apps and MySQL. Dockerizing Spring Boot Applications: Learn how to containerize your Spring Boot apps with Docker for seamless deployment, scalability, & efficient management.
Comprehending Docker and Its Fundamental Ideas
It’s crucial to comprehend what Docker is and the basic ideas that make it such a potent tool for developers before we begin Dockerizing our apps.
1. Docker: What is it?
Applications inside lightweight containers can be automatically deployed, scaled, and managed with the help of the open-source Docker platform. An application’s code, runtime, libraries, environment variables, and configuration files are all included in a container, which is a self-sufficient, portable package.
2. Fundamental Ideas in Docker:
Containers:
Containers are lightweight, isolated environments in which programs operate. Containers are more effective and quicker to start than virtual machines since they share the host operating system’s kernel.
Images:
A Docker image is a template that is read-only and used to generate containers.
It includes all of the elements required for the program to operate, including the runtime, libraries, and code.
Container instances are created using images.
Docker Engine:
The management of container lifecycles is handled by the Docker Engine. It is made up of a server, a REST API, and a command-line interface (CLI) for interaction and control.
Docker Hub:
The default registry contains an extensive library of pre-built Docker images. Here, you can also share and re-use your own photos.
Docker Architecture:
Docker works with client-server architecture, in which the Docker Client communicates with the Docker Daemon. The Daemon
handles the creation, running, and management of containers. Docker images are stored in the Docker Registry and are used to create Docker containers. This setup ensures that containers can be created in a consistent and repeatable way.
Crucial Docker commands:
To use Docker effectively, you need to be familiar with some of the most often used commands:
docker build : Using this command we can build a Docker image from a Dockerfile.
docker run: Using this docker run command we can create and start a container from a specific image.
docker ps: Provides a list of running containers.
docker exec:The command executes within an active container. docker stop: Terminates a running container.
docker-compose: manages apps across several containers with a simple YAML configuration file.
Dockerfile:
A Dockerfile is a specialized script that outlines a series of instructions for creating a Docker image. It contains the base image, the working directory, and any necessary steps to set up the application environment, such as copying data or installing dependencies.
This is an illustration of a basic Dockerfile for a Java program:
Ex:
FROM eclipse-temurin:17-jdk
WORKDIR /app
COPY bin/com/Employee.class /app/com/Employee.class ENTRYPOINT [“java”, “com/Employee”]
Docker Compose:
The definition and administration of multi-container Docker applications are made easier with the help of Docker Compose. It enables you to manage the lifespan of numerous Docker containers with a single command and to specify their configuration in a single file. When your application needs multiple containers to operate (such as a web server, database, cache, etc.), Docker Compose is especially helpful.
Now it’s time to install Docker so that we can Dockerize our Java and Spring Boot applications.
Install Docker:
Step 1: You can go to this link to download and install Docker Desktop: https://www.docker.com/products/docker-desktop/. Choose the “Download for Windows (AMD64)” option.
Step 2: Run the Docker installer (exe file), and your Docker Desktop will be ready to use.
Preparing Your Java and Spring Boot Applications:
Once you’re familiar with the Docker concepts, it’s time to Dockerize a Java program and a Spring Boot application. Let’s break down the steps for each.
Dockerizing a Simple Java Program
For a simple Java program, let’s say we have a basic “Shop” Java application. We will create a Docker image to run this Java application inside a container.
For Free, Demo classes Call: 020-71173125
Registration Link: Java Classes in Pune!
1. Create a Simple Java Program (Shop.java):
public class Shop {
int id;
String name;
Shop(){}
Shop(int id,String name)
{
this.id=id;
this.name=name;
}
void display()
{
System.out.println(id+” “+name);
}
public static void main(String[] args) {
Shop shop=new Shop(1001,”General Store”); shop.display();
}
}
2. Create a Dockerfile for the Java program in the project:
FROM eclipse-temurin:17-jdk
WORKDIR /app
COPY bin/com/Shop.class /app/com/Shop.class ENTRYPOINT [“java”, “com/Shop”]
3. Build the Docker Image:
docker build -t “Shopapp:latest” .
4. Run the Container:
docker run Shopapp:latest
This will execute your Java program inside the container and print Shop details, Docker! to the console.
The output is shown below.
Dockerizing a Spring Boot Application
Spring Boot simplifies Java application development, especially for microservices. Dockerizing a Spring Boot application is very similar to Dockerizing any other Java application but with a few Spring Boot-specific adjustments.
1. Create a Spring Boot Application:
Create a Spring boot app using the Spring Starter project and select dependencies like Spring Web and Spring Data JPA, MySql if you need database integration.
2. Add a Dockerfile for the Spring Boot app:
Dockerfile
FROM eclipse-temurin:17-jdk
WORKDIR /app
COPY target/*.jar ./app.jar
ENTRYPOINT [“java”, “-jar”,”app.jar”]
3. Build the Application:
First, compile the Spring Boot application using Maven.
4. Now First pull mysql image and make a mysql container of database configuration:
docker pull mysql
docker run –name mysqlcontainer -e
MYSQL_ROOT_PASSWORD=root -e
MYSQL_DATABASE=dockerdatabase -d mysql:latest
5. Build the Docker Image for the spring-boot app:
docker build -t “projecttest:v2”.
6. Run the Docker Container:
docker run –name myspringboottestapp –link
mysqlcontainer:mysql -e
SPRING_DATASOURCE_URL=jdbc:mysql://mysql:3306/dockerd atabase -e SPRING_DATASOURCE_USERNAME=root -e SPRING_DATASOURCE_PASSWORD=root -p 8081:8081 -d projecttest:v1
Now, you can access the Spring Boot application by visiting http://localhost:8081 in your browser.
Conclusion
Dockerizing Java and Spring Boot applications not only simplifies the development but also ensures consistency and portability across different environments. Whether you’re working with a simple Java application or a complex Spring Boot microservice, Docker provides a standardized environment that can be easily replicated anywhere.
By understanding the core Docker concepts—such as containers, images, Dockerfiles and Docker Hub—you can efficiently containerize your applications, integrate them with databases like MySQL, and streamline your development workflow.
This structure provides a clear progression from Docker basics to applying Docker to real-world Java and Spring Boot applications. The step-by-step guidance helps readers understand how to effectively containerize their projects and integrate them with databases like MySQL.
To explore more do visit: Click Here
Author:-
Kiran Tiwari
Call the Trainer and Book your free demo Class For Java Call now!!!
| SevenMentor Pvt Ltd.
© Copyright 2021 | SevenMentor Pvt Ltd.