美文网首页程序人生
(六):maven docker 相关操作

(六):maven docker 相关操作

作者: 木夕月_fc7b | 来源:发表于2018-09-19 15:19 被阅读0次

    简介

    通过maven插件,结合maven命令,实现打包、生成镜像、推送到仓库中心功能。

    1、开启Docker daemon服务远程访问

    修改配置

    • [x] 修改 /etc/docker/daemon.json 文件,没有就创建,文件内容如下:
    {
      "hosts": [
        "tcp://0.0.0.0:2376",
        "unix:///var/run/docker.sock"
      ],
        "insecure-registries" : ["172.16.1.146"] 
    }
    

    重启docker

    sudo systemctl daemon-reload
    sudo systemctl restart docker
    

    验证

    [root@localhost ~]# docker -H 172.16.1.191:2376 images
    REPOSITORY                                      TAG                    IMAGE ID            CREATED             SIZE
    172.16.1.146:8888/wondertek/docker-test         1.0.0-20180713171831   063b60635335        25 minutes ago      727 MB
    172.16.1.146:8888/wondertek/docker-test         1.0.0-20180713171646   3b8deedff90d        27 minutes ago      727 MB
    172.16.1.146:8888/wondertek/docker-test         1.0.0-20180713091144   aa898d0cbd28        32 minutes ago      727 MB
    172.16.1.146:8888/wondertek/docker-test         1.0.0-2018071308       a91e9e3b0678        About an hour ago   727 MB
    172.16.1.146:8888/wondertek/docker-test         1.0.0-20180713083134   a91e9e3b0678        About an hour ago   727 MB
    172.16.1.146:8888/wondertek/docker-test         1.0.0-20180713080550   db286edbbc18        About an hour ago   727 MB
    

    2、MAVEN配置

    此处根据dockerfile生成镜像,
    pom.xml中添加插件。

    插件git地址:https://github.com/spotify/dockerfile-maven

    <!-- 生成时间戳 -->
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>1.9.1</version>
        <executions>
            <execution>
                <id>timestamp-property</id>
                <goals>
                    <goal>timestamp-property</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <name>current.time</name>
            <pattern>yyyyMMddHHmmss</pattern>
            <timeZone>GMT+8</timeZone>
        </configuration>
    </plugin>
    
    <!-- 打包生成镜像、push镜像到私有镜像中心 -->
    <plugin>
        <groupId>com.spotify</groupId>
        <artifactId>dockerfile-maven-plugin</artifactId>
        <version>1.4.0</version>
        <executions>
            <execution>
                <id>default</id><!-- 要绑定到的生命周期的阶段 -->
                <phase>install</phase><!-- 要绑定到的生命周期的阶段 -->
                <goals> <!-- 要绑定的插件的目标 -->
                    <goal>build</goal>
                    <goal>push</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <!-- 私有镜像中心的用户名 -->
            <username>registry</username>
            <!-- 私有镜像中心的密码 -->
            <password>******</password>
            <!-- 172.16.1.146:私有镜像中心地址; wondertek/${project.artifactId}:镜像名称-->
            <repository>172.16.1.146/wondertek/${project.artifactId}</repository>
            <!-- 镜像版本号 -->
            <tag>${project.version}-${current.time}</tag>
            <buildArgs>
                <!-- 参数,提供给dockerfile使用 -->
                
                <JAR_FILE>target/docker-test-1.0.0.jar</JAR_FILE>
            </buildArgs>
    
        </configuration>
    </plugin>
    

    3、dockerfile配置

    官网说明:https://docs.docker.com/develop/develop-images/dockerfile_best-practices/

    # 拉取基础镜像
    FROM hub.c.163.com/library/java:8-jdk
    # 镜像的作者
    MAINTAINER csp@xxx.com
    
    #挂载目录,通过 VOLUME 指令创建的挂载点,无法指定主机上对应的目录,是自动生成的
    VOLUME ["/data1","/data2"]
    
    RUN ["mkdir", "-p", "/app"]
    
    #结合maven插件dockerfile-maven-plugin的打包使用
    ARG JAR_FILE
    ADD ${JAR_FILE} /app/app.jar
    
    #为后面的 RUN, CMD, ENTRYPOINT, ADD 或 COPY 指令设置镜像中的当前工作目录。
    #WORKDIR  /usr/local/docker/test
    
    #拷贝当前目录文件到容器/app
    #COPY .  /app
    
    #与 COPY 类似,从 build context 复制文件到镜像。不同的是,如果 src 是归档文件(tar, zip, tgz, xz 等),文件会被自动解压到 dest。
    #ADD src dest
    
    #设置环境变量,环境变量可被后面的指令使用
    ENV EVN_SET_TEST "WELCOME TO DOCKERFILE CONTAINER!"
    
    ##################
    #  RUN、CDM、ENTRYPOINT 命令都包含两种格式:Shell 格式和 Exec 格式
    #  CMD还可以放在ENTRYPOINT后,为其传递参数。
    #####  shell 格式:######
    ##  底层会调用 /bin/sh -c <command>
    
    # 在容器中运行指定的命令
    RUN echo $EVN_SET_TEST
    
    # 容器启动命令 只有最后一个生效,CMD 可以被 docker run 之后的参数替换。
    #只有最后一个生效
    CMD echo "CMD Hello world"
    
    #配置容器启动时运行的命令
    ENTRYPOINT echo "ENTRYPOINT Hello, $EVN_SET_TEST"
    
    ######  Exec 格式: #####
    ## 当指令执行时,会直接调用 <command>,不会被 shell 解析
    # ENTRYPOINT ["/bin/echo", "Hello, $EVN_SET_TEST"]
    # 正确写法应该为:
    # ENTRYPOINT ["/bin/sh", "-c", "echo Hello, $EVN_SET_TEST"]
    
    # 为Exec 格式的ENTRYPOINT传递参数,结果输出Hello, $EVN_SET_TEST dockerfile world
    # CMD ["dockerfile world"]
    
    #只有最后一个生效
    ENTRYPOINT ["java","-jar","/app/app.jar"]
    
    
    
    
    
    #表示哪个端口提供服务的提示,宿主机如果要访问,需要结合-P参数联合使用。
    EXPOSE 8080
    

    4、build publish镜像

    配置远程docker环境变量

    export DOCKER_HOST=tcp://172.16.1.191:2376
    

    windows直接在环境变量中添加。

    mvn命令执行

    build镜像:
    mvn clean package dockerfile:build -DskipTests
    

    注意:
    Image will be built as xxxxx这一步会比较慢,需要将文件上传到服务器上(等待时间取决于文件大小和上传速度)

    发布到镜像中心,注意,该命令不会build镜像。
    mvn  dockerfile:push
    
    配置中 build 和 publish 都绑定在install周期上。
    如果不想触发相关流程,可以添加相关例外参数。
    mvn clean install -Ddockerfile.skip
    
    命令 说明
    dockerfile.skip Disables the entire dockerfile plugin; all goals become no-ops.
    dockerfile.build.skip Disables the build goal; it becomes a no-op.
    dockerfile.tag.skip Disables the tag goal; it becomes a no-op.
    dockerfile.push.skip Disables the push goal; it becomes a no-op.
    也可以取消pom.xml中executions的配置,通过 mvn clean install dockerfile:build dockerfile:push -DskipTests 构建并且推送到注册中心
    

    远程镜像查看:

    [root@localhost log]# docker -H 172.16.1.191:2376 images
    REPOSITORY                                      TAG                    IMAGE ID            CREATED             SIZE
    172.16.1.146:8888/wondertek/docker-test         1.0.0-20180713171831   063b60635335        2 days ago          727 MB
    172.16.1.146:8888/wondertek/docker-test         1.0.0-20180713171646   3b8deedff90d        2 days ago          727 MB
    172.16.1.146:8888/wondertek/docker-test         1.0.0-20180713091144   aa898d0cbd28        2 days ago          727 MB
    172.16.1.146:8888/wondertek/docker-test         1.0.0-2018071308       a91e9e3b0678        2 days ago          727 MB
    172.16.1.146:8888/wondertek/docker-test         1.0.0-20180713083134   a91e9e3b0678        2 days ago          727 MB
    172.16.1.146:8888/wondertek/docker-test         1.0.0-20180713080550   db286edbbc18        2 days ago          727 MB
    127.0.0.1:5200/docker-test                      1.0.0-20180705042837   1e5b41afb0a1        10 days ago         727 MB
    dockerlocal/docker-test                         1.0.0                  7d1c3e14aede        10 days ago         643 MB
    docker.io/postgres                              10-alpine              e273e6bfba1c        2 weeks ago         39.5 MB
    

    镜像历史层级查看:

    --no-trunc 查看完整的命令

    [root@localhost log]# docker history 172.16.1.146:8888/wondertek/docker-test:1.0.0-20180716100040 --no-trunc
    IMAGE                                                                     CREATED             CREATED BY                                                                                                                                                                                                                               SIZE                COMMENT
    sha256:c4cf1fc7cae80b238d46b5741834140c3188e45ba929c92cd1e35145c38a7bc1   40 minutes ago      /bin/sh -c #(nop)  EXPOSE 8080/tcp                                                                                                                                                                                                       0 B                 
    sha256:539db240ca1f6ec8b260e1af3ffa0a12a160b49d470644090bb9bfc26729b53e   40 minutes ago      /bin/sh -c #(nop)  ENTRYPOINT ["java" "-jar" "./app.jar"]                                                                                                                                                                                0 B                 
    sha256:cccad169f4dfa5b3fd6e2df754b10ae5f45541aa7d4964da4f60557643ac5796   40 minutes ago      /bin/sh -c #(nop)  ENTRYPOINT ["/bin/sh" "-c" "echo \"ENTRYPOINT Hello, $EVN_SET_TEST\""]                                                                                                                                                0 B                 
    sha256:e16f9ebbab1b040da6332b0fd5f0e36066219b8793fad42d1f23e8d7dbd6a412   40 minutes ago      /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "echo \"CMD Hello world\""]                                                                                                                                                                       0 B                 
    sha256:d06785ad2059e9718e3c5a6c0334d34027232efe719dcc94b0d391ce2fad650b   40 minutes ago      |1 JAR_FILE=target/docker-test-1.0.0.jar /bin/sh -c echo $EVN_SET_TEST                                                                                                                                                                   0 B                 
    sha256:2517b2a4980975b64f648b6a9104255f13db0cc409aaa72fa66dbfe61d6e4e96   40 minutes ago      /bin/sh -c #(nop)  ENV EVN_SET_TEST=WELCOME TO DOCKERFILE CONTAINER!                                                                                                                                                                    0 B                 
    sha256:7daa3e4150f810e727993d1ddc99debe6c0c397e91f02ffd7dd65abe8e1b7171   40 minutes ago      /bin/sh -c #(nop) ADD file:be73bf23e734c61fa90ae97fe0b66384632a30fa3f9c967b1afa127a2f52c198 in /app/app.jar                                                                                                                              83.6 MB             
    sha256:d42fa5d86ebf2508a9e3de305e04ac3e66dc8e7791c127487161a00ecd2ee710   40 minutes ago      /bin/sh -c #(nop)  ARG JAR_FILE                                                                                                                                                                                                          0 B                 
    sha256:7b0922396475e204d624772921470e94b7348f52d7ceb8717d4b38871387c303   40 minutes ago      /bin/sh -c #(nop)  VOLUME [/data1 /data2]                                                                                                                                                                                                0 B                 
    sha256:beb6d7c2897188c448f6e33a91cbc7110aac171cc5556663e01bbd806e9fb10f   40 minutes ago      /bin/sh -c #(nop)  MAINTAINER csp@xxx.com                                                                                                                                                                                                0 B                 
    sha256:a001fc27db5aae61800c78fa1c794177f07e5422525ded583ff2ccde6f6107bc   19 months ago       /bin/sh -c /var/lib/dpkg/info/ca-certificates-java.postinst configure                                                                                                                                                                    418 kB              
    <missing>                                                                 19 months ago       /bin/sh -c set -x  && apt-get update  && apt-get install -y   openjdk-8-jdk="$JAVA_DEBIAN_VERSION"   ca-certificates-java="$CA_CERTIFICATES_JAVA_VERSION"  && rm -rf /var/lib/apt/lists/*  && [ "$JAVA_HOME" = "$(docker-java-home)" ]   352 MB              
    <missing>                                                                 19 months ago       /bin/sh -c #(nop)  ENV CA_CERTIFICATES_JAVA_VERSION=20140324                                                                                                                                                                             0 B                 
    <missing>                                                                 19 months ago       /bin/sh -c #(nop)  ENV JAVA_DEBIAN_VERSION=8u111-b14-2~bpo8+1                                                                                                                                                                            0 B                 
    <missing>                                                                 19 months ago       /bin/sh -c #(nop)  ENV JAVA_VERSION=8u111                                                                                                                                                                                                0 B                 
    <missing>                                                                 19 months ago       /bin/sh -c #(nop)  ENV JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64                                                                                                                                                                       0 B                 
    <missing>                                                                 19 months ago       /bin/sh -c {   echo '#!/bin/sh';   echo 'set -e';   echo;   echo 'dirname "$(dirname "$(readlink -f "$(which javac || which java)")")"';  } > /usr/local/bin/docker-java-home  && chmod +x /usr/local/bin/docker-java-home               87 B                
    <missing>                                                                 19 months ago       /bin/sh -c #(nop)  ENV LANG=C.UTF-8                                                                                                                                                                                                      0 B                 
    <missing>                                                                 19 months ago       /bin/sh -c echo 'deb http://deb.debian.org/debian jessie-backports main' > /etc/apt/sources.list.d/jessie-backports.list                                                                                                                 55 B                
    <missing>                                                                 19 months ago       /bin/sh -c apt-get update && apt-get install -y --no-install-recommends   bzip2   unzip   xz-utils  && rm -rf /var/lib/apt/lists/*                                                                                                       1.29 MB             
    <missing>                                                                 19 months ago       /bin/sh -c apt-get update && apt-get install -y --no-install-recommends   bzr   git   mercurial   openssh-client   subversion     procps  && rm -rf /var/lib/apt/lists/*                                                                 123 MB              
    <missing>                                                                 19 months ago       /bin/sh -c apt-get update && apt-get install -y --no-install-recommends   ca-certificates   curl   wget  && rm -rf /var/lib/apt/lists/*                                                                                                  44.3 MB             
    <missing>                                                                 19 months ago       /bin/sh -c #(nop)  CMD ["/bin/bash"]                                                                                                                                                                                                     0 B                 
    <missing>                                                                 19 months ago       /bin/sh -c #(nop) ADD file:1d214d2782eaccc743b8d683ccecf2f87f12a0ecdfbcd6fdf4943ce616f23870 in /                                                                                                                                         123 MB          
    

    私有镜像中心查看,这里使用的是前面介绍的harbor:

    image

    相关文章

      网友评论

        本文标题:(六):maven docker 相关操作

        本文链接:https://www.haomeiwen.com/subject/vdeinftx.html