Docker file for a Spring Boot Application
This guide would provide a detailed overview of a Dockerfile and every component of it.
- In the root of your project, create a new file named- Dockerfile
- This file icon would auto update to the Docker logo.
Remember
Dockerfile → Used to build the image of your Application
Docker-compose.yaml → Used to run the image as a container in your system
Dockerfile - an Overview
- By writing a docker we have written and indicated various instructions which should be followed while building the docker image of our application. These images encapsulate everything needed to run an application, including the code, libraries, dependencies, and runtime environment.
- A Dockerfile serves as the blueprint for building a Docker image. It consists of a sequence of commands that the Docker daemon processes to assemble the image. When you run the
docker buildcommand, Docker reads the instructions in the Dockerfile line by line to create the image
Post mvn clean install you would find a .jar file in the /target directory of your project.
Example Dockerfile 1
FROM openjdk:22-oracle COPY target/*.jar helloworld.jar EXPOSE 8081 ENTRYPOINT [ "java","-jar","helloworld.jar"
Dockerfile Code and Syntax Breakdown
1. FROM openjdk:22-oracle
- Purpose: This line specifies the base image for your Docker image.
- Details:
openjdk: This indicates that the image is based on the OpenJDK (Java Development Kit) environment.22-oracle: This specifies the version of OpenJDK to use. JDK 22 is used in the application.
2. COPY target/*.jar helloworld.jar
- Purpose: This line copies the JAR file from your local machine → target folder into the Docker image.
- Details:
target/*.jar: This specifies the source path, wheretargetis the directory in your project where the compiled JAR file is placed after mvn clean install.- The
*.jarwildcard means it will copy any JAR file found in that directory. helloworld.jar: This is the destination path inside the Docker image. The JAR file will be renamed tohelloworld.jarwithin the image.- Renaming it makes it easier to reference the file later in the
ENTRYPOINT.
3. EXPOSE 8081
- Purpose: This line informs Docker that the container listens on the specified network port at runtime.
- Details:
8081: This is the port number that your application will be accessible on when the container is running. Note that this does not publish the port; it only serves as documentation and allows for easier configuration of network settings when running the container.
4. ENTRYPOINT [ "java","-jar","helloworld.jar" ]
- Purpose: This line sets the command that will be executed when the container starts.
- Details:
ENTRYPOINT: This instruction configures a container to run as an executable. It defines the command that will be executed when the container starts.[ "java","-jar","helloworld.jar" ]: This is the command and its arguments in JSON array format.java: This is the Java runtime command.-jar: This option tells Java to run a JAR file.helloworld.jar: This is the name of the JAR file that was copied into the image. It is the main application that will be executed.
Summary
In summary, this Dockerfile sets up an environment to run a Spring Boot or a Java application packaged as a JAR file. It uses OpenJDK as the base image, copies the application JAR into the image, exposes a port for communication, and specifies the command to run the application when the container starts.
Alternative docker file code
FROM openjdk:17-jdk COPY target/springboottest-0.0.1-SNAPSHOT.jar EXPOSE 8081 ENTRYPOINT[ "java","-jar","springboottest-0.
Key differences between the 2 files -
Copying JAR Files
First Snippet
COPY target/*.jar helloworld.jar
- Wildcard Usage: This line uses a wildcard (
*.jar) to copy any JAR file found in thetargetdirectory and renames it tohelloworld.jar. This approach is flexible, as it allows for any JAR file in the target directory to be used without specifying its exact name.
Second Snippet
COPY target/springboottest-0.0.1-SNAPSHOT.jar
- Specific JAR File: This line explicitly specifies the exact JAR file to copy (
springboottest-0.0.1-SNAPSHOT.jar). This means that the Dockerfile is tailored for a specific build of the application, and if the JAR file name changes, the Dockerfile will need to be updated.
The first snippet is more flexible regarding the JAR file it can use, while the second snippet is tailored for a specific application build with a defined JAR name. The choice between these two snippets would depend on the specific requirements of your application and the desired Java version.