IDEA integrates docker plug-in to implement one-key deployment of SpringBook project

Keywords: Docker Maven Java firewall

Virtual machine CentOS 7.X, docker 1.3.X, Win10 Idea 2018.1
The Docker plug-in has been downloaded by default Idea
The default virtual machine docker has jdk installed

Open docker configuration file, open port 2375

[root@xuan ~]# vim /usr/lib/systemd/system/docker.service

Add ExecStart=/usr/bin/dockerd-current

-H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock

Reload configuration file

systemctl daemon-reload

Restart docker

systemctl start docker

View process

netstat -tulp

PS: Centos7 does not have netstat, so download it first.

yum install net-tools

Firewall Open 2375 Port Number

[root@xuan ~]# firewall-cmd --zone=public --add-port=2375/tcp --permanent

service iptables restart

[root@xuan ~]# firewall-cmd --reload

Idea Connect Virtual Machine docker

Open the small wrench to find the docker and enter the virtual machine ip

The Springboot project adds docker-maven-plugin plug-ins.

<!--Use docker-maven-plugin Plug-in unit-->
<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>1.0.0</version>
    <!--Binding a plug-in to a phase implement-->
    <executions>
        <execution>
            <id>build-image</id>
            <!--Binding plug-ins to package this phase Up. That is to say, the user only needs to execute mvn package ,It will execute automatically. mvn docker:build-->
            <phase>package</phase>
            <goals>
                <goal>build</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <!--Specify the generated mirror name-->
        <imageName>hsz1992/${project.artifactId}</imageName>
        <!--Specified tag-->
        <imageTags>
            <imageTag>latest</imageTag>
        </imageTags>
        <!-- Appoint Dockerfile Route-->
        <dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>
        <!--Designated remote docker api address-->
        <dockerHost>http://192.168.159.130:2375</dockerHost>
        <!-- Here's a copy. jar Package to docker Container specified directory configuration -->
        <resources>
            <resource>
                <targetPath>/</targetPath>
                <!--jar The path where the package is located corresponds to what is configured here target Catalog-->
                <directory>${project.build.directory}</directory>
                <!-- Need to include jar Pack, which corresponds to Dockerfile File name added in -->
                <include>${project.build.finalName}.jar</include>
            </resource>
        </resources>
    </configuration>
</plugin>

Complete pom:

<?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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.qwx</groupId>
    <artifactId>docker</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <java.version>1.8</java.version>
        <docker.image.prefix>docker</docker.image.prefix>
    </properties>

    <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>
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>1.0.0</version>

                <!--Binding a plug-in to a phase implement-->
                <executions>
                    <execution>
                        <id>build-image</id>
                        <!--Binding plug-ins to package this phase Up. That is to say, the user only needs to execute mvn package ,It will execute automatically. mvn docker:build-->
                        <phase>package</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                    </execution>
                </executions>

                <configuration>
                    <!--Specify the generated mirror name-->
                    <imageName>hsz1992/${project.artifactId}</imageName>
                    <!--Specified tag-->
                    <imageTags>
                        <imageTag>latest</imageTag>
                    </imageTags>
                    <!-- Appoint Dockerfile Route-->
                    <dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>

                    <!--Designated remote docker api address-->
                    <dockerHost>http://192.168.100.104:2375</dockerHost>

                    <!-- Here's a copy. jar Package to docker Container specified directory configuration -->
                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <!--jar The path where the package is located corresponds to what is configured here target Catalog-->
                            <directory>${project.build.directory}</directory>
                            <!-- Need to include jar Pack, which corresponds to Dockerfile File name added in -->
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Create docker folders and Dockerfile files, docker in src/main


Dockerfile:

FROM java:8
VOLUME /tmp
ADD docker-1.0-SNAPSHOT.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
Deep pits here:
The jar package name after ADD must be the same as the package name. If you see that the mirror name after successful configuration begins with shaxxx, please go back.

FROM: Selected jdk
EXPOSE is the same port number as server.port=8082
ADD: The first jar is the packaged project name and the second is the alias

Find Maven Projects on the right side of idea, Lifecycle, and double-click package Packing
build success means that the package is successful, and focusing on the docker view, images will have an additional image

Create a container, find the image just generated, and click Create a container

Image ID is the image generated by packaging
Container name container name is optional
Bind ports 8081 is the open port of docker and 8080 is the project port.

After setting up, start the container and go to the virtual machine to see if the startup is successful after successful startup.

docker container ls

Access virtual machine ip + port number

http://192.168.100.104:8081/test/docker


END

Posted by abakash on Mon, 07 Oct 2019 02:27:18 -0700