- Build a Jar
target/docker-hello-world.jar
- Setup the Prerequisites for Running the JAR
docker run -dit openjdk:11
- Copy the jar
docker container cp target/docker-hello-world.jar <<container-name>>:/tmp
docker container commit --change='CMD ["java","-jar","/tmp/docker-hello-world.jar"]' <<container-name>> docker-hello-world:latest
- Run the jar
docker run -p 8080:8080 docker-hello-world:latest
Detached mode
docker run -p 8080:8080 -d docker-hello-world:latest
Hello World
{"message":"Hello World"}
{"message":"Hello World, in28minutes"}
############## Basic
- Add a new file at project level: "Dockerfile"
- Add the following to the file
FROM openjdk:11
EXPOSE 8080
ADD target/docker-hello-world.jar docker-hello-world.jar
ENTRYPOINT ["sh", "-c", "java -jar /docker-hello-world.jar"]
- Build the Docker Image using command
docker build -t docker-hello-world:wrkspc .
- Run the Docker Container using
docker run -p 8080:8080 -d docker-hello-world:wrkspc
With Spring Boot 2.3.0 we can create images while running maven build - mvn spring-boot:build-image
######## we need to create images not from the fat jar but the class files and dependencies ############Follow below steps to achieve the above
- Add a plugin for this type of build
FROM openjdk:11
ARG DEPENDENCY=target/dependency
COPY ${DEPENDENCY}/BOOT-INF/lib /app/lib
COPY ${DEPENDENCY}/META-INF /app/META-INF
COPY ${DEPENDENCY}/BOOT-INF/classes /app
ENTRYPOINT ["java","-cp","app:app/lib/*","com.tanmay.docker.dockerhelloworld.DockerHelloWorldApplication"]
- Add the following dependency in pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>mave n-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
- Run maven command for image creation
mvn spring-boot:build-image
########################### PLUGINS
- Remove the Dockerfile
- Add the below maven dependency
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>1.6.1</version>
<configuration>
<container>
<creationTime>USE_CURRENT_TIMESTAMP</creationTime>
</container>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>dockerBuild</goal>
</goals>
</execution>
</executions>
</plugin>
Extra Configurations which makes it similar to Dockerfile
<configuration>
<from>
<image>openjdk:alpine</image>
</from>
<to>
<image>in28min/${project.name}</image>
<tags>
<tag>${project.version}</tag>
<tag>latest</tag>
</tags>
</to>
<container>
<jvmFlags>
<jvmFlag>-Xms512m</jvmFlag>
</jvmFlags>
<mainClass>com.in28minutes.rest.webservices.restfulwebservices.RestfulWebServicesApplication</mainClass>
<ports>
<port>8100</port>
</ports>
</container>
</configuration>