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 注册服务)

这里写图片描述

这里写图片描述

这里写图片描述

查看远程镜像仓库:

这里写图片描述

`docker-maven-plugin` 是 Maven 插件之一,用于将 Java 应用程序,包括基于 Spring Boot 的项目,构建为 Docker 镜像。以下是使用这个插件将应用打包推送到私有仓库的基本步骤: 1. **添加插件依赖**:首先,在 `pom.xml` 文件的 `<build>` 标签内添加 `docker-maven-plugin` 的依赖配置,例如: ```xml <plugins> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.4.15</version> <!-- 更新到最新版本 --> </plugin> </plugins> ``` 2. **配置镜像信息**:在 `<project>` 节点下设置 Dockerfile 相关的配置,如镜像名称、标签、基础镜像等。这里假设你有一个名为 `application.jar` 的主程序包文件: ```xml <build> <plugins> <plugin> <configuration> <imageName>${project.artifactId}</imageName> <imageTags> <tag>${project.version}</tag> <tag>latest</tag> <!-- 如果需要常量更新的标签 --> </imageTags> <baseImage>openjdk:8-jdk-alpine</baseImage> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin> </plugins> </build> ``` 3. **私有仓库配置**:如果你有一个私有Docker注册表(如JFrog Artifactory),你需要配置 MavenDocker 接口,通常通过环境变量 `DOCKER_REGISTRY_URL` 和 `DOCKER_USERNAME`、`DOCKER_PASSWORD` 或者认证密钥: ```xml <profiles> <profile> <id>docker-push</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <dockerPushRepository>your-private-repo:5000</dockerPushRepository> <!-- 替换为你的私有仓库地址 --> </properties> <build> <plugins> <plugin> <executions> <execution> <goals> <goal>push</goal> </goals> <phase>deploy</phase> </execution> </executions> <configuration> <registryUrl>${dockerPushRepository}</registryUrl> <username>your-docker-registry-username</username> <password>your-docker-registry-password</password> <!-- 或者从安全环境下获取密钥 --> </configuration> </plugin> </plugins> </build> </profile> </profiles> ``` 4. **构建和推送镜像**:运行 `mvn clean package docker:build docker:push -Pdocker-push` 来构建推送镜像
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

墨隐天涯

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

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

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

打赏作者

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

抵扣说明:

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

余额充值