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

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

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

    <server>
      <id>docker-aliyunid>
      <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>EurekaServerfinalName>
        
        <resources>
            <resource>
                <directory>src/main/dockerdirectory>
                <filtering>truefiltering>
                <includes>
                    <include>**/Dockerfileinclude>
                includes>
                <targetPath>../dockertargetPath>
            resource>
        resources>
        <plugins>
            
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
            <plugin>
                <groupId>com.spotifygroupId>
                <artifactId>docker-maven-pluginartifactId>
                <version>0.4.13version>
                <executions>
                    <execution>
                        <phase>packagephase>
                        <goals>
                            <goal>buildgoal>
                        goals>
                    execution>
                    <execution>
                        <id>tag-imageid>
                        <phase>packagephase>
                        <goals>
                            <goal>taggoal>
                        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-imageid>
                        <phase>deployphase>
                        <goals>
                            <goal>pushgoal>
                        goals>
                        <configuration>
                            <imageName>${docker.repository}/${docker.registry.name}/${project.artifactId}:${project.version}imageName>
                        configuration>
                    execution>
                executions>
                <configuration>
                    
                    <serverId>docker-aliyunserverId>
                    <registryUrl>${docker.repository}registryUrl>
                    <pushImage>truepushImage>
                    <dockerDirectory>target/dockerdockerDirectory>
                    <imageName>
                        ${docker.repository}/${docker.registry.name}/${project.artifactId}:${project.version}
                    imageName>
                    <imageTags>
                        
                        <imageTag>latestimageTag>
                    imageTags>
                    <resources>
                        <rescource>
                            <targetPath>/targetPath>
                            <directory>${project.build.directory}directory>
                            <include>${project.build.finalName}.jarinclude>
                        rescource>
                    resources>
                configuration>
            plugin>
        plugins>
    build>

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

springboot 利用 docker-maven插件生成docker镜像并推送到镜像仓库_第1张图片

springboot 利用 docker-maven插件生成docker镜像并推送到镜像仓库_第2张图片

springboot 利用 docker-maven插件生成docker镜像并推送到镜像仓库_第3张图片

查看远程镜像仓库:

springboot 利用 docker-maven插件生成docker镜像并推送到镜像仓库_第4张图片

你可能感兴趣的:(docker)