Step By Step Spring Boot Docker Deployment Example

By | 2019-02-03T12:56:33+00:00 February 3rd, 2019|Spring Boot|0 Comments

In this tutorials, we are going to see a Step by Step Spring Boot Docker deployment process.

Spring Boot Docker Deployment:

Here I am going to create a simple spring boot hello world application and deploy it on docker engine.

Technologies:

  • Spring Boot 2.1.2
  • Java 8
  • Docker 18.09

Pre Requisites:

Application Structure:

Spring Boot Docker Example-min

Dependencies:

No separate dependencies required for docker.

pom.xml
<?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>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.onlinetutorialspoint</groupId>
  <artifactId>Spring-boot-docker</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>Spring-boot-docker</name>
  <description>Demo project for Spring Boot Docker Deployment</description>
  <properties>
    <java.version>1.8</java.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
    <finalName>spring-boot-docker</finalName>
  </build>
</project>

Creating a Simple Spring Boot Rest Controller.

HelloController.java
package com.onlinetutorialspoint.Springbootdocker;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @GetMapping("/hello")
    public String hello(){
        return "Hello from docker";
    }
}

Create Docker Configuration file.

Docerfile
FROM openjdk:8
ADD target/spring-boot-docker.jar spring-boot-docker.jar
EXPOSE 8090
ENTRYPOINT ["java", "-jar", "spring-boot-docker.jar"]

FROM: is used to get the image from docker hub. Here we are getting the openjdk:8 version 8 from docker hub as our application is java based.

ADD: is used to adding the application jar (/target/spring-boot-docker.jar) file to Docker container.

EXPOSE: is used to define the port on which the container should expose.

ENTRYPINT: is used by the docker to start the application.

Spring Boot Main class.

SpringBootDockerApplication.java
package com.onlinetutorialspoint.Springbootdocker;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootDockerApplication {

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

}

Build the Application using maven: It will generate spring-boot-docker.jar file and placed on /target folder.

Terminal
E:\work\Spring-boot-docker>mvn clean install
[INFO] Scanning for projects...
[INFO]
[INFO] ------------< com.onlinetutorialspoint:Spring-boot-docker >-------------
[INFO] Building Spring-boot-docker 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ Spring-boot-docker ---
[INFO] Deleting E:\work\Spring-boot-docker\target
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ Spring-boot-docker ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ Spring-boot-docker ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to E:\work\Spring-boot-docker\target\classes
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ Spring-boot-docker ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:\work\Spring-boot-docker\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ Spring-boot-docker ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ Spring-boot-docker ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:3.1.1:jar (default-jar) @ Spring-boot-docker ---
[INFO] Building jar: E:\work\Spring-boot-docker\target\spring-boot-docker.jar

Creating Docker Image :

Step 1: Build the application using Docker. Go to the terminal and execute the below docker commands.

docker build -f Dockerfile -t spring-boot-docker
E:\work\Spring-boot-docker>docker build -f Dockerfile -t spring-boot-docker .

Using the docker build command we are going to executing the -f Dockerfile (which we defined as part of docker file configuration) and creating tag with the name of -t spring-boot-docker, and the (.) dot tells where the Dockerfile exist, for our case the Dockerfile available in current working directory so that given as dot.

Terminal
Sending build context to Docker daemon  16.89MB
Step 1/4 : FROM openjdk:8
8: Pulling from library/openjdk
ab1fc7e4bf91: Pull complete
35fba333ff52: Pull complete
f0cb1fa13079: Pull complete
3d1dd648b5ad: Pull complete
a9f886e483d6: Pull complete
4346341d3c49: Pull complete
006f2208d67a: Pull complete
fb85cf26717d: Pull complete
Digest: sha256:6d881fb5c8dbc6ad1e9392ce35e289afb53b3148450848fb8f6aabc5d106720f
Status: Downloaded newer image for openjdk:8
 ---> 5f4603da3fbc
Step 2/4 : ADD target/spring-boot-docker.jar spring-boot-docker.jar
 ---> 3c8f17d9a553
Step 3/4 : EXPOSE 8090
 ---> Running in f2ac5b50dce6
Removing intermediate container f2ac5b50dce6
 ---> c797a30a9883
Step 4/4 : ENTRYPOINT ["java", "-jar", "spring-boot-docker.jar"]
 ---> Running in 2add22058b5d
Removing intermediate container 2add22058b5d
 ---> 2c87b7f7f764
Successfully built 2c87b7f7f764
Successfully tagged spring-boot-docker:latest

We can observe the above logs containing 4 different steps (which we defined in Dockerfile) while executing docker build command.

Step 2:  After successful completion of step 1 we can see the docker image by hitting the docker images command.

Spring Boot Docker Example 2-min

We can see our latest spring-boot-docker image in the list.

Step 3: Run the docker image on 8090 port using the below command.

Terminal: docker run -p 8090:8090 spring-boot-docker
E:\work\Spring-boot-docker>docker run -p 8090:8090 spring-boot-docker

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.2.RELEASE)

2019-02-03 05:39:53.679  INFO 1 --- [           main] c.o.S.SpringBootDockerApplication        : Starting SpringBootDockerApplication v0.0.1-SNAPSHOT on d26c4095ca5a with PID 1 (/spr
ing-boot-docker.jar started by root in /)
2019-02-03 05:39:53.696  INFO 1 --- [           main] c.o.S.SpringBootDockerApplication        : No active profile set, falling back to default profiles: default
2019-02-03 05:39:57.202  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8090 (http)
2019-02-03 05:39:57.296  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-02-03 05:39:57.308  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.14]
2019-02-03 05:39:57.338  INFO 1 --- [           main] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in 
2019-02-03 05:39:57.565  INFO 1 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-02-03 05:39:57.566  INFO 1 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 3741 ms
2019-02-03 05:39:58.271  INFO 1 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-02-03 05:39:58.878  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8090 (http) with context path ''
2019-02-03 05:39:58.886  INFO 1 --- [           main] c.o.S.SpringBootDockerApplication        : Started SpringBootDockerApplication in 6.606 seconds (JVM running for 7.754)

Access the application:

Spring Boot Docker Example 3

References:

Install Docker on windows 10

Happy Learning 🙂

Download Example

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment