Skip to content

Docker With SpringBoot#

Build Docker Image With Executable Jar File#

  • In the previous section Spring Boot With Executable Jar, we learnt how to build an executable jar file from Spring Boot project. Now we will continue to build a docker image with an executable jar file and then we can use this image to deploy our spring boot application on any server which support container technology.

Create A Docker File#

  • So firstly, we need to write a Dockerfile. A Dockerfile is a text file that contains the instructions for building a Docker image. A Docker image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings.
  • So after building the Spring Boot application and got the executable jar file. We will create a Dockerfile as below.
Dockerfile
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# Use the base image with Java 8 and Alpine Linux
FROM openjdk:8

# Set the working directory in the container
WORKDIR /app

# Copy the compiled Spring Boot application JAR file into the container
COPY target/json-schema-validator-0.0.1-SNAPSHOT.jar /app/json-schema-validator-0.0.1-SNAPSHOT.jar

# Expose the port that the Spring Boot application listens on
EXPOSE 8080

# Set the command to run the Spring Boot application when the container starts
CMD ["java", "-jar", "-Dspring.profiles.active=dev", "json-schema-validator-0.0.1-SNAPSHOT.jar"]
Commands Description
FROM openjdk:8 This specifies the base image to use for the Docker image. In this case, it is using the official OpenJDK 8 image with Alpine Linux as the base operating system. This image provides Java 8 runtime environment.
WORKDIR /app This sets the working directory inside the container to /app. It's a good practice to use a specific directory for your application.
COPY target/json-schema-validator-0.0.1-SNAPSHOT.jar /app/json-schema-validator-0.0.1-SNAPSHOT.jar This copies the compiled Spring Boot application JAR file from the target directory of the host machine into the /app directory of the container. The source file is json-schema-validator-0.0.1-SNAPSHOT.jar in the target directory, and the destination is /app/json-schema-validator-0.0.1-SNAPSHOT.jar in the container.
EXPOSE 8080 This declares that the Spring Boot application listens on port 8080 inside the container. It does not actually publish the port to the host machine, but it provides a hint to users of the image about the exposed port.
CMD ["java", "-jar", "-Dspring.profiles.active=dev", "json-schema-validator-0.0.1-SNAPSHOT.jar"] This is the command that will be executed when the container starts. It runs the Spring Boot application using the java -jar command. The -Dspring.profiles.active=dev part sets the active Spring profile to dev. The json-schema-validator-0.0.1-SNAPSHOT.jar is the JAR file that will be executed.

Build Docker Image From Dockerfile#

  • Afer creating a Dockerfile, we will use the command below to build a docker image.
1
docker build -f <Dockerfile Name> -t <image name:version>

Ex:

1
docker build . -t json-schema-validator:0.0.1
  • The . at the end of the command tells Docker to build the image from the current directory. You can view more info in Docker Commands And Descriptions

  • Now, we can use command docker images to check the image that we have just built.

1
2
3
4
duc@duc-MS-7E01:~$ docker images
REPOSITORY                    TAG              IMAGE ID       CREATED         SIZE
json-schema-validator         0.0.1            f21d3db8026e   7 days ago      575MB
json-schema-validator         0.0.1-SNAPSHOT   e1725659dbcf   7 days ago      575MB

Test Docker Image#

  • Finally, let's use the command below to test our docker image that we have just build.
1
docker run -p 8080:8080 -d json-schema-validator:0.0.1-SNAPSHOT
  • This command will run a container with using the json-schema-validator image and expose it on port 8080 on the host machine.
  • We can use the command docker ps to check the container is created or not.
duc@duc-MS-7E01:~$ docker ps
CONTAINER ID   IMAGE                                  COMMAND                  CREATED              STATUS              PORTS                                       NAMES
2cf8f7065fce   json-schema-validator:0.0.1-SNAPSHOT   "java -jar -Dspring.…"   About a minute ago   Up About a minute   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp   angry_goldstine
  • Then we can use command docker logs <containerId> to view the log of container.
1
docker logs 2cf8f7065fce

 #zoom

See Also#

References#