Use the Docker plugin to build an image in IDEA and push it to the private server Harbor

Part of the content reference:
CSDN:

1. Enable remote access to the Docker server

1.1 Enable 2375 remote access

The default dokcer does not support remote access, you need to add some configuration to enable Docker remote access

# 首先查看docker配置文件所在位置
systemctl status docker

# 会输出如下内容:
● docker.service - Docker Application Container Engine
   Loaded: loaded (/etc/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2020-12-17 14:22:23 CST; 18min ago
     Docs: http://docs.docker.com
 Main PID: 25113 (dockerd)

Determine the location of the docker configuration file: /etc/systemd/system/docker.service

Then edit and modify the docker configuration file:

vi /lib/systemd/system/docker.service

Find the line containing ExecStart and add the following:

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

Among them, port 2375 is the docker remote service port, which includes portainer, a visualization tool for docker, and the function of remotely uploading images.

1.2 Add harbor mirror configuration

Edit the docker configuration file:

vi /etc/docker/daemon.json
# 添加harbor镜像地址
{
    
    
 "insecure-registries": ["192.168.0.20:81"]
}

1.3 Restart the docker service

# 后台配置重新加载
systemctl daemon-reload 
# 重启docker服务
systemctl restart docker.service
# 此处可能会出现docker无法启动情况,可能是由于docker.service配置文件修改错误,重新修改一次然后重新执行上述命令即可

#查看配置的端口号(2375)是否开启(非必要)
netstat -nlpt

Two, operate Docker through IDEA

2.1 Download the docker plugin

Use idea's docker plug-in to connect to docker, idea has already downloaded the docker plug-in by default, if not, you need to download the docker plug-in in idea
Insert picture description here

2.2 Configure remote docker

Click the setting option of idea (file --> setting -> docker) to create a new connection. After the
Insert picture description here
Insert picture description here
connection is successful, you can use docker on the server (virtual machine)
Insert picture description here

2.3 Pull image

Ideas can pull images in a visual way, without having to type commands by themselves.
Insert picture description here
Insert picture description here
Insert picture description here
Sometimes the pull time will time out. You can configure a domestic image to get Alibaba Cloud accelerator
Insert picture description here

2.4 Create a container and run

Create and run the docker container After the
Insert picture description here
Insert picture description here
creation is successful, you can see the newly created container, or you can use the docker command on the server (virtual machine) to view
Insert picture description here

Operations such as restarting, stopping, and deleting containers
Insert picture description here

Three, IDEA-Maven packaging image

Short book: Yin Kaikai : Push mirror of maven plug-in docker-maven-plugin to harbor private warehouse (2)
CSDN: Drunk Cuckoo Bird: docker-private server build and push into Springboot project as mirror

Upload the built image to the harbor private server through the docker-maven-plugin plugin

3.1 Modify the maven configuration file settings.xml

In the maven configuration file, add the username and password of the harbor private server:

  <servers>
    <server>
    <id>dockerharbor</id>
    <username>harbor</username>
    <password>123456</password>
    <configuration>
      <email>[email protected]</email>
    </configuration>
  </server>
  </servers>

3.2 Modify pom.xml in SpringBoot project

Add property configuration, property configuration, refer to this in the following plug-in configuration:

  • docker.repostory is the docker private server address. The default port after harbor configuration is 80, without the port number. But I changed it to 81
  • docker.registry.name is the name of the mirror warehouse configured in the harbor and must be consistent! Here I configure test, because the mirror warehouse name configured in the harbor is also test.

<properties>
	<!--docker插件-->
	<!-- docker私服地址,Harbor配置完默认地址就是80,默认不带端口号。但是我这里是81 -->
	<docker.repostory>192.168.10.11:81</docker.repostory>
	<!--项目名,需要和Harbor中的项目名称保持一致 -->
	<docker.registry.name>test</docker.registry.name>
</properties>

3.3 docker-maven-plugin plugin configuration

  • serverId specifies the server node previously configured in maven's settings.xml, so that maven will find the username, password and email address configured in it
  • registryUrl specifies the properties configured above, which is the access url of the harbor private server. Note that I set to use port 81, and the default is port 80
  • imageName specifies the name of the image uploaded to the harbor private server, which must be consistent with the url and image warehouse name on the harbor. The docker.registry.name is the properties configured above

method one:

<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>
                <executions>
                    <execution>
                        <id>build-image</id>
                        <!--用户只需执行mvn package ,就会自动执行mvn docker:build-->
                        <phase>package</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <serverId>harbor</serverId>
                    <registryUrl>http://${docker.repostory}</registryUrl>
                    <!--Building image 192.168.0.20/demo1-->
                    <imageName>${docker.repostory}/${docker.registry.name}/${project.artifactId}:${project.version}
                    </imageName>
                    <!--指定标签 这里指定的是镜像的版本,我们默认版本是latest-->
                    <!--<imageTags>
                        <imageTag>latest</imageTag>
                    </imageTags>-->
                    <!--指定基础镜像jdk1.8-->
                    <baseImage>jdk:1.8</baseImage>
                    <!-- 指定 Dockerfile 路径-->
                    <!--镜像制作人本人信息 -->
                    <!--<maintainer>[email protected]</maintainer>-->
                    <!--切换到工作目录-->
                    <workdir>/opt</workdir>
                    <!--${project.build.finalName}.jar是打包后生成的jar包的名字-->
                    <entryPoint>["java", "-jar","-Xms256m","-Xmx512m","/${project.build.finalName}.jar"]
                    </entryPoint>
                    <!--必须配置dockerHost标签(除非配置系统环境变量DOCKER_HOST)-->
                    <dockerHost>http://192.168.0.20:2375</dockerHost>
                    <!-- jar包位置-->
                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <!-- target目录下-->
                            <directory>${project.build.directory}</directory>
                            <!--通过jar包名找到jar包-->
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                </configuration>
            </plugin>
        </plugins>
    </build>

Next, just click clean to clear all the previous packaged files, and then click the package package file to complete the image construction. The real one-click deployment. After the
Insert picture description here
Insert picture description here
image is successfully built, just create the container and run it to
Insert picture description here
Insert picture description here
access through ip

In this way, you can make the image directly through the Maven package command, but if you want to push the image to the harbor private server, you need to execute docker:push, that is:

Click push to push the image to the harbor private server

Way two:

<plugin>
  <groupId>com.spotify</groupId>
  <artifactId>docker-maven-plugin</artifactId>
  <version>1.0.0</version>
  <configuration>
	  <serverId>my-hub</serverId>
	  <registryUrl>http://${docker.repostory}</registryUrl>
	  <!--必须配置dockerHost标签(除非配置系统环境变量DOCKER_HOST)-->
	  <dockerHost>http://192.168.10.11:2375</dockerHost>
	  <!--Building image 192.168.10.11/demo1-->
	  <imageName>${docker.repostory}/${docker.registry.name}/${project.artifactId}:${project.version}</imageName>
	  <!-- 指定 Dockerfile 路径-->
	  <dockerDirectory>${basedir}/</dockerDirectory>
	  <!-- jar包位置-->
	  <resources>
		  <resource>
		  <targetPath>/ROOT</targetPath>
		  <!-- target目录下-->
		  <directory>${project.build.directory}</directory>
		  <!--通过jar包名找到jar包-->
		  <include>${pack-name}</include>
		  </resource>
	  </resources>
  </configuration>
</plugin>

Then the jar package name in the Dockerfile file needs to be modified accordingly:

FROM java:8
WORKDIR /ROOT
ADD /ROOT/demo1-2.jar /ROOT/
ENTRYPOINT ["java", "-jar", "demo1-2.jar"]

Click pakage to package, the jar package of the springboot project is generated on the target

After finishing, click docker bulid to build the project image

Then click push to push the image to the harbor private server

3.4 docker-maven-plugin operation container

Reference for this part:

  • Nuggets: MacroZheng: https://juejin.im/post/6868060821927723021

  • docker-maven-pluginNot only can operate the image, but also the container. For example, we previously needed to use the following Docker command to run the container;

docker run -p 8080:8080 --name mall-tiny-fabric \
--link mysql:db \
-v /etc/localtime:/etc/localtime \
-v /mydata/app/mall-tiny-fabric/logs:/var/logs \
-d 192.168.3.101:5000/mall-tiny/mall-tiny-fabric:0.0.1-SNAPSHOT
  • Now we only need to configure it in the plug-in, <image>adding a <run>node under the node can define the behavior of the container startup:
<!--定义容器启动行为-->
<run>
    <!--设置容器名,可采用通配符-->
    <containerNamePattern>${project.artifactId}</containerNamePattern>
    <!--设置端口映射-->
    <ports>
        <port>8080:8080</port>
    </ports>
    <!--设置容器间连接-->
    <links>
        <link>mysql:db</link>
    </links>
    <!--设置容器和宿主机目录挂载-->
    <volumes>
        <bind>
            <volume>/etc/localtime:/etc/localtime</volume>
            <volume>/mydata/app/${project.artifactId}/logs:/var/logs</volume>
        </bind>
    </volumes>
</run>
  • Then docker:startyou can start it directly by using the command;
mvn docker:start

[root@linux-local mydata]# docker ps
CONTAINER ID        IMAGE                                                         COMMAND                  CREATED             STATUS              PORTS                                            NAMES
95ce77c0394b        192.168.3.101:5000/mall-tiny/mall-tiny-fabric:0.0.1-SNAPSHOT   "java -jar /mall-tin…"   32 seconds ago      Up 31 seconds       0.0.0.0:8080->8080/tcp                           mall-tiny-fabric
  • Stop the container using the docker:stopcommand;
mvn docker:stop
  • docker:removeIsn't it very convenient to delete container usage commands?
mvn docker:remove

Guess you like

Origin blog.csdn.net/An1090239782/article/details/111316025