1.修改docker配置文件,打开2375端口
[root@s162 docker
]
[root@s162 docker
]
[root@s162 docker
]
[root@s162 docker
]
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
dockerd 27021 root 5u IPv6 352598799 0t0 TCP *:2375
(LISTEN
)
2. Idea安装配置docker插件
2.1. idea-plugins市场 安装docker插件
略…
2.2. 配置docker
3.springboot项目部署到docker服务器
3.1. 编写docker/dockerfile
3.2.maven添加docker-maven-plugin插件
<plugin>
<groupId>com.spotify
</groupId>
<artifactId>docker-maven-plugin
</artifactId>
<version>1.0.0
</version>
<configuration>
<imageName>jhs/${project.artifactId}:${project.version}
</imageName>
<dockerDirectory>${project.basedir}/docker
</dockerDirectory>
<dockerHost>http://192.168.129.162:2375
</dockerHost>
<resources>
<resource>
<targetPath>/
</targetPath>
<directory>${project.build.directory}
</directory>
<include>${project.build.finalName}.jar
</include>
</resource>
</resources>
<buildArgs>
<JAR_FILE>target/${project.build.finalName}.jar
</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
3.3. docker:build
使用命令$ mvn clean package docker:build -Dmaven.test.skip=true构建镜像,在docker服务器上查看镜像是否上传成功:
3.4 docker:tag
docker命令行格式为:#docker tag <imageId or imageName> <nexus-hostname>:<repository-port>/<image>:<tag>
插件配置 <configuration>补充配置:
<configuration>
<image>jhs/${project.artifactId}:${project.version}
</image>
<newName>192.168.129.160:5000/${project.artifactId}:${project.version}
</newName>
</configuration>
为镜像打上tag标签,为后续的push做准备:mvn clean docker:tag -Dmaven.test.skip=true -DskipDockerBuild
3.5 docker:push
插件配置 <configuration>补充配置:
<configuration>
<serverId>nexus-docker-registry
</serverId>
<registryUrl>192.168.129.160:5000
</registryUrl>
<imageName>192.168.129.160:5000/${project.artifactId}
</imageName>
</configuration>
将上文打上tag标签的镜像,推送到私服nexus:mvn clean docker:push -Dmaven.test.skip=true -DskipDockerBuild -DskipDockerTag
3.6 docker插件参数
-DskipDockerBuild to skip image build-DskipDockerTag to skip image tag-DskipDockerPush to skip image push-DskipDockerto skip any Docker goals
3.7 绑定命令到maven phases
<executions>
<execution>
<id>build-image
</id>
<phase>package
</phase>
<goals>
<goal>build
</goal>
</goals>
</execution>
<execution>
<id>tag-image
</id>
<phase>package
</phase>
<goals>
<goal>tag
</goal>
</goals>
<configuration>
<image>jhs/${project.artifactId}:${project.version}
</image>
<newName>192.168.129.160:5000/${project.artifactId}:${project.version}
</newName>
</configuration>
</execution>
<execution>
<id>push-image
</id>
<phase>deploy
</phase>
<goals>
<goal>push
</goal>
</goals>
<configuration>
<serverId>nexus-docker-registry
</serverId>
<registryUrl>192.168.129.160:5000
</registryUrl>
<imageName>192.168.129.160:5000/${project.artifactId}
</imageName>
</configuration>
</execution>
</executions>
3.8 最佳实践
<properties>
<docker.host>http://192.168.129.162:2375
</docker.host>
<docker.registry.url>192.168.129.160:5000
</docker.registry.url>
</properties>
<build>
<plugins>
<plugin>
<groupId>com.spotify
</groupId>
<artifactId>docker-maven-plugin
</artifactId>
<version>1.0.0
</version>
<configuration>
<imageName>dic/${project.artifactId}:${project.version}
</imageName>
<dockerDirectory>${project.basedir}/docker
</dockerDirectory>
<dockerHost>${docker.host}
</dockerHost>
<resources>
<resource>
<targetPath>/
</targetPath>
<directory>${project.build.directory}
</directory>
<include>${project.build.finalName}.jar
</include>
</resource>
</resources>
<buildArgs>
<JAR_FILE>target/${project.build.finalName}.jar
</JAR_FILE>
</buildArgs>
</configuration>
<executions>
<execution>
<id>build-image
</id>
<phase>package
</phase>
<goals>
<goal>build
</goal>
</goals>
</execution>
<execution>
<id>tag-image
</id>
<phase>package
</phase>
<goals>
<goal>tag
</goal>
</goals>
<configuration>
<image>dic/${project.artifactId}:${project.version}
</image>
<newName>${docker.registry.url}/${project.artifactId}:${project.version}
</newName>
</configuration>
</execution>
<execution>
<id>push-image
</id>
<phase>deploy
</phase>
<goals>
<goal>push
</goal>
</goals>
<configuration>
<serverId>nexus-docker-registry
</serverId>
<registryUrl>${docker.registry.url}
</registryUrl>
<imageName>${docker.registry.url}/${project.artifactId}
</imageName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
4.nexus搭建docker私服(补充)
https://blog.csdn.net/it_freshman/article/details/109214524
附录
dockerfile-maven-plugin 插件
<plugin>
<groupId>com.spotify
</groupId>
<artifactId>dockerfile-maven-plugin
</artifactId>
<version>1.3.6
</version>
<configuration>
<repository>jhs/${project.artifactId}
</repository>
<tag>${project.version}
</tag>
<buildArgs>
<JAR_FILE>target/${project.build.finalName}.jar
</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
执行命令:mvn clean package dockerfile:build -Dmaven.test.skip=true