Containerization -Spring Security - JWT Authentication - Series 3
- Anand Nerurkar
- Apr 13, 2022
- 2 min read
Let us add containerization for our application. For that we have to make use of Docker and create image ,push that image to docker hub. Once image is pushed, we can pull it on any host running docker runtime and run the container for the application.
Containerization is important steps in the Cloud Native Application Development and play important role in Cloud Agnostic Design.
Assumption :
You have docker runtime installed on your machine and login to your docker runtime.
Prerequisite Knowledge
Spring Boot
Java 8
Maven
H2 DB
Spring Security
Eclipse IDE
Docker Runtime
Docker knowledge
Docker Hub Account
Tutorial updated codebase - https://github.com/anerurkar/jwt-demo/tree/master
Docker Workflow Diagram

1 Update pom.xml file with below plugin entry for docker plugin and updating jar file name.
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.4.3</version>
<executions>
<execution>
<id>default</id>
<goals>
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
<configuration>
<repository>anandn76/${project.artifactId}</repository>
<tag>${project.version}</tag>
</configuration>
<dependencies>
<!-- To make this work on JDK 9+ -->
<dependency>
<groupId>javax.activation</groupId>
<artifactId>javax.activation-api</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
</plugin>
<finalName>spring-boot-jwt-demo</finalName>
Please note that for docker plugin , we have set the build ,push goals as shown above in green color. Also using docker hub repository anandn76 for this example.
2. Create Docker file with name as Dockerfile only.
# Start with a base image containing Java runtime
FROM java:8
# Make port 8080 available to the world outside this container
EXPOSE 8080
ADD target/spring-boot-jwt-demo.jar spring-boot-jwt-demo.jar
# Run the jar file
ENTRYPOINT ["java","-jar","spring-boot-jwt-demo.jar"]
3. Build & Run the project
Select your project, right click -> Run as- Maven Build- Configure the goals as clean package dockerfile:push

Click Apply- Run
It should build your project, build image and push image to your docker hub repository docker.io/anandn76/project.artifactid
Please see below sample console log for your reference.
Building jar: C:\Users\andyn\git\repository\jwt-demo\target\spring-boot-jwt-demo-docker-info.jar
[INFO] Successfully built anandn76/spring-boot-jwt-demo:0.0.1-SNAPSHOT
[INFO]
[INFO] --- dockerfile-maven-plugin:1.4.3:push (default-cli) @ spring-boot-jwt-demo ---
[INFO] The push refers to repository [docker.io/anandn76/spring-boot-jwt-demo]
[INFO] Image 190553039a3f: Preparing
[INFO] Image 35c20f26d188: Preparing
[INFO] Image c3fe59dd9556: Preparing
[INFO] Image 6ed1a81ba5b6: Preparing
[INFO] Image a3483ce177ce: Preparing
[INFO] Image ce6c8756685b: Preparing
[INFO] Image 30339f20ced0: Preparing
[INFO] Image ce6c8756685b: Waiting
[INFO] Image 0eb22bfb707d: Preparing
[INFO] Image 30339f20ced0: Waiting
[INFO] Image a2ae92ffcd29: Preparing
[INFO] Image 0eb22bfb707d: Waiting
[INFO] Image a2ae92ffcd29: Waiting
[INFO] Image 35c20f26d188: Layer already exists
[INFO] Image c3fe59dd9556: Layer already exists
[INFO] Image 6ed1a81ba5b6: Layer already exists
[INFO] Image a3483ce177ce: Layer already exists
[INFO] Image 190553039a3f: Pushing
[INFO] Image 0eb22bfb707d: Layer already exists
[INFO] Image ce6c8756685b: Layer already exists
[INFO] Image 30339f20ced0: Layer already exists
[INFO] Image a2ae92ffcd29: Layer already exists
[INFO] Image 190553039a3f: Pushed
[INFO] 0.0.1-SNAPSHOT: digest: sha256:2093bd3d42a9defd86f7c154ec4dc7febf87eee56a12017c7cb887473fd1d2a1 size: 2212
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
4. Please login to your docker hub account and see image has been pushed to repository.


5. Goto your docker terminal and enter below command to pull image to your local machine
docker pull anandn76/spring-boot-jwt-demo:0.0.1-SNAPSHOT

6. Now use run command to run container for the application with below command ,can see container is started for the application successfully.

Conclusion
We have successfully started container for the application and it is running on 9092 port.
Comments