Example of how to deploy Spring Boot using Docker

Example of how to deploy Spring Boot using Docker

Here we mainly use spring-boot out of the box, which can generate a standalone program, and the maven plug-in docker-maven-plugin

The main steps here

  • Build a simple springboot project
  • Add docker-maven-plugin and write dockerfile
  • Practice generating docker images

A simple Spring Boot project

Take spring boot 2.0 as an example

Add parament dependency in pom.xml file

 <parent>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-parent</artifactId>
 <version>2.0.0.RELEASE</version>
</parent>

Add web and test dependencies

<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>

Create a Controller with an index() method that returns: Hello Docker!

@RestController
public class Controller {
 
  @RequestMapping("/")
  public String index() {
    return "Hello Docker!";
  }
}

Startup Class

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

After adding, start the project. After successful startup, open the browser: http://localhost:8080/, and the page returns: Hello Docker!, indicating that the Spring Boot project is configured normally.

Add dcoker-maven-plugin

Add the Docker image prefix to the properties node in pom.xml

<properties>
 <docker.image.prefix>springboot</docker.image.prefix>
</properties>

Add the docker build plugin in plugins

<build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
      <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>
              <targetPath>/</targetPath>
              <directory>${project.build.directory}</directory>
              <include>${project.build.finalNmae}.jar</include>
            </resource>
          </resources>
        </configuration>
      </plugin>
    </plugins>
  </build>

Writing a Dockerfile

Create a Dockerfile file in the directory src/main/docker. The Dockerfile file is used to explain how to build the image.

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD spring-boot-docker-1.0.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
EXPOSE 8080

This Dockerfile is very simple. It builds the JDK basic environment and adds the Spring Boot Jar to the image. Here is a brief description:

  • FROM means using the Jdk8 environment as the base image. If the image is not local, it will be downloaded from DockerHub.
  • VOLUME, VOLUME points to a /tmp directory. Since Spring Boot uses the built-in Tomcat container, Tomcat uses /tmp as the working directory by default. The effect of this command is: create a temporary file in the host's /var/lib/docker directory and link it to the /tmp directory in the container
  • ADD, copy the file and rename it
  • ENTRYPOINT, to shorten Tomcat startup time, add the java.security.egd system property to point to /dev/urandom as ENTRYPOINT
  • EXPOSE indicates that port 8080 provides services

Generate docekr image

A Docker environment is required to package the Spring Boot project.

Three dependencies are required

  • jdk environment
  • Maven
  • Docker environment

If there is no error when running java -version,mvn -version,docker version , the environment is ready.

Enter the project directory

mvn package -Dmavne.test.skip=true
java -jar target/spring-boot-docker-1.0.jar

If it can run normally, it means there is no problem with the jar package.

Then build the image

mvn docker:build

If build success is displayed, it is successful.

Use docker images to view the built image

Run the image

docker run -p 8080:8080 -t springboot/spring-boot-docker

Then curl http://127.0.0.1:8080 and you can see Hello Docker! is returned, indicating success

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Notes on deploying SpringBoot projects with IDEA and Dockerfile
  • How to deploy SpringBoot project using Dockerfile
  • Complete steps for Spring Boot to quickly deploy projects using Docker
  • Dockerfile usage in springboot

<<:  How to build a drag and drop plugin using vue custom directives

>>:  Briefly describe the difference between Redis and MySQL

Recommend

HTML cellpadding and cellspacing attributes explained in pictures

Cell -- the content of the table Cell margin (tabl...

Detailed explanation of the basic implementation principle of MySQL DISTINCT

Preface DISTINCT is actually very similar to the ...

Example code for implementing the wavy water ball effect using CSS

Today I learned a new CSS special effect, the wav...

Detailed explanation of the difference between alt and title

These two attributes are often used, but their di...

Vue uses custom instructions to add watermarks to the bottom of the page

Project Scenario Add a custom watermark to the en...

Example of using CSS filter to write mouse over effect

Use CSS filter to write mouse over effect <div...

Vue defines private filters and basic usage

The methods and concepts of private filters and g...

Detailed use cases of vue3 teleport

Official Website https://cli.vuejs.org/en/guide/ ...

MYSQL database GTID realizes master-slave replication (super convenient)

1. Add Maria source vi /etc/yum.repos.d/MariaDB.r...

Detailed explanation of the execution process of mysql update statement

There was an article about the execution process ...

Detailed explanation of several solutions for JavaScript interruption requests

Table of contents 1 Promise Interrupt the call ch...

A brief discussion of 3 new features worth noting in TypeScript 3.7

Table of contents Preface Optional Chaining Nulli...

Basic usage of UNION and UNION ALL in MySQL

In the database, both UNION and UNION ALL keyword...

Explore VMware ESXI CLI common commands

Table of contents 【Common commands】 [Summary of c...