利用 Maven 命令生成项目框架
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
利用 Maven 打包
mvn package
java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App
//显示 Hello World!
//利用 maven-jar-plugin 就可以指定在 jar 中的 MANIFEST.MF 中Main-Class,才可以直接运行 jar
java -jar target/my-app-1.0-SNAPSHOT.jar
//显示 Hello World!
利用 Maven 安装包到本地仓库
mvn install
利用 Maven 创建镜像并且推送到Private Registry
首先要修改 pom.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>my-app</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<!-- docker-maven-plugin插件(不带Dockerfile文件) -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<mainClass>com.mycompany.app.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.13</version>
<configuration>
<!--用于指定镜像名称-->
<imageName>10.22.17.210:5000/${project.name}:${project.version}</imageName>
<!--用于指定基础镜像,相当于Dockerfile中的FROM指令-->
<baseImage>openjdk:8-jdk-slim</baseImage>
<!--相当于Dockerfile的ENTRYPOINT指令-->
<entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
<!--是否跳过docker build-->
<skipDockerBuild>false</skipDockerBuild>
<resources>
<resource>
<targetPath>/</targetPath>
<!--用于指定需要复制的根目录,${project.build.directory}表示target目录-->
<directory>${project.build.directory}</directory>
<!--用于指定需要复制的文件。${project.build.finalName}.jar指的是打包后的jar包文件。-->
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
<dockerHost>http://10.22.17.210:2375</dockerHost>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
其次必须在服务端启动 Docker Daemon 服务,并且运行私有 Registry。
///// start vi /lib/systemd/system/docker.service /////
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock -H fd:// --containerd=/run/containerd/containerd.sock
///// end vi /lib/systemd/system/docker.service /////
systemctl daemon-reload
systemctl restart docker
docker start myregistry
运行如下命令推送镜像
mvn package
mvn install
mvn docker:build -DpushImage
在服务端运行镜像
docker images
docker run --rm --name myapp 10.22.17.210:5000/my-app:1.0-SNAPSHOT
//显示 Hello World!
参考资料