Detailed usage of docker-maven-plugin

Detailed usage of docker-maven-plugin

Docker-Maven-Plugin

There are two ways to deploy microservices:

  • Manual deployment: First, generate a jar package (or war package) based on the source code packaging, write a Dockerfile file, create a new image based on the base image, upload the jar package (or war package) to the virtual machine and copy it to the JDK container. (Too much trouble)
  • Automatic deployment via Maven plugin. This is also a method often used in actual enterprise development.

Maven plugin automatic deployment steps

Written at the beginning: This method has loopholes and can be easily remotely inserted into the mining machine image by hackers. Be careful when using it. If 2375 is opened and no IP restriction is imposed, it will be pulled into mining. . . It is recommended to use CA encryption port

1. Modify the docker configuration of the host machine and open port 2375 to allow remote access

Executing the Maven command locally is a remote operation for the host machine. The remote operation of Docker is closed by default. Open port 2375 first.

First, execute the command on the host machine and modify the configuration file (centos 7)

vi /lib/systemd/system/docker.service

Add configuration after ExecStart= ‐H tcp://0.0.0.0:2375 ‐H unix:///var/run/docker.sock

ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock \

unix://var/run/docker.sock : unix socket, local clients will use this to connect to the Docker Daemon

tcp://0.0.0.0:2375 : TCP socket, which means that any remote client is allowed to connect to Docker Daemon through port 2375.

turn out to be

After

If it is centos7, modify the following

ExecStart=/usr/bin/dockerd -H fd:// -H tcp://0.0.0.0:2375

2. Refresh the configuration and restart the service

systemctl daemon-reload //Load Docker daemon thread systemctl restart docker //Restart Docker

3. Configure container firewall

Even if port 2375 is opened, it cannot be accessed from the outside and can only be accessed from within. You need to configure the firewall. If Alibaba Cloud is not configured, be careful of being pulled into mining-_-, this is my school server, you need a VPN to access it, it doesn't matter

This is closed.

Use the command systemctl stop firewalld (turn off the firewall)
systemctl disable firewalld (disable the firewall, it will not start at boot)

Turn off selinux

vi /etc/selinux/config, change SELINUX=enforcing to disabled

insert image description here

After the reboot is complete, check the firewall and selinux status

systemctl status firewalld (check firewall status)
secon (check selinux status) 

insert image description here

Alibaba Cloud recommends configuring security curses

At this time I connected to docker and succeeded

4. Add configuration in pom.xml

Dockerfile will be automatically generated with the following configuration

<build>
        <finalName>testDocker</finalName>
        <plugins>

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

            <!--docker maven plugin-->
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>1.1.0</version>
                <!--Configuration section-->
                <configuration>
                    <!--Modify the content of the imageName node to the private warehouse address and port, plus the image id and TAG, we want to transfer it directly to the private server-->
                    <!--Configure the final generated image name, in docker images, we take the project name: version -->
                    <imageName>172.19.240.208:5000/${project.artifactId}:${project.version}</imageName>
                    <!--Base image, equivalent to from--> in Dockerfile
                    <baseImage>ascdc/jdk8</baseImage>
                    <!--Entry point, project.build.finalName is the filename tag content under the build tag under the project tag, testDocker-->
                    <!--Equivalent to starting the container, java-jar/testDocker.jar will be automatically executed-->
                    <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
                    <!--Whether to push to docker private warehouse-->
                    <pushImage>true</pushImage>
                    <registryUrl>172.19.240.208:5000</registryUrl>

                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <directory>${project.build.directory}</directory>
                            <!--Which file is uploaded to Docker, equivalent to add testDocker.jar in Dockerfile /-->
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                    <dockerHost>http://172.19.240.208:2375</dockerHost>
                </configuration>
            </plugin>
        </plugins>
    </build>

Note that there are three points to push to a private repository, and the premise is that the container is in the started state

<!-- 1. Add a registryUrl node, the content is the private warehouse address and port (I set port 5000 here) -->
<registryUrl>your ip:5000</registryUrl>

<!-- 2. Add pushImage node-->
<pushImage>true</pushImage>

<!-- 3. Modify the content of the imageName node to the private warehouse address and port, add the image id and TAG, and the name in the warehouse is your project name: version-->
<imageName>yourip:5000/${project.artifactId}:${project.version}</imageName>

Run mvn clean and mvn install and put it in the local repository first.

[External link image transfer failed. The source site may have an anti-hotlink mechanism. It is recommended to save the image and upload it directly (img-84l0HMwJ-1579593153706)(/Users/zhangye/Library/Application Support/typora-user-images/image-20200120214402887.jpg)]

Then execute mvn docker:build command to create the image. If you want to upload it, add the -DpushImage parameter

[External link image transfer failed. The source site may have an anti-hotlink mechanism. It is recommended to save the image and upload it directly (img-wdYBT027-1579593153707)(/Users/zhangye/Library/Application Support/typora-user-images/image-20200120214544482.jpg)]

There was an error when running

[ERROR] Failed to execute goal com.spotify:docker-maven-plugin:1.1.0:build (default-cli) on project demo:
Exception caught: pull access denied for jdk1.8, repository does not exist or may require 'docker login':
denied: requested access to the resource is denied -> [Help 1]

I checked on Baidu and many people said that I need to log in. In fact, I don’t need to log in, nor does the repository not exist. In fact, there is no jdk1.8 image file on my docker, and I need to download one myself. I can docker search jdk8 and pull one, or upload the tar package myself and then create one with Dockerfile. I pulled one directly

insert image description here

docker images

insert image description here

Change the base image name in the project

insert image description here

Successfully built and pushed

insert image description here

[External link image transfer failed. The source site may have an anti-hotlink mechanism. It is recommended to save the image and upload it directly (img-SAUhrnA3-1579593153710)(/Users/zhangye/Library/Application Support/typora-user-images/image-20200121154702289.jpg)]

insert image description here

View Mirror

insert image description here

View Warehouse

insert image description here

This is the end of this article about the detailed usage of docker-maven-plugin. For more information about the usage of docker-maven-plugin, 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:
  • The docker-maven-plugin plugin cannot pull the corresponding jar package
  • Summary of the dockerfile-maven-plugin usage guide
  • Use of Maven plugin docker-maven-plugin
  • dockerfile-maven-plugin minimalist tutorial (recommended)
  • Teach you how to use docker-maven-plugin to automate deployment

<<:  MySQL database Shell import_table data import

>>:  Solution to define the minimum height of span has no effect

Recommend

Some experience in building the React Native project framework

React Native is a cross-platform mobile applicati...

js development plug-in to achieve tab effect

This article example shares the specific code of ...

JavaScript DOMContentLoaded event case study

DOMContentLoaded Event Literally, it fires after ...

WeChat applet scroll-view realizes left-right linkage effect

WeChat applet uses scroll-view to achieve left-ri...

Solution to overflow of html table

If the table is wide, it may overflow. For exampl...

MySQL DML language operation example

Additional explanation, foreign keys: Do not use ...

JavaScript to dynamically load and delete tables

This article shares the specific code of JavaScri...

Example of how to set up a multi-column equal height layout with CSS

Initially, multiple columns have different conten...

MySQL Installer Community 5.7.16 installation detailed tutorial

This article records the detailed tutorial of MyS...

Vue realizes the percentage bar effect

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

MySQL data migration using MySQLdump command

The advantages of this solution are simplicity an...

Vue Beginner's Guide: Creating the First Vue-cli Scaffolding Program

1. Vue--The first vue-cli program The development...

JS realizes the card dealing animation

This article example shares the specific code of ...

Docker data volume container creation and usage analysis

A data volume container is a container specifical...