0 environment

Linux: Vargrant installs Linux

Docker: Install Docker on Linux

1 Preparations

  • Start with a new SpringBoot project and add the following dependencies to pom.xml:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>
<! - the web module -- -- >
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.3.3. RELEASE</version>
</dependency>
Copy the code
  • Add the following to the tags in pom.xml:
<properties>
    <java.version>1.8</java.version>
    <! Docker image name prefix -->
    <docker.image.prefix>springboot</docker.image.prefix>
</properties>
Copy the code
  • Add the following plug-in to the pom.xml tag :(build tag complete code)
<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>
                <! -- Mirror name -->
                <imageName>${docker.image.prefix}/${project.artifactId}</imageName>
                <dockerDirectory>src/main/docker</dockerDirectory>
                <resources>
                    <resource>
                        <targetPath>/</targetPath>
                        <! --jar package directory, default target-->
                        <directory>${project.build.directory}</directory>
                        <! --jar package name, default is $project.artifactid}-${project.version}-->
                        <include>${project.build.finalName}.jar</include>
                    </resource>
                </resources>
            </configuration>
        </plugin>
    </plugins>
</build>
Copy the code
  • (Optional) Changing the port Number

Add the following to application.properties:

server.port=8081
Copy the code
  • Add a controller for test use
package com.ifknow.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/ * * *@author: ifknow <br>
 * @date: 2020/9/8  10:32 <br>
 * @description: * / test
@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String hello(a) {
        return "Hello Docker!"; }}Copy the code
  • Create a new docker folder in SRC /main and create a Dockerfile file in the docker folder (important!).

Dockerfile contains the following contents:

# Use JDK 8 as the base environment # Create a temporary file in the /var/lib/docker directory and link it to the/TMP directory of the Tomcat container Jar # ADD spring-boot-docker-1.0.jar # ADD spring-boot-docker-1.0.jar # ADD spring-boot-docker-1.0.jar # ADD spring-boot-docker-1.0.jar # ADD spring-boot-docker-1.0.jar # To shorten Tomcat startup time, Egd system attribute pointing to /dev/urandom as ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]Copy the code

  • upload

Upload the SpringBoot project to a directory on Linux. SecureCRT or Xftp can be used.

  • other

Maven and Docker must be installed in the Linux environment.

2 Deploy the Springboot JAR project

  • Go to the project directory:
cd spring-boot-docker/
Copy the code
  • Perform the packing operation:
mvn clean package
Copy the code
  • Run jar package tests
Java - jar target/spring - the boot - docker - 1.0. The jarCopy the code
  • After successful startup, build the Dockerfile image
mvn package docker:build
Copy the code

. If the following figure is displayed, the image is successfully built.

  • Look at mirror
# list all docker imagesCopy the code

  • Run the mirror
Docker run -p 8081:8081 -t springboot/spring-boot-docker # -p 8081:8081 Mapping localhost port 8081 to container port 8081 (host port before colon) # -t springboot/spring-boot-dockerCopy the code
  • View a mirror in progress
Docker ps # docker ps -a # View all images including running and finishedCopy the code

  • Visit http://ip:8081/hello in your browser and the page returns Hello Docker!

3 Deploy the WAR package project

Creating a war package project is quite complicated. Here is a solution: Make changes to the Jar package project above!

Modified pom. XML

  • Upload the war package of the project to the specified directory of the server!
  • Install and start the Tomcat image
IO /tomcat # start tomcat # -p port mapping Docker run -d -p 8088:8080 docker. IO /tomcat Map host port 8088 to docker container portCopy the code
  • test

Open your browser and visit http://ip:8088.

** Note: ** If 404 occurs, the possible cause of the port is not released. But I am not for this reason. After checking the Tomcat container directory in Docker and the official directory of Tomcat, we can see that webapp.dist in the container is the correct WebApp directory.

Solutions:

(Photo: blog.csdn.net/qq_40891009…)

(Photo: blog.csdn.net/qq_40891009…)

  • War file deployment

Copy the war package we passed through to the Tomcat container’s Webapps

To view the currently running image:

docker ps
Copy the code

Copy operation:

Docker cp/root/workspace/spring - the boot - docker - 0.0.1 - the SNAPSHOT. 8 f63b712c7f war: / usr/local/tomcat/webapps # # cp is the meaning of copies / root/workspace/spring - the boot - docker - 0.0.1 - the SNAPSHOT. The meaning of war is a war file path # 8 f63b712c7f: / usr/local/tomcat/webapps. 8f63b712C7f is the container_id of the Tomcat containerCopy the code

The Tomcat container automatically decompresses the WAR package.

  • Test access

Visit http://ip:8088/spring-boot-docker-0.0.1-SNAPSHOT/hello in your browser