Introduction
A Dockerfile is a script that having the command and while deployment the docker image container We will get up and running service which configured with commands.
Prerequisites
- Basic knowledge of Docker.
- Basic knowledge of Linux commands.
Step 1: Create a SpringBoot Directory
To begin, we set up a directory just for SpringBoot-related files by employing the mkdir command.
mkdir springboot
Step 2: Create a Dockerfile
Now that we have created a folder, we can use the nano editor to create a Dockerfile within that folder:
nano Dockerfile
Paste the following commands.
FROM openjdk:8-jdk-alpine
COPY target/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","/app.jar"]
Save and exit from the nano text editor.
This Dockerfile uses the openjdk:8-jdk-alpine image as the base image, which is a minimal version of the OpenJDK 8 runtime environment. It then uses the COPY command to copy the Spring Boot jar file (generated by the build process) into the container. The EXPOSE command tells Docker that the container will listen on port 8080. The ENTRYPOINT command sets the command and parameters that will be executed when the container is run. This command runs the Java application using the -jar option and specifying the path to the app.jar file.
Note that you need to build your Spring Boot application and generate the jar file before building the image. Also, you may want to configure the application for your specific needs, such as setting environment variables, adding health checks, and so on.
Step 3: Building Spring Boot Docker Image
The docker build command is now used to construct the Dockerfile. within which we give our image a custom name (such as springboot_image) and tag it with the 1.0.0 suffix.
docker build -t springboot_image:1.0.0 .
The docker images command should be used to verify the image’s existence after it has been constructed.
We can see a list of all the images that have been created or obtained from any public or private registry using the docker images command.
Step 4: Deploy Spring Boot App Container
Run the image locally as a container after it has been built:
We use detached mode to run the container continuously in the background. In the docker run command, include -d.
We provide port 8080. To have the server run on localhost, use -p 8080:8080.
As a result, the docker run command also uses the image and the tag that goes with it as input to run the container.
docker run --name custom_springboot -d -p 8080:8080 springboot_image:1.0.0
Step 5: Testing Spring Boot Test Page
Go to any local browser and type localhost to see if the Apache server is present.
Conclusion
We have successfully build Spring Boot custom image from scratch dockerfile on ubuntu 22.04 LTS, If you still have questions, please post them in the comments section below.