Details running the Spring Boot application in the Docker container

  • 2020-06-15 10:33:33
  • OfStack

spring Boot simplifies the development process of Spring applications and provides all kinds of framework configurations (ES4en-ES5en-ES6en-ES7en) out of the box, following the principle of convention priority configuration. On the other hand, Spring Boot has the ability to build code directly into an executable jar package, which is a deployment unit that can run independently. Based on the above features, it is now widely believed that Spring Boot provides an ability to rapidly construct micro-services (ES14en-ES15en).

Docker and Spring Boot

Docker is an implementation of an Linux container, a lightweight process-based resource isolation technology. Each container corresponds to one process in the operating system, but it has its own network space, file system, PID, and so on. In addition to implementing Linux containers, Docker also makes containers "social", allowing users to publish container images on Docker Hub to share and collaborate with other developers. Please refer to the official documentation for the tutorial on installing Docker

Spring Boot applications are typically built as a single executable jar package, via ES37en-ES38en... Run, but the framework itself does not provide a way to run in the background as a service, usually with the help of process management tools such as Systemd, Supervisord, etc. On the other hand, although the application runtime environment is very simple, containerizing them as Docker container images and running them is very beneficial for automated deployment and operation.

This article will illustrate how to containerize Web, the simplest application developed by Spring Boot, as an example. The focus of this article is to build the Docker image and run the Docker container

Build and implement Spring Boot application

After the application code is written, it can be packaged and run directly:

Maven: mvn package & & java -jar target/spring-boot-docker-0.1.0.jar Gradle: gradle build & & java -jar build/libs/gs-spring-boot-docker-0.1.0.jar

Enter [http://localhost:8080/](http://localhost:8080/) in the browser address bar to access the application

Dockerfile builds the image

Docker provides files in Dockerfile format to build application images, now start to create 1 Spring Boot application Dockerfile:


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

java:8 refers to the official java image provided on Docker Hub. The version number is 8, which means jdk1.8. With this base image, Dockerfile can directly obtain its status via the FROM instruction -- that is, java is installed in the container.

VOLUME /tmp creates the /tmp directory and persists it to the Docker data folder because the embedded Tomcat container used by Spring Boot USES /tmp as the working directory by default ADD ES102en-ES104en-0.1.0.ES105en app.jar copies the application jar package to/app.jar ENTRYPOINT represents the command executed by default after the container runs

Dockerfile is very simple. After editing, run docker build image command:


docker build -t tmy/spring-boot-app .

Then run the Docker container:


docker run -d -p 8080:8080 --name sample-app tmy/spring-boot-app

Where -ES122en stands for background running container, which naturally solves the problem that Spring Boot does not support background running applications. -ES125en 8080:8080 means to map port 8080 inside the container to port 8080 on the host machine so that applications can be accessed directly from the host machine. Give the container an easy to remember name for later management.

Create an Docker image using Maven/Gradle

To make it easier for Java developers to create Docker images, Maven/Gradle both provide responsive plug-ins.

Maven


<properties>
   <docker.image.prefix>springio</docker.image.prefix>
</properties>
<build>
  <plugins>
    <plugin>
      <groupId>com.spotify</groupId>
      <artifactId>docker-maven-plugin</artifactId>
      <version>0.2.3</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.finalName}.jar</include>
          </resource>
        </resources>
      </configuration>
    </plugin>
  </plugins>
</build>

pom. xml above includes the configuration of ES148en-ES149en-ES150en:

imageName specifies the name of the mirror image dockerDirectory specifies the location of Dockerfile resources refers to the files that need to be placed with Dockerfile from 1 to be used in building the image. jar packages generally need to be included

After the above configuration, run the following command to create 1 image in local Docker:


$ mvn package docker:build

Gradle

Gradle also has plug-ins that support Docker:


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

group = 'springio'

...
apply plugin: 'docker'

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

The above configuration is basically similar to Maven, with the following command to build the image:


$ gradle build buildDocker

Related articles: