Spring Boot – Creating Docker Image

Docker is a container management service that eases building and deployment.

In this chapter, we are going to see How to create a Docker image by using Maven and Gradle dependencies for your Spring Boot application.

1. Create Dockerfile

First, create a file with the name Dockerfile under the directories src/main/docker with the contents shown below. Note that this file is important to create a Docker image.

FROM java:8
VOLUME /tmp
ADD dockerapp-0.0.1-SNAPSHOT.jar app.jar
RUN bash -c 'touch /app.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

2. Maven

For Maven, add the Docker Maven plugin into your build configuration file pom.xml

<properties>
   <docker.image.prefix>spring-boot-maixuanviet</docker.image.prefix>
</properties>

<build>
   <plugins>
      <plugin>
         <groupId>com.spotify</groupId>
         <artifactId>docker-maven-plugin</artifactId>
         <version>1.0.0</version>
         
         <configuration>
            <imageName>${docker.image.prefix}/${project.artifactId}</imageName>
            <dockerDirectory>src/main/docker</dockerDirectory>
            <resources>
               <resource>
                  <directory>${project.build.directory}</directory>
                  <include>${project.build.finalName}.jar</include>
               </resource>
            </resources>
         </configuration>
      </plugin>
      
      <plugin>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
   </plugins>
   
</build>

The complete pom.xml file is given below −

<?xml version = "1.0" encoding = "UTF-8"?>
<project xmlns = "http://maven.apache.org/POM/4.0.0" 
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 
   http://maven.apache.org/xsd/maven-4.0.0.xsd">
   
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.maixuanviet</groupId>
   <artifactId>dockerapp</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>
   <name>dockerapp</name>
   <description>Demo project for Spring Boot</description>

   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.5.9.RELEASE</version>
      <relativePath /> <!-- lookup parent from repository -->
   </parent>

   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <java.version>1.8</java.version>
      <docker.image.prefix>spring-boot-maixuanviet</docker.image.prefix>
   </properties>

   <dependencies>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>
   </dependencies>

   <build>
      <plugins>
         <plugin>
            <groupId>com.spotify</groupId>
            <artifactId>docker-maven-plugin</artifactId>
            <version>1.0.0</version>
            
            <configuration>
               <imageName>${docker.image.prefix}/${project.artifactId}</imageName>
               <dockerDirectory>src/main/docker</dockerDirectory>
               <resources>
                  <resource>
                     <directory>${project.build.directory}</directory>
                     <include>${project.build.finalName}.jar</include>
                  </resource>
               </resources>
            </configuration>
         </plugin>
         
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>
   
</project>

Now, you can run your application by using the Maven command mvn package docker:build

MVN Package Docker Build

Note − Enable the Expose daemon on tcp://localhost:2375 without TLS.

After build success, you can see the output on the console as shown below −

MVN Package Docker Output

Now, see the Docker images by the command using docker images and see the image info on the console.

Docker Images Command

3. Gradle

To build a Docker image by using Gradle build configuration, we need to add the docker plugin and need to write a task buildDocker to create a Docker image.

The code for Gradle Docker configuration is given below.

buildscript {
   .....
   dependencies {
      .....
      classpath('se.transmode.gradle:gradle-docker:1.2')
   }
}

group = 'spring-boot-maixuanviet'

.....
apply plugin: 'docker'

task buildDocker(type: Docker, dependsOn: build) {
   applicationName = jar.baseName
   dockerfile = file('src/main/docker/Dockerfile')
   doFirst {
      copy {
         from jar
         into stageDir
      }
   }
}

The complete build.gradle file is given below.

buildscript {
   ext {
      springBootVersion = '1.5.9.RELEASE'
   }
   repositories {
      mavenCentral()
   }
   dependencies {
      classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
      classpath('se.transmode.gradle:gradle-docker:1.2')
   }
}

group = 'spring-boot-maixuanviet'

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'docker'

group = 'com.maixuanviet'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
   mavenCentral()
}
dependencies {
   compile('org.springframework.boot:spring-boot-starter-web')
   testCompile('org.springframework.boot:spring-boot-starter-test')
}
task buildDocker(type: Docker, dependsOn: build) {
   applicationName = jar.baseName
   dockerfile = file('src/main/docker/Dockerfile')
   doFirst {
      copy {
         from jar
         into stageDir
      }
   }
}

Now, create a Docker image by using the command shown below −

gradle build buildDocker
Gradle Build Docker

After executing the command, you can see the BUILD SUCCESSFUL log on the console window.

Docker Build Successful

Now, see the Docker images by the command using docker images and see the image’s info on the console.

Get Image Info Using DockerImages

Related posts:

Spring’s RequestBody and ResponseBody Annotations
Jackson – Decide What Fields Get Serialized/Deserialized
Stack Memory and Heap Space in Java
How to Round a Number to N Decimal Places in Java
Java Program to Implement LinkedBlockingDeque API
Java Program to Create a Minimal Set of All Edges Whose Addition will Convert it to a Strongly Conne...
Introduction to Liquibase Rollback
Java Program to Implement D-ary-Heap
Java Program to Implement LinkedBlockingQueue API
Java Program to Implement Expression Tree
So sánh HashMap và HashSet trong Java
Java Program to Find the Shortest Path from Source Vertex to All Other Vertices in Linear Time
Array to String Conversions
Java toString() Method
Java Program to Implement the Binary Counting Method to Generate Subsets of a Set
Spring Security Custom AuthenticationFailureHandler
Java Program to Implement Find all Forward Edges in a Graph
Error Handling for REST with Spring
Introduction to Eclipse Collections
Spring Boot Change Context Path
REST Web service: Tạo ứng dụng Java RESTful Client với Jersey Client 2.x
Java Program to Check whether Directed Graph is Connected using BFS
Java Program to implement Priority Queue
Giới thiệu Aspect Oriented Programming (AOP)
Java Program to Implement Ford–Fulkerson Algorithm
Pagination and Sorting using Spring Data JPA
Giới thiệu Java 8
Java Program to Find Inverse of a Matrix
Deploy a Spring Boot WAR into a Tomcat Server
What is Thread-Safety and How to Achieve it?
Using Java Assertions
SOAP Web service: Upload và Download file sử dụng MTOM trong JAX-WS