SpringBoot with Docker

Before we use docker inside the spring boot application first we need to install the Docker Desktop. Please follow below steps for the installation.

  1. 1. Go to this link Install Docker Desktop download docker desktop
  2. 2. Then, double-click on the Docker Desktop Installer.exe to run the installer.
  3. 3. Once you start the installation process, always enable Hyper-V Windows Feature on the Configuration page.
  4. 4. Then, follow the installation process to allow the installer and wait till the process is done.
  5. 5. After completion of the installation process, click Close and restart.
  6. 6. Open the Docker Desktop
  7. 7. Before we signin into Docker Desktop create or login the Docker Hub link
  8. 8. We can see all the deployed containers and images from the Docker Desktop.

What is Docker

Docker is an open-source containerization platform that allows you to package your application and its dependencies into a portable container. In this article, we will explore how to containerize a Spring Boot application using Docker.

First we need to create the spring boot application. Please refer the following SpringBoot CRUD Example session to create a sample spring boot application. Once we created the sample spring boot application then in the same application next we need to create the docker file.

Creating a Dockerfile

A Dockerfile is a text file that contains a set of instructions for creating a Docker image. In this section, we will create a Dockerfile for our Spring Boot application. Create a new file called Dockerfile in the root directory of your Spring Boot application. Here’s an example Dockerfile:


                        FROM openjdk:17-jdk-slim
                        WORKDIR /opt/app
                        ARG JAR_FILE=target/sb-crud-with-docker-example-0.0.1-SNAPSHOT.jar
                        COPY ${JAR_FILE} app.jar
                        ENTRYPOINT ["java","-jar","app.jar"]

                    

Let’s go through each line of the Dockerfile

  1. 1. FROM openjdk:17-jdk-slim: This line specifies the base image to use for the Docker image. In this case, we are using the official OpenJDK 17 image.
  2. 2. WORKDIR /opt/app /tmp: This will Change the working directory to /opt/app in container.
  3. 3. ${JAR_FILE} app.jar: This line copies the JAR sb-crud-with-docker-example-0.0.1-SNAPSHOT.jar file to /opt/app/app.jar this.
  4. 4. ENTRYPOINT ["java","-jar","app.jar"]: This will run the jar file with speficied command in container.

Build the docker image

Now that we have created a Dockerfile, we can use it to build a Docker image for our Spring Boot application.

Open a terminal or command prompt and navigate to the root directory of your Spring Boot application. Run the following command to build the Docker image:


                    docker build -t app .
                  

This command will build a Docker image with the tag app. The . at the end of the command specifies the build context, which is the current directory.

Running a Containerized Application

To run a containerized application, we need to start a Docker container using the Docker image that we built in the previous step.

Run the following command to start a new Docker container:


                        docker run -p 8086:8086 app
                    

This command starts a new Docker container using the app image and maps port 8086 in the container to port 8086 on the host machine. The containerized application should now be accessible at http://localhost:8086/api/v1/customer/getAll

Managing Containerized Applications

Docker provides a powerful set of tools for managing containerized applications. Here are some of the most useful Docker commands for managing containers:

  1. - docker ps: This command lists all running Docker containers.
  2. - docker stop <container-id>: This command stops a running Docker container.
  3. - docker rm <container-id>: This command removes a stopped Docker container.
  4. - docker images: This command lists all Docker images on the local.
utilising Docker to containerize a Spring Boot application offers a number of advantages, including portability, scalability, and simplicity in deployment. Creating a Dockerfile for our Spring Boot application, producing a Docker image, and running a containerized application were all covered in this article. A few helpful Docker commands for managing containers were also briefly covered. We can streamline the deployment process and improve the scalability and resilience of our Spring Boot application by containerizing it.

Full source code is available in follwong GitHub repository: SpringBoot with Docker