java deploy microservices to Docker

The microservice of this project is developed by spring boot, and each microservice project is made into jar package. Finally, jar is run in the Docker container. The deployment process is as follows

  • 1. The spring boot project is finally built into Jar package.
  • 2. Create Docker image
  • 3. Create container
  • 4. Start the container

Manual deployment of packaged plug-ins in maven

Use IDEA to build spring boot project, copy plug-in dependency below to microservice project

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

The complete spring docker project pom.xml file is as follows

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>docker</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>docker</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Run in the project directory: mvn clear package or execute the clear package command through IDEA. The package is successful, as shown in the following figure:

Copy the jar package of the previous step to the Linux server, and prepare to create the image.
Test whether the jar package can run. Execute: java -jar docker-0.0.1-SNAPSHOT.jar

Write the Dockerfile file in the location of docker-0.0.1-SNAPSHOT.jar

The content of Dockerfile is

FROM java:8
ADD docker-0.0.1-SNAPSHOT.jar /docker-test.jar 
EXPOSE 9000
ENTRYPOINT ["java","-jar","/docker-test.jar"]

Execute in the directory where the Dockerfile file is located

docker build -t docker-test:0.0.1 .

Image created successfully, query image

docker images

Create startup container

docker run -d -p 9000:9000 docker-test:0.0.1

After the container is started, you can query the running container through docker ps.

View project operation log

This completes the manual build of the mirror deployment project

maven uses Dockerfile to build image

maven provides docker maven plugin plug-ins to complete the process from packaging to building images and containers.

1) write POM · docker.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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>docker</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>docker</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

    <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>
                <!--docker Image related configuration information-->
                <configuration>
                    <!--Image name, project name here-->
                    <imageName>${project.artifactId}‐${project.version}</imageName>
                    <!--Dockerfile File directory-->
                    <dockerDirectory>${project.basedir}/src/main/resources</dockerDirectory>
                    <!--TAG,Engineering version number is used here-->
                    <imageTags>
                        <imageTag>${project.version}</imageTag>
                    </imageTags>
                    <imageName>${project.artifactId}:${project.version}</imageName>
                    <!--Configuration information for building mirrors-->
                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <directory>${project.build.directory}</directory>
                            <include>${project.artifactId}‐${project.version}.jar</include>
                        </resource>
                    </resources>
                </configuration>
            </plugin>

        </plugins>
    </build>

</project>

2) copy the Dockerfile file to src/main/resource
3) delete the docker test image created before

docker rmi -f docker-test:0.0.1

4) enter the project root directory (where POM · docker.xml is located) for execution, and a successful image can be created.

mvn ‐f pom_docker.xml clean package ‐DskipTests docker:build

Keywords: Java Docker Maven Spring

Added by godwisam on Tue, 15 Oct 2019 17:17:58 +0300