Spring Boot apps are deployed through Docker Publishing

Keywords: Docker Spring Boot

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

Manual Deployment

1. idea creates a spring boot project

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>

You must add the spring-boot-maven-plugin plug-in, which imports dependent packages when you type a Jar package. When you run the mvn package for packaging, it is packaged into a JAR file that can run directly, and you can run it 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. Project as Jar Package

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

$ mvn package

From the output log, Jar runs the Jar package directly in the target directory

$ java -jar DockerDemo-1.0-SNAPSHOT.jar

Then enter it in the browser http://localhost:8080/hello Testing

3. Building docker image

Create Dockerfile file

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

Explanation of parameters:

  • FROM: Represents docker mirroring based on JDK8
  • VOLUME: Represents the creation of a mount point with a container directory of / tmp, which is automatically generated by the host directory. The creation / TMP is 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 DockerDemo.jar
  • RUN:RUN is followed by the bash command, -c means to execute the following string as a command, that is, to execute touch/DockerDemo.jar, which modifies 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 on the command line. To reduce Tomcat startup time, add the system attribute of java.security.egd to point to/dev/urandom as ENTRYPOINT

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

$ docker image build -t DockerDemo:1 .

Explanation of parameters:

  • build: means make a mirror
  • -t: means label the image, equivalent to the docker tag mirror ID new mirror name: version number
  • .: Indicates the location of the Dockerfile,.Represents in the current directory

4. View and run the image

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

Explanation of parameters:

  • docker container run: Represents a running container
  • - name: alias the container. You can use aliases instead of container ID s when operating the container to facilitate container management
  • -d: indicates that the container is open and running in the background
  • -p: Port mapping. Map 8080 ports inside containers to 80 ports of hosts

Plug-in Deployment

Plug-in Deployment To add a dockerfile-maven-plugin plug-in 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>

        <!-- Mirror prefix, required for pushing mirrors to remote libraries, where a private library from Ali Cloud is configured -->
        <docker.image.prefix>
            registry.cn-huhehaote.aliyuncs.com/monkeybrain
        </docker.image.prefix>
        <!-- docker mirrored 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 Environmental Science -->
        <!--<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>
            <!-- To configure spring boot maven Plug-ins that package projects into runnable jar package -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <executable>true</executable>
                </configuration>
            </plugin>

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

            <!-- To configure docker maven Plugin, Binding install Lifecycle, running maven install Generated on docker image -->
            <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>
                    <!-- Modify here docker node ip,Need to open docker Node's remote management port 2375,
                    How to configure it to refer to the previous docker Installation and configuration articles -->
                    <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 Label-->
                    <imageTags>
                        <imageTag>latest</imageTag>
                    </imageTags>
                    <!--base image-->
                    <baseImage>java:8</baseImage>
                    <!-- There entryPoint Defines the run command at container startup, which runs at container startup 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 a remote library when generating a mirror, pushImage Set to true -->
                    <!--<pushImage>false</pushImage>-->
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>

Run Push Command

$ mvn clean package docker:build -DpushImage

Posted by lyonsperf on Wed, 29 Sep 2021 09:16:14 -0700