Scope
This post will list down the steps to run a Spring Boot (or any runnable jar) in a Docker container.
O/S: Ubuntu 20 LTS
Docker version: 20.10
After the development and unit testing is completed, build the runnable jar (using Maven clean package install goals or equivalent)
Build Dockerfile
Copy the jar to a clean directory of Ubuntu host. I keep all files at /home/pandian/feedparser. FeedParser-2.jar is my jar file name.
Create a Dockerfile (in the same name, where your jar exists) using your favorite editor.
# Alpine Linux with OpenJDK JRE
FROM openjdk:8-jre-alpine
# copy JAR into image
COPY FeedParser-2.jar /FeedParser-2.jar
# run application with this command line
CMD ["/usr/bin/java", "-jar", "-Dspring.profiles.active=default", "/FeedParser-2.jar"]
The lines starts with # are comments.
Firstly, it fetches a unix container with openjdk:8
Secondly, it copies our executable jar to / of the container
Finally, it runs our jar file
After you save and exit the editor, following is what you will see in this directory.
$ ls -alt
total 17680
drwxrwxr-x 2 pandian pandian 4096 Dec 19 03:24 .
drwxr-xr-x 8 pandian pandian 4096 Dec 19 03:20 ..
-rw-rw-r-- 1 pandian pandian 250 Dec 19 03:20 Dockerfile
-rw-r--r-- 1 pandian pandian 18091416 Dec 19 03:18 FeedParser-2.jar
Build your docker image
We need to build our image by issuing the following command
$ sudo docker build -t feedparser:latest /home/pandian/feedparser
feedparser is the image name, latest is the tag name. It will build the image by executing the commands provided in Dockerfile one by one.
$ sudo docker build -t feedparser:latest /home/pandian/feedparser
Sending build context to Docker daemon 18.09MB
Step 1/3 : FROM openjdk:8-jre-alpine
8-jre-alpine: Pulling from library/openjdk
e7c96db7181b: Pull complete
f910a506b6cb: Pull complete
b6abafe80f63: Pull complete
Digest: sha256:f362b165b870ef129cbe730f29065ff37399c0aa8bcab3e44b51c302938c9193
Status: Downloaded newer image for openjdk:8-jre-alpine
---> f7a292bbb70c
Step 2/3 : COPY FeedParser-2.jar /FeedParser-2.jar
---> 7fb6417e9838
Step 3/3 : CMD ["/usr/bin/java", "-jar", "-Dspring.profiles.active=default", "/FeedParser-2.jar"]
---> Running in 44b4e9443e63
Removing intermediate container 44b4e9443e63
---> 23879d1fc20e
Successfully built 23879d1fc20e
Successfully tagged feedparser:latest
If you list the images, you will see a new image by name feedparser got added.
$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
feedparser latest 23879d1fc20e 2 minutes ago 103MB
hello-world latest feb5d9fea6a5 2 months ago 13.3kB
openjdk 8-jre-alpine f7a292bbb70c 2 years ago 84.9MB
Run the container
Last step is to run the container. Issue the following command.
$ sudo docker run feedparser
This command will kick start the application in a container.
$ sudo docker run feedparser
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/FeedParser-2.jar!/BOOT-INF/lib/logback-classic-1.2.7.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/FeedParser-2.jar!/BOOT-INF/lib/slf4j-simple-1.7.32.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
19:31:10.253 [main] INFO org.grassfield.feed.parser.FeedParserApplication - Starting FeedParser
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.6.1)
19:31:11.508 [main] INFO o.g.f.parser.FeedParserApplication:55 - Starting FeedParserApplication v2 using Java 1.8.0_212 on 867ffa00ea49 with PID 1 (/FeedParser-2.jar started by root in /)