springboot 利用 docker-maven插件生成docker镜像并推送到镜像仓库

springboot 利用 docker-maven插件生成docker镜像并推送到镜像仓库

一、环境配置
java、maven 、docker 安装及环境变量配置
    1. maven配置文件修改

    <server>
      <id>docker-aliyun</id>
      <username>私仓账号</username>
      <password>私仓密码</password>
      <configuration>
            <email>邮箱</email>
      </configuration>
    </server>

    2.docker 添加环境变量
    配置DOCKER_HOST环境变量:tcp://localhost:2375 打开daemon 2375访问权限(windows docker >> settings >> General)

二、添加Dockerfile文件及修改maven编译配置

  1. 添加Dockerfile
FROM java:8

MAINTAINER ziyun "wlcloudy@163.com"

# 环境变量
ENV WORK_PATH /home/project/eureka-server
ENV APP_NAME @project.build.finalName@.@project.packaging@
ENV APP_VERSION @project.version@

EXPOSE 5555

#VOLUME
VOLUME ["/home/project", "/tmp/data"]

#COPY
COPY $APP_NAME $WORK_PATH/

# WORKDIR
WORKDIR $WORK_PATH

# ENTRYPOINT
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom"]

# CMD
CMD ["-jar", "@project.build.finalName@.@project.packaging@"]
  1. 修改maven打包配置信息
<!-- 添加镜像仓库 -->
<properties>
        <docker.repository>仓库地址</docker.repository>
        <docker.registry.name>仓库名(即阿里云仓库命名空间名称)</docker.registry.name>
</properties>

<build>
        <finalName>EurekaServer</finalName>
        <!-- Dockerfile文件路径配置 -->
        <resources>
            <resource>
                <directory>src/main/docker</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/Dockerfile</include>
                </includes>
                <targetPath>../docker</targetPath>
            </resource>
        </resources>
        <plugins>
            <!-- 打包Docker -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>0.4.13</version>
                <executions>
                    <execution>
                        <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.repository}/${docker.registry.name}/${project.artifactId}:${project.version}</image>
                            <newName>${docker.repository}/${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.repository}/${docker.registry.name}/${project.artifactId}:${project.version}</imageName>
                        </configuration>
                    </execution>
                </executions>
                <configuration>
                    <!-- 私有仓库配置,需要settings.xml文件配合serverId对应的服务地址 -->
                    <serverId>docker-aliyun</serverId>
                    <registryUrl>${docker.repository}</registryUrl>
                    <pushImage>true</pushImage>
                    <dockerDirectory>target/docker</dockerDirectory>
                    <imageName>
                        ${docker.repository}/${docker.registry.name}/${project.artifactId}:${project.version}
                    </imageName>
                    <imageTags>
                        <!--docker的tag为项目版本号、latest-->
                        <imageTag>latest</imageTag>
                    </imageTags>
                    <resources>
                        <rescource>
                            <targetPath>/</targetPath>
                            <directory>${project.build.directory}</directory>
                            <include>${project.build.finalName}.jar</include>
                        </rescource>
                    </resources>
                </configuration>
            </plugin>
        </plugins>
    </build>

三、案例演示(eureka-server 注册服务)

这里写图片描述

这里写图片描述

这里写图片描述

查看远程镜像仓库:

这里写图片描述

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

墨隐天涯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值