Analysis of the process of publishing and deploying Spring Boot applications through Docker

Analysis of the process of publishing and deploying Spring Boot applications through Docker

There are two ways to deploy Spring Boot projects into docker, manual deployment and plug-in deployment

Manual deployment

1. Create a spring boot project with idea

pom.xml file

<?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">
    <parent>
        <artifactId>spring-cloud-examples</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>DockerDemo</artifactId>

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

</project>

spring-boot-maven-plugin plug-in must be added. The function of this plug-in is to introduce dependent packages when packaging the Jar package. When running "mvn package" for packaging, it will be packaged into a JAR file that can be run directly. It can be run directly using the "java -jar" command.

Startup Class

package dockerdemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

    @RequestMapping("/hello")
    public String hello(){
        return "Hello Docker World!";
    }
}

2. Package the project into a Jar

Then execute the Maven command in the directory where the project pom.xml file is located to package the project into a Jar package.

$ mvn package

From the output log, we can see that the Jar is in the target directory. Run the Jar package directly.

$ java -jar DockerDemo-1.0-SNAPSHOT.jar

Then enter http://localhost:8080/hello in the browser to test

insert image description here

3. Build docker image

Create a Dockerfile

FROM java:8
VOLUME /tmp
ADD DockerDemo-1.0-SNAPSHOT.jar DockerDemo.jar
RUN bash -c "touch /DockerDemo.jar"
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/DockerDemo.jar"]

Parameter explanation:

  • FROM: Indicates that the docker image is created based on JDK8
  • VOLUME: means creating a mount point, the container directory is /tmp, and the host directory is automatically generated. /tmp is created because the Tomcat container embedded in Spring Boot uses /tmp as the working directory by default.
  • ADD: Copy DockerDemo-1.0-SNAPSHOT.jar outside the container to the container and rename it to DockerDemo.jar
  • RUN: RUN is followed by a bash command, and -c means that the string behind it is executed as a command, that is, touch /DockerDemo.jar is executed. This command changes the access time and modification time of the DockerDemo.jar file to the current time.
  • ENTRYPOINT: The command that runs when the container starts, which is equivalent to entering java -jar xxxx.jar in the command line. In order to shorten the startup time of Tomcat, add the system property of java.security.egd to point to /dev/urandom as ENTRYPOINT

After creating the Dockerfile, put the packaged Spring Boot project jar package and Dockerfile file in any directory, and use the docker command to build the image file:

$ docker image build -t DockerDemo:1 .

Parameter explanation:

  • build: means making a mirror
  • -t: means to label the image, which is equivalent to docker tag image ID New image name: version number
  • .: indicates the location of the Dockerfile file, and . indicates the current directory

4. View and run the image

#View the image:
$ docker images
#Run the image:
$ docker container run --name DockerDemo -d -p 80:8080 DockerDemo:1

Parameter explanation:

  • docker container run: means running the container
  • –name: Give the container an alias. When operating the container, you can use the alias instead of the container ID to facilitate container management
  • -d: indicates that the container will run in the background after it is started
  • -p: port mapping. Map port 8080 inside the container to port 80 on the host

insert image description here

Plugin deployment

To deploy the plugin, add dockerfile-maven-plugin plugin to the project's pom.xml file.

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">
    <parent>
        <artifactId>spring-cloud-docker</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>spring-cloud-eureka</artifactId>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <!-- Image prefix, required when pushing images to remote libraries. Here, an Alibaba Cloud private library is configured. -->
        <docker.image.prefix>
            registry.cn-huhehaote.aliyuncs.com/monkeybrain
        </docker.image.prefix>
        <!-- docker image tag -->
        <docker.tag>latest</docker.tag>

        <!-- Activated profile -->
        <!--<activatedProperties></activatedProperties>-->
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
    </dependencies>

    <profiles>
        <!-- Docker environment -->
        <!--<profile>
            <id>docker</id>

            <properties>
                <activatedProperties>docker</activatedProperties>
                <docker.tag>docker-demo-${project.version}</docker.tag>
            </properties>
        </profile>-->
    </profiles>

    <build>
        <!--Default Maven command-->
        <defaultGoal>install</defaultGoal>
        <finalName>${project.artifactId}</finalName>

        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>


        <plugins>
            <!-- Configure the spring boot maven plugin to package the project into a runnable jar package-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <executable>true</executable>
                </configuration>
            </plugin>

            <!-- Skip unit testing when packaging -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>

            <!-- Configure the docker maven plugin, bind the install lifecycle, and generate a docker image when running maven install -->
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>0.4.13</version>
                <!--<executions>
                    <execution>
                        <phase>install</phase>
                        <goals>
                            <goal>build</goal>
                            <goal>tag</goal>
                        </goals>
                    </execution>
                </executions>-->
                <configuration>
                    <!-- To modify the docker node ip here, you need to open the remote management port 2375 of the docker node.
                    For specific configuration, please refer to the previous article on Docker installation and configuration-->
                    <dockerHost>http://localhost:2375</dockerHost>
                    <imageName>${docker.image.prefix}/${project.build.finalName}</imageName>
                    <serverId>aliyun-docker-registry</serverId>
                    <registryUrl>registry.cn-huhehaote.aliyuncs.com</registryUrl>
                    <pushImage>true</pushImage>
                    <!--Mirror tag-->
                    <imageTags>
                        <imageTag>latest</imageTag>
                    </imageTags>
                    <!--Base Image-->
                    <baseImage>java:8</baseImage>
                    <!-- The entryPoint here defines the command to run when the container starts. When the container starts, it runs java -jar package name-->
                    <entryPoint>
                        ["java","-jar","/${project.build.finalName}.jar"]
                    </entryPoint>
                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <directory>${project.build.directory}</directory>
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>

                    <!--<image>${docker.image.prefix}/${project.build.finalName}</image>
                    <newName>${docker.image.prefix}/${project.build.finalName}:${docker.tag}</newName>
                    <forceTags>true</forceTags>-->
                    <!-- If you need to push to the remote library when generating the image, set pushImage to true -->
                    <!--<pushImage>false</pushImage>-->
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>

Run the push command

$ mvn clean package docker:build -DpushImage

This concludes this article on the process analysis of Spring Boot application release and deployment through Docker. For more relevant Spring Boot application Docker deployment content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Docker compose deploys SpringBoot project to connect to MySQL and the pitfalls encountered
  • Springboot multiple data sources cooperate with docker to deploy mysql master-slave to achieve read-write separation effect
  • Implementation steps for docker deployment of springboot and vue projects
  • How to implement automatic deployment of Springboot service Docker
  • Implementation of Springboot packaging as Docker image and deployment

<<:  Use SWFObject to perfectly solve the browser compatibility problem of inserting Flash into HTML

>>:  Teach you how to implement the observer mode in Javascript

Recommend

Detailed explanation of VUE's data proxy and events

Table of contents Review of Object.defineProperty...

Comparative Analysis of High Availability Solutions of Oracle and MySQL

Regarding the high availability solutions for Ora...

Vue implements weather forecast function

This article shares the specific code of Vue to r...

New usage of watch and watchEffect in Vue 3

Table of contents 1. New usage of watch 1.1. Watc...

Introduction to ufw firewall in Linux

Let's take a look at ufw (Uncomplicated Firew...

Implementation of building Kubernetes cluster with VirtualBox+Ubuntu16

Table of contents About Kubernetes Basic environm...

Linux MySQL root password forgotten solution

When using the MySQL database, if you have not lo...

JavaScript custom calendar effect

This article shares the specific code of JavaScri...

Detailed explanation of how to view MySQL memory usage

Preface This article mainly introduces the releva...

Summary of commonly used commands for docker competition submission

Log in to your account export DOCKER_REGISTRY=reg...

Detailed explanation of HTML basics (Part 2)

1. List The list ul container is loaded with a fo...

html base url tag

Its function is to set a global style. Then your s...

Tutorial on deploying springboot package in linux environment using docker

Because springboot has a built-in tomcat server, ...