BlackChen'site

项目使用docker打包发布并推送到远程仓库

springboot项目使用maven dockerfile 插件打包生成镜像

Harbor中创建项目

创建项目

创建项目

我们创建了docker-demo项目.

项目使用maven构建, 引入dockerfile-maven-plugin 并添加配置

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-deploy-plugin</artifactId>
				<configuration>
					<skip>true</skip>
				</configuration>
			</plugin>
			<plugin>
				<groupId>com.spotify</groupId>
				<artifactId>dockerfile-maven-plugin</artifactId>
				<version>1.4.13</version>
				<executions>
					<execution>
						<id>build-image</id>
						<!-- 执行阶段在deploy -->
						<phase>deploy</phase>
						<goals>
							<goal>build</goal>
						</goals>
					</execution>
					<execution>
						<id>tag-image-version</id>
						<phase>deploy</phase>
						<goals>
							<goal>tag</goal>
							<goal>push</goal>
						</goals>
						<configuration>
							<tag>${project.version}</tag>
						</configuration>
					</execution>
					<execution>
						<id>tag-image-latest</id>
						<phase>deploy</phase>
						<goals>
							<goal>tag</goal>
							<goal>push</goal>
						</goals>
						<configuration>
							<tag>latest</tag>
						</configuration>
					</execution>
				</executions>
				<configuration>
					<!-- 配置远程仓库地址 -->
					<repository>10.11.25.140/docker-demo/${artifactId}</repository>
					<!-- 配置远程仓库用户名密码 -->
					<username><![CDATA[admin]]></username>
					<password><![CDATA[Harbor12345]]></password>
					<buildArgs>
						<!-- 构建参数, 可以传入到DockerFile中 -->
						<JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
					</buildArgs>
				</configuration>
			</plugin>
		</plugins>
	</build>

修改对应的repository, username,password 为自己对应的地址和配置

Dockerfile 放在项目对应模块的顶层目录中.

FROM openjdk:8-jre-alpine3.8

ARG JAR_FILE
VOLUME /tmp
ENV JAVA_OPTS="-Xmx1024m" \
    RUNNING_ENV="dev"
ADD ${JAR_FILE} app.jar
RUN sh -c 'touch /app.jar' \
    ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

ENTRYPOINT [ "sh", "-c", "java -Denv=$RUNNING_ENV $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]

构建镜像并发布到远程仓库

mvn deploy

Harbor中查看

1576560111017

注意事项

  1. 新增私有仓库时,需要修改dockerinsecure-registries 配置

    • Mac
      • dockerpreferences中, 添加 insecure registries
        1576554994707
    • Linux
      registry-insecure
  2. pommaven-deploy-plugin,如果不跳过deploy将会报错

    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.8.2:deploy (default-deploy) on project docker-demo: Deployment failed: repository element was not specified in the POM inside distributionManagement element or in -DaltDeploymentRepository=id::layout::url parameter -> [Help 1]
    
    

相关git代码

springboot-docker-demo

评论