Build Docker Image of Spring Boot Application

Priyank Agarwal
3 min readAug 18, 2021

Docker is becoming a more important tool in the DevOps process. We will see how we can build a docker image of the spring boot application and go through Docker commands.

Prerequisite:

You must have installed docker in your machine.

Follow the below steps to generate a docker image.

  1. Make it ready spring boot application with the support of maven. For me, I already created a sample spring boot application. You can clone the spring boot app by using this link: Spring Boot App.
  2. Sample spring boot application runs on port 8080. You can run the jar file locally and test the endpoint by hitting URL: http://localhost:8080/ping.
  3. Afterward, While cloning there is also Dockerfile cloned. We will see each command role and how we can build a docker image.
  4. Fire a below build command to build the image.
docker build -t sample-app:1.0 .

Here -t tends to tag followed by sample-app (image name) and its version 1.0.

Here below explained docker command written in a Dockerfile.

  1. Define base image for java application. This image is already stored in the docker hub. We are using the existing image for the project. You can also find different images for different projects and you use them as base images.
FROM openjdk:8-jdk-alpine as build

2. Define environment variable as maven version. You can define different versions based on your needs.

ENV MAVEN_VERSION 3.5.4

3. Define environment variable for maven directory where maven source file stores.

ENV MAVEN_HOME /usr/lib/mvn

4. Define maven path with bin directory. This is the same as what we are defining locally maven home path in environment variables.

ENV PATH $MAVEN_HOME/bin:$PATH

5. Below command will download maven and placed them into containers. Different commands are appended by &&. They will download maven unzip it and remove the zip file from the container and move the unzipped folder to maven defined location respectively.

RUN wget http://archive.apache.org/dist/maven/maven-3/$MAVEN_VERSION/binaries/apache-maven-$MAVEN_VERSION-bin.tar.gz && \
tar -zxvf apache-maven-$MAVEN_VERSION-bin.tar.gz && \
rm apache-maven-$MAVEN_VERSION-bin.tar.gz && \
mv apache-maven-$MAVEN_VERSION /usr/lib/mvn

6. Define working directory where java application stores.

WORKDIR /workspace/app

7. Copying pom.xml file into the container.

COPY pom.xml .

8. Copying source file into a container in the src folder.

COPY src src

9. Running maven command to download all dependencies for the project.

RUN mvn install -DskipTests

10. Create a target folder where jar file stores after build.

RUN mkdir -p target/dependency && (cd target/dependency; jar -xf ../*.jar)

11. Defined variable that passed at build time for application dependency. It is used in the next commands for layers.

ARG DEPENDENCY=/workspace/app/target/dependency

12. Here we defined layers. When the application code is changed then it will be rebuilt only those layers which are changed. It won’t be rebuilt all layers. It saves time for building an image. Here we defined three layers.

For more information about layers please follow this link: Spring Boot Layers

COPY — from=build ${DEPENDENCY}/BOOT-INF/lib /app/lib
COPY — from=build ${DEPENDENCY}/META-INF /app/META-INF
COPY — from=build ${DEPENDENCY}/BOOT-INF/classes /app

13. Define entry point for java spring boot application where a main () method is defined.

ENTRYPOINT [“java”,”-cp”,”app:app/lib/*”,”com.app.sample.SampleAppApplication”]

14. Expose port number for docker container. Docker container runs on specified port number.

EXPOSE 8080

Run Docker Image

Hit the below command to run the docker image.

docker container run --name java-app -p 8765:8080 sample-app:1.0

Here,- -name tends to container name which is java-app followed by -p means exposed port 8765 externally, mapped to internal port which is 8080. You can expose different ports. In the last, we are specifying the image name and its version. After running this command ping service can be accessed via this URL: http://localhost:8765/ping.

Docker Command Cheat Sheet

You can download the docker cheat sheet command by using the following link: Cheat Sheet Link

--

--