Quick Contact

    Hibernate

     

    Dockerizing Spring Boot Application | Spring Boot Docker Tutorial

    Here we will learn step by step how to dockerize the Spring boot application. We will create a docker image for the Spring boot application and deploy it in a Docker container.

    Development Steps

    1. Create Spring boot application
    2. Build a simple REST API
    3. Create Dockerfile
    4. Build Docker Image
    5. Run Docker Image in a Container
    1. Create Spring boot application

      Spring Boot provides a web tool called Spring Initializer to bootstrap an application quickly. Just go to https://start.spring.io/ and generate a new spring boot project.

      Use the below details in the Spring boot creation:

      Project Name:

      springboot-docker-demo

      Project Type:

      Maven

      Choose dependencies:

      Spring Web

      Package name:

      net.ducat.springboot

      Packaging:

      Jar

      Here is the pom.xml file for your reference:

      < ?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 https://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.5.4< /version>
      < relativePath/>< !-- lookup parent from repository -->
      < /parent>
      < groupId>net.javaguides< /groupId>
      < artifactId>springboot-docker-demo< /artifactId>
      < version>0.0.1-SNAPSHOT< /version>
      < name>springboot-docker-demo< /name>
      < description>Demo project for Spring Boot and Docker< /description>
      < properties>
      < java.version>11< /java.version>
      < /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>org.springframework.boot< /groupId>
      < artifactId>spring-boot-maven-plugin< /artifactId>
      < /plugin>
      < /plugins>
      < /build>
      < /project>			
      
    2. Build a simple REST API

      Let’s create a simple REST API to test our Spring boot application deployment in a docker container.

      Let’s open SpringbootDockerDemoApplication and add the following content to it:

      packagenet.Ducat.springboot;
      importorg.springframework.boot.SpringApplication;
      importorg.springframework.boot.autoconfigure.SpringBootApplication;
      importorg.springframework.web.bind.annotation.GetMapping;
      importorg.springframework.web.bind.annotation.RestController;
      
      @SpringBootApplication
      @RestController
      publicclassSpringbootDockerDemoApplication{
      
      	@GetMapping("/welcome")
      	publicStringwelcome(){
      		return"Spring Boot Docker Demo";
      	}
      
      	publicstaticvoidmain(String[] args) {
      		SpringApplication.run(SpringbootDockerDemoApplication.class, args);
      	}
      }
      
      Use the following command to maven build this project:
      mvnpackage
      
    3. Create Dockerfile

      Next step, go to the project root directory and create a file named Dockerfile and the following content to it:

      # define base docker image
      FROM openjdk:11
      LABEL maintainer="ducat.net"
      ADD target/springboot-docker-demo-0.0.1-SNAPSHOT.jar springboot-docker-demo.jar
      ENTRYPOINT ["java", "-jar", "springboot-docker-demo.jar"]
      

      FROM:

      A docker image can use another image available in the docker registry as its base or parent image. In the above example, we use the openjdk:11 image as our base image.

      LABEL:

      The LABEL instruction is used to add metadata to the image. In the above Dockerfile, we have added some info about the maintainer of the image through LABEL instruction.

      ADD:

      The ADD instruction is used to copy new files and directories to the docker image.

      ENTRYPOINT:

      This is where you configure how the application is executed inside the container.

    4. Build Docker Image

      Now that we have defined the Dockerfile, let’s build a docker image for our application.

      Use the below command to build the docker image:

      dockerbuild-tspringboot-docker-demo:latest .

      The file path . defines the location of the Dockerfile in the current directory, and the -t argument tags the resulting image, where the repository name is the springboot-docker-demo and the tag is the latest.

      After the build is successfully finished, we can check to see if it appears in the list of docker images available locally. To do so, we can execute the below command.

      docker images

    5. Run Docker Image in a Container

      Once we build the docker image, next we will run our docker image in a docker container.

      Let’s use the following command to run the docker image:

      docker run -p 8081:8080springboot-docker-demo

      The -p argument establishes a port mapping from 8080 to 8081. The springboot-docker-demo is a docker image name.

      Once docker image running in a container, you will see below output:

      arunpc@Arunpc-HPspringboot-docker-demo%dockerrun-p8081:8080springboot-docker-demo

    	._________
    /\\/___'_____(_)______\\\\
    (()\___|'_ | '_||'_ \/ _` | \ \ \ \
     \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
      '|____|.__|_||_|_||_\__,|////
    =========|_|==============|___/=/_/_/_/
    ::Spring Boot ::(v2.5.4)
    
    2021-09-03 10:10:38.293  INFO1--- [           main] n.j.s.SpringbootDockerDemoApplication    :StartingSpringbootDockerDemoApplicationv0.0.1-SNAPSHOTusingJava11.0.12on520f1f1ab260withPID1(/springboot-docker-demo.jarstartedbyrootin/)
    2021-09-03 10:10:38.311  INFO1--- [           main] n.j.s.SpringbootDockerDemoApplication    :Noactiveprofileset,falling back to default profiles:default
    2021-09-03 10:10:42.814  INFO1--- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  :Tomcatinitializedwithport(s):8080(http)
    2021-09-03 10:10:42.868  INFO1--- [           main] o.apache.catalina.core.StandardService   :Startingservice [Tomcat]
    2021-09-03 10:10:42.870  INFO1--- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [ApacheTomcat/9.0.52]
    2021-09-03 10:10:43.116  INFO1--- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]:InitializingSpringembeddedWebApplicationContext
    2021-09-03 10:10:43.117  INFO1--- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext:initializationcompletedin4443 ms
    2021-09-03 10:10:44.909  INFO1--- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  :Tomcatstartedonport(s):8080(http)withcontextpath''
    2021-09-03 10:10:45.002  INFO1--- [           main] n.j.s.SpringbootDockerDemoApplication    :StartedSpringbootDockerDemoApplicationin8.99seconds(JVMrunningfor10.882)
    2021-09-03 10:11:18.099  INFO1--- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]:InitializingSpringDispatcherServlet'dispatcherServlet'
    2021-09-03 10:11:18.104  INFO1--- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        :InitializingServlet'dispatcherServlet'
    2021-09-03 10:11:18.127  INFO1--- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        :Completedinitializationin23ms
    

    Apply now for Advanced Java Training Course

    Copyright 1999- Ducat Creative, All rights reserved.

    Anda bisa mendapatkan server slot online resmi dan terpercaya tentu saja di sini. Sebagai salah satu provider yang menyediakan banyak pilihan permainan.