3 Ways to Create Spring Boot Docker Images Without Docker File

Introduction

  1. Spring Boot build plugin
  2. Fabric8 Maven Plugin
  3. JIB maven plugin

Creating an Application

@RestController
@SpringBootApplication
public class ServiceDockerImageBuildApplication {

public static void main(String[] args) {
SpringApplication.run(ServiceDockerImageBuildApplication.class, args);
}

@GetMapping("/")
public ResponseEntity get(){
return ResponseEntity.ok("All Ok");
}
}

Building Docker Images using Spring Boot Build Plugin

mvn spring-boot:build-image
===> DETECTING
[INFO] [creator] 6 of 24 buildpacks participating
[INFO] [creator] paketo-buildpacks/ca-certificates 3.2.4
[INFO] [creator] paketo-buildpacks/bellsoft-liberica 9.3.7
[INFO] [creator] paketo-buildpacks/syft 1.12.0
[INFO] [creator] paketo-buildpacks/executable-jar 6.2.4
[INFO] [creator] paketo-buildpacks/dist-zip 5.2.4
[INFO] [creator] paketo-buildpacks/spring-boot 5.12.0
...
...
[INFO] Successfully built image 'docker.io/library/service-docker-image-build:0.0.1-SNAPSHOT'
mvn spring-boot:build-image \
-Dspring-boot.build-image.imageName=my-spring-boot-image
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<imageName>my-spring-boot-image</imageName>
</configuration>
</plugin>

Building Docker images using Fabric8 Maven Plugin

<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.40.1</version>
<configuration>
<images>
<image>
<name>${project.name}:${project.version}</name>
<build>
<from>openjdk:17</from>
<assembly>
<name>build</name>
<descriptorRef>artifact</descriptorRef>
</assembly>
<ports>
<port>8080</port>
</ports>
<cmd>java -jar build/${project.name}-${project.version}.jar</cmd>
</build>
</image>
</images>
</configuration>
<executions>
<execution>
<id>build-image</id>
<phase>verify</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>

Building Docker images using Google’s JIB Plugin

<build>
<plugins>
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<from>
<image>openjdk:17</image>
</from>
<to>
<image>jib-build-${project.name}:${project.version}</image>
</to>
</configuration>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>dockerBuild</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
  • Layer 1: Contains all the dependencies.
  • Layer 2: Contains the application resources.
  • Layer 3: Contains the application classes.
  • Layer 4: Contains the jib-classpath-file containing classpath information and the jib-main-class-file containing the name of the fully qualified main class that will be executed.

Conclusion

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Amrut Prabhu

Amrut Prabhu

Software Craftsman, Tech Enthusiast. I run https://refactorfirst.com to post all my articles