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

xiaoxiao2021-02-28  13

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编译配置

添加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@"] 修改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 注册服务)

查看远程镜像仓库:

转载请注明原文地址: https://www.6miu.com/read-2350222.html

最新回复(0)