With Docker, we can create portable Java CLI applications for various environments by pre-building container images. This could make deployment a lot easier and less messy in particularly in Production environments. This post briefly demonstrates how to create a Docker image with a Java CLI application.
Contents
Requirements
We used the following for this post.
- Docker for Windows (Docker Desktop Community)
- OpenJDK 8
- SpringBoot
- Maven
The CLI Application
The application is a Spring Boot application generated via Spring Initializr in IntelliJ IDEA. It has this simple main class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.turreta.cli.docker.comturretaclidocker; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import java.util.concurrent.TimeUnit; @SpringBootApplication public class ComTurretaCliDockerApplication { public static void main(String[] args) throws InterruptedException { SpringApplication.run(ComTurretaCliDockerApplication.class, args); System.out.println("######################################"); System.out.println("In Java CLI App in a Docker container"); System.out.println("######################################"); TimeUnit.SECONDS.sleep(30); } } |
Nothing fancy.
pom.xml
The pom.xml file has these information:
1 2 3 4 5 6 |
... <groupId>com.turreta.cli.docker</groupId> <artifactId>com-turreta-cli-docker</artifactId> <version>0.0.1-SNAPSHOT</version> <name>com-turreta-cli-docker</name> ... |
DockerFile
We need a DockerFile file. Create it in the same project directory. It is used to build our desired Docker image locally.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# Pull base image. FROM openjdk:8 LABEL maintainer="www.turreta.com" RUN mkdir /app ARG JAR_FILE ENV JAVA_XMS=512 JAVA_XMX=1024 COPY /target/${JAR_FILE}.jar /app/app.jar WORKDIR /app ENTRYPOINT java -Xms${JAVA_XMS}m -Xmx${JAVA_XMX}m \ -Djava.security.egd=file:/dev/./urandom -jar app.jar |
Build, Run to Test
Build Project
First we need to build our Maven project using the following command to create the jar file in the target directory.
1 |
clean package |
Build Docker Image
Next, we invoke the following command in command-prompt in the same directory where the DockerFile is. Note that we specified the artifactId and version. The file name of the generated jar file is also used here.
1 |
docker build -t turreta.com/cli-docker:0.0.1-SNAPSHOT --build-arg JAR_FILE=com-turreta-cli-docker-0.0.1-SNAPSHOT . |
We will then get the following console output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
build-arg JAR_FILE=com-turreta-cli-docker-0.0.1-SNAPSHOT . Sending build context to Docker daemon 7.742MB Step 1/8 : FROM openjdk:8 ---> 27da2af61908 Step 2/8 : LABEL maintainer="www.turreta.com" ---> Running in 3b4301a1033c Removing intermediate container 3b4301a1033c ---> dcb5f766bfa6 Step 3/8 : RUN mkdir /app ---> Running in 39dc6d724012 Removing intermediate container 39dc6d724012 ---> 209ab82f3a87 Step 4/8 : ARG JAR_FILE ---> Running in ae2cd6744a1f Removing intermediate container ae2cd6744a1f ---> d7e6b4574348 Step 5/8 : ENV JAVA_XMS=512 JAVA_XMX=1024 ---> Running in cdb1a009e38b Removing intermediate container cdb1a009e38b ---> db7156ee44b5 Step 6/8 : COPY /target/${JAR_FILE}.jar /app/app.jar ---> c5fc31c99f98 Step 7/8 : WORKDIR /app ---> Running in a388e2b5a010 Removing intermediate container a388e2b5a010 ---> 94c0e98e72a2 Step 8/8 : ENTRYPOINT java -Xms${JAVA_XMS}m -Xmx${JAVA_XMX}m -Djava.security.egd=file:/dev/./urandom -jar app.jar ---> Running in 8d362c78c7e1 Removing intermediate container 8d362c78c7e1 ---> 075f8831395c Successfully built 075f8831395c Successfully tagged turreta.com/cli-docker:0.0.1-SNAPSHOT SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories. |
Run the Application
Now we can ran our application using the following command.
1 |
docker run --rm turreta.com/cli-docker:0.0.1-SNAPSHOT |
The --rm option is to automatically clean up the container and remove the file system when the container exits. Please see https://docs.docker.com/engine/reference/run/.
This post is part of the Docker For Developers tutorial.