springboot使用maven构建docker镜像并提交到阿里云镜像

500 阅读1分钟

首先注册阿里云镜像仓库,新建仓库,并获得仓库公网地址.

registry.cn-shanghai.aliyuncs.com/smartlife-docker/deploy

此处,需要注意,镜像仓库有密码,需要在首页进行密码重置.页面显示使用登录密码即可,但是实际使用中还是要重置一次比较靠谱.

以下为本地操作.

首先配置 maven 的 setting.xml

添加如下内容

      <server>
        <id>docker</id>   <!--此处为配置ID信息-->
        <username>a@a.com</username>   <!--用户名-->
        <password>123123</password>  <!--密码-->
        <configuration>
          <email>a@a.com</email>  <!--对应用户名-->
        </configuration>
      </server>

下面在程序中新增dockerfile,构建docker镜像

# Version 0.0.1
FROM java:8

VOLUME /data/deploy/tmp

ADD @project.build.finalName@.@project.packaging@ app.jar

RUN sh -c 'touch /app.jar'

EXPOSE 7074
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

dockerfile文件位于 

/src/main/docker/

修改pom文件

<docker.image.prefix>镜像名</docker.image.prefix>
<docker.repostory>阿里云仓库地址</docker.repostory>
<docker.registry.name>命名空间</docker.registry.name>

在build部分增加

docker-maven-plugin

插件

            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>0.4.13</version>
                <!--配置使用mvn clean package的同时进行docker构建-->
                <executions>
                    <execution>
                        <id>build-image</id>
                        <phase>package</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <skipDocker>false</skipDocker>
                    <!-- 私有仓库配置,需要settings.xml文件配合serverId对应的服务地址 -->
                    <serverId>docker-aliyun</serverId>
                    <registryUrl>${docker.repostory}/${docker.registry.name}/${project.name}</registryUrl>
                    <!-- <forceTags>true</forceTags> -->
                    <!--install阶段也上传,否则只有deploy阶段上传-->
                    <pushImage>true</pushImage>
                    <dockerDirectory>target/docker</dockerDirectory>
                    <imageName>
                        ${docker.repostory}/${docker.registry.name}/${project.name}:${project.version}
                    </imageName>
                    <dockerHost>http://192.168.125.245:2375</dockerHost>
                    <resources>
                        <rescource><!-- 将打包文件放入dockerDirectory指定的位置 -->
                            <targetPath>/</targetPath>
                            <directory>${project.build.directory}</directory>
                            <include>${project.build.finalName}.jar</include>
                        </rescource>
                    </resources>
                </configuration>
            </plugin>

在 build节点下增加 resources节点

        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/docker</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/Dockerfile</include>
                </includes>
                <targetPath>../docker</targetPath>
            </resource>
        </resources>

配置对应的dockerfile位置.

至此.运行 mvn clean package即可.完成本地jar包构建和推送docker镜像到阿里云仓库