An example of how to deploy Spring Boot using Docker

  • 2021-01-18 06:46:08
  • OfStack

spring-boot out of the box, can generate a stand-alone program, and 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

1 simple Spring Boot project

Take spring boot 2.0 as an example

Add parament dependency to pom.xml file


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

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

index(); Hello (); Hello (); Docker ();


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

Start the class


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

http://localhost:8080/, page return: Hello Docker! , indicating that the Spring Boot project configuration is normal.

Add dcoker maven -- plugin

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


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

Add the docker build plug-in to 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>

Write Dockerfile

Create the Dockerfile file in the directory src/main/docker. The Dockerfile file is used to describe 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

The Dockerfile file is simple. Build the Jdk base environment, add Spring Boot Jar to the image.

FROM: using Jdk8 environment as the base image. If the image is not local, it will be downloaded from DockerHub VOLUME, VOLUME refers to a directory /tmp. Since Spring uses the built-in Tomcat container, Tomcat uses /tmp as the working directory by default. The effect of this command is to create a temporary file in /var/lib/docker on the host and link it to /tmp in the container ADD, copy the file and rename it ENTRYPOINT. In order to shorten Tomcat startup time, add java.security.egd system attribute pointing to /dev/urandom as ENTRYPOINT EXPOSE specifies port 8080 for service

Generate an docekr image

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

Three dependencies are required

jdk environment maven docker environment

run java -version,mvn -version,docker version These commands report no errors if the environment is ready.

Go to Project Directory


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

The jar package is fine if it works properly.

And then build the image


mvn docker:build

build: success succeeded.

Use docker images to view the built image

Run the mirror


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

Then curl http://127.0.0.1:8080 you can see return Hello Docker! , indicating success.


Related articles: