This post documents the steps I performed to containerize my REST application built with Spring Boot.
This uses the following tools
- Spring Tool Suite 4
- Maven – STS inbuilt
- Open JDK (Built by Azul)
- Docker
Step 1: Build your spring boot application using Maven:
This will build the file and jar file will be copied to the target folder of your project folder.
[INFO] --- maven-jar-plugin:3.1.2:jar (default-jar) @ messenger --- [INFO] Building jar: D:\Pandian\Documents\workspace_hamsa\messenger\target\messenger-0.0.1-SNAPSHOT.jar [INFO] [INFO] --- spring-boot-maven-plugin:2.1.8.RELEASE:repackage (repackage) @ messenger --- [INFO] Replacing main artifact with repackaged archive [INFO] [INFO] --- maven-install-plugin:2.5.2:install (default-install) @ messenger --- [INFO] Installing D:\Pandian\Documents\workspace_hamsa\messenger\target\messenger-0.0.1-SNAPSHOT.jar to C:\Users\barat\.m2\repository\org\grassfield\messenger\0.0.1-SNAPSHOT\messenger-0.0.1-SNAPSHOT.jar [INFO] Installing D:\Pandian\Documents\workspace_hamsa\messenger\pom.xml to C:\Users\barat\.m2\repository\org\grassfield\messenger\0.0.1-SNAPSHOT\messenger-0.0.1-SNAPSHOT.pom [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 13.659 s [INFO] Finished at: 2019-10-12T08:57:43+08:00 [INFO] ------------------------------------------------------------------------
You may see above. The jar file messenger-0.0.1-SNAPSHOT.jar is created in the target folder.
Step 2: Create Dockerfile
We start building our own container now. We need to define the docker configuration in a Dockerfile. It will be placed in the root of project folder.
Following is the Docker file for this project.
#this uses alpine base image with openjdk 8 FROM openjdk:8-jdk-alpine #Maintainer contact details MAINTAINER Murugapandian Ramaiah #where to persist the data generated by the container. VOLUME /tmp #which port of this container is to be exposed outside EXPOSE 8080 #specify the SpringBoot bulky jar ARG JAR_FILE=target/messenger-0.0.1-SNAPSHOT.jar ADD ${JAR_FILE} messenger.jar #specify how to execute the application ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom", "-jar", "/messenger.jar"]
Step 3: Build the container image
SSH the project folder to the /tmp partition of docker host Execute the following command.
$ cd /tmp/messenger $ docker build -t messenger . Step 1/7 : FROM openjdk:8-jdk-alpine ---> a3562aa0b991 Step 2/7 : MAINTAINER Murugapandian Ramaiah ---> Using cache ---> 4bb0470845c7 Step 3/7 : VOLUME /tmp ---> Using cache ---> 232d70e90d54 Step 4/7 : EXPOSE 8080 ---> Using cache ---> 0a1d2e61d4a4 Step 5/7 : ARG JAR_FILE=target/messenger-0.0.1-SNAPSHOT.jar ---> Using cache ---> f179766aa87f Step 6/7 : ADD ${JAR_FILE} messenger.jar ---> bc76f586ac0b Step 7/7 : ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom", "-jar", "/messenger.jar"] ---> Running in 0646cdbee037 Removing intermediate container 0646cdbee037 ---> 574eb9b9004b Successfully built 574eb9b9004b Successfully tagged messenger:latest
The container image is created
$ docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE messenger latest 574eb9b9004b 23 minutes ago 123MB openjdk 8-jdk-alpine a3562aa0b991 5 months ago 105MB pandian/centos_ssh latest 7f019be4424c 14 months ago 294MB centos latest 5182e96772bf 14 months ago 200MB ubuntu latest 735f80812f90 14 months ago 83.5MB hello-world latest 2cb0d9787c4d 15 months ago 1.85kB
messenger is added.
Step 4: run the container.
$ docker run -p 6666:8080 messenger . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.1.8.RELEASE) 2019-10-12 07:11:42.372 INFO 1 --- [ main] org.grassfield.msg.MessengerApplication : Starting MessengerApplication v0.0.1-SNAPSHOT on 0f9f70c91b73 with PID 1 (/messenger.jar started by root in /) 2019-10-12 07:11:42.375 INFO 1 --- [ main] org.grassfield.msg.MessengerApplication : No active profile set, falling back to default profiles: default 2019-10-12 07:11:43.394 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2019-10-12 07:11:43.430 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2019-10-12 07:11:43.431 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.24] 2019-10-12 07:11:43.530 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2019-10-12 07:11:43.530 INFO 1 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1118 ms 2019-10-12 07:11:43.758 INFO 1 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2019-10-12 07:11:44.005 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2019-10-12 07:11:44.008 INFO 1 --- [ main] org.grassfield.msg.MessengerApplication : Started MessengerApplication in 2.072 seconds (JVM running for 2.438)
I endorse this post by Rajeev – https://www.callicoder.com/spring-boot-docker-example/

Dont miss this carving in Temple of tooth, Kandy (Summer 2019)