Maven+Docker,发布到Registry

1、配置Pom.xml

<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <docker.repostory>registry.cn-hangzhou.aliyuncs.com</docker.repostory>
  <docker.registry.name>kingsy</docker.registry.name>
</properties>
<build>
	<finalName>app</finalName>
	<plugins>
		<!-- 生成Jar包 -->
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-jar-plugin</artifactId>
			<version>2.4</version>
			<configuration>
				<archive>
					<manifest>
						<addClasspath>true</addClasspath>
						<classpathPrefix>lib/</classpathPrefix>
						<mainClass>org.demo.docker_package.App</mainClass>
					</manifest>
				</archive>
			</configuration>
		</plugin>
		<!-- 打包Docker -->
		<plugin>
			<groupId>com.spotify</groupId>
			<artifactId>docker-maven-plugin</artifactId>
			<version>0.4.13</version>
			<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>${docker.registry.name}/${project.artifactId}:${project.version}</image>
                        <newName>${docker.repostory}/${docker.registry.name}/${project.artifactId}:${project.version}</newName>
                    </configuration>
				</execution>
				 <execution>
                    <id>push-image</id>
                    <phase>deploy</phase>
                    <goals>
                        <goal>push</goal>
                    </goals>
                    <configuration>
                        <imageName>${docker.repostory}/${docker.registry.name}/${project.artifactId}:${project.version}</imageName>
                    </configuration>
                </execution>    
			</executions>
			<configuration>
				<serverId>docker-aliyun</serverId>
                <registryUrl>registry.cn-hangzhou.aliyuncs.com</registryUrl>
				<pushImage>true</pushImage>
				<dockerDirectory>${project.basedir}</dockerDirectory>
				<imageName>${docker.repostory}/${docker.registry.name}/${project.artifactId}:${project.version}</imageName>
				<imageTags>
                    <imageTag>latest</imageTag>
                </imageTags>
				<resources>
			      <resource>
			        <targetPath>/</targetPath>
			        <directory>${project.build.directory}</directory>
			        <include>${project.build.finalName}.jar</include>
			      </resource>
			    </resources>
			</configuration>
		</plugin>
	</plugins>
</build>

二、创建Dockerfile

FROM java:7
ADD target/app.jar /home
WORKDIR /home
ENTRYPOINT ["java","-jar","app.jar"]

三、修改~/.m2/settings.xml

<settings>
     <server>
      <id>docker-aliyun</id>
      <username>kingsylin@vip.qq.com</username>
      <password>密码</password>
      <configuration>
            <email>kingsylin@vip.qq.com</email>
      </configuration>
    </server>
</settings>

四、执行maven命令

clean install

  

原文地址:https://www.cnblogs.com/kingsy/p/6411371.html