Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Build containers faster with Jib, a Google image build tool for Java applications

Q Chen
June 14, 2018

Build containers faster with Jib, a Google image build tool for Java applications

Q Chen

June 14, 2018
Tweet

More Decks by Q Chen

Other Decks in Programming

Transcript

  1. github.com/GoogleContainerTools/jib
    Build containers
    faster with Jib
    A container image builder
    for Java applications

    View full-size slide

  2. Our Team
    Cloud Tools for Java
    Appu Goundan
    @loosebazooka
    Qingyang “Q” Chen
    @coollog

    View full-size slide

  3. github.com/GoogleContainerTools/jib
    Containers
    “Write once, run anywhere”

    View full-size slide

  4. github.com/GoogleContainerTools/jib
    Building a Java
    container

    View full-size slide

  5. github.com/GoogleContainerTools/jib
    Me
    Java Developer
    Building website for pet clinic
    Wants to containerize the
    backend
    Wants container on registry
    ilovejava.io/petclinic-app

    View full-size slide

  6. github.com/GoogleContainerTools/jib

    View full-size slide

  7. So I read some tutorials

    View full-size slide

  8. github.com/GoogleContainerTools/jib
    FROM ubuntu:14.04
    RUN apt-get update && apt-get install -y python-software-properties software-properties-common
    RUN add-apt-repository ppa:webupd8team/java
    RUN echo "oracle-java8-installer shared/accepted-oracle-license-v1-1 boolean true"
    | debconf-set-selections
    RUN apt-get update && apt-get install -y oracle-java8-installer maven
    ADD . /usr/local/petclinic
    RUN cd /usr/local/petclinic && mvn install
    CMD ["/usr/bin/java"
    , "-cp", "/usr/local/petclinic/target/petclinic-1.0.jar"
    , "petclinic.WebServer"
    ]

    View full-size slide

  9. So I read some more tutorials

    View full-size slide

  10. github.com/GoogleContainerTools/jib
    FROM openjdk:8
    COPY target/petclinic-*.jar /app.jar
    ENTRYPOINT java -jar /app.jar

    View full-size slide

  11. github.com/GoogleContainerTools/jib
    Problem: openjdk:8 is 284MB
    FROM openjdk:8
    COPY target/petclinic-*.jar /app.jar
    ENTRYPOINT java -jar /app.jar

    View full-size slide

  12. Some more searching

    View full-size slide

  13. github.com/GoogleContainerTools/jib
    FROM openjdk:8-jre-alpine
    COPY target/petclinic-*.jar /app.jar
    ENTRYPOINT java -jar /app.jar
    82 MB

    View full-size slide

  14. github.com/GoogleContainerTools/jib
    docs.docker.com/develop/develop-images/dockerfile_best-practices

    View full-size slide

  15. github.com/GoogleContainerTools/jib
    **
    !target/petclinic-*.jar
    .dockerignore

    View full-size slide

  16. Some more tutorials later

    View full-size slide

  17. github.com/GoogleContainerTools/jib
    FROM openjdk:8-jre-alpine
    COPY target/dependencies /app/dependencies
    COPY target/classes /app/classes
    ENTRYPOINT java -cp /app/dependencies/*:/app/classes petclinic.WebServer
    $ mvn dependencies:copy-dependencies to target/dependencies/

    View full-size slide

  18. github.com/GoogleContainerTools/jib
    ...



    com.spotify
    docker-maven-plugin

    1.0.0

    ilovejava.io/petclinic-app

    gcr.io/distroless/java

    ["java", "-jar", "/${project.build.finalName}.jar"
    ]


    /
    ${project.build.directory}

    ${project.build.finalName}.jar







    ...

    View full-size slide

  19. github.com/GoogleContainerTools/jib
    What did we
    do?
    1. Write first Dockerfile
    2. Reduce image size
    3. Don’t run installs
    4. Use better base image
    5. Write .dockerignore
    6. Improve incremental speed
    7. Switch to use a Maven plugin
    Order of layers to optimize for cache hits
    Use of multi-stage builds
    Understanding Docker cache mechanism and quirks Download and install Docker
    Have elevated privileges to run Docker daemon
    saturnism.me/talk/docker-tips-and-tricks

    View full-size slide

  20. github.com/GoogleContainerTools/jib
    Containerizing with Docker
    Project JAR
    docker
    Docker
    Daemon
    Container
    Image
    Dockerfile
    build
    send build
    push
    Registry

    View full-size slide

  21. github.com/GoogleContainerTools/jib
    I’m a Java developer, I
    don’t want to have to
    care about Dockerfiles
    Some Java Developer
    Somewhere

    View full-size slide

  22. github.com/GoogleContainerTools/jib
    Containerizing, simplified
    Project Container image
    build
    on registry

    View full-size slide

  23. github.com/GoogleContainerTools/jib
    Containerizing, simplified
    PetClinic project ilovejava.io/petclinic-app
    build

    View full-size slide

  24. github.com/GoogleContainerTools/jib
    1. Apply the plugin.
    1. Apply the plugin.
    2. mvn jib:build
    Steps:
    (or gradle jib)

    View full-size slide

  25. github.com/GoogleContainerTools/jib
    Demo
    $ git clone https://github.com/spring-projects/spring-petclinic && cd spring-petclinic
    $ ./mvnw compile com.google.cloud.tools:jib-maven-plugin:0.9.7:build \
    -Dimage=gcr.io/my-project/petclinic

    View full-size slide

  26. github.com/GoogleContainerTools/jib
    Project ilovejava.io/petclinic-app
    build
    Docker daemon
    build
    Docker context
    generate
    Advanced Configuration
    JVM flags OCI image format credentials program arguments

    View full-size slide

  27. github.com/GoogleContainerTools/jib
    A “compiler” for
    containers

    View full-size slide

  28. github.com/GoogleContainerTools/jib
    Dockerfile
    “script”
    FROM base container image
    Run commands to install dependencies
    COPY application files over
    Configure the entrypoint
    Run the container
    Produces some layers
    Produces some layers

    View full-size slide

  29. github.com/GoogleContainerTools/jib
    Compile Containerize

    View full-size slide

  30. github.com/GoogleContainerTools/jib
    Code
    Executable
    Compile

    View full-size slide

  31. github.com/GoogleContainerTools/jib
    Code
    Executable
    Compile
    Java
    Container
    Containerize

    View full-size slide

  32. github.com/GoogleContainerTools/jib
    Code
    Executable
    Compile
    Java
    Container
    Containerize
    Containers are the executables of the cloud.

    View full-size slide

  33. github.com/GoogleContainerTools/jib
    Java
    JAR
    jar

    View full-size slide

  34. github.com/GoogleContainerTools/jib
    Java
    JAR
    jar
    Java
    Container
    Jib

    View full-size slide

  35. How Jib Works

    View full-size slide

  36. github.com/GoogleContainerTools/jib
    Pure Java Speed
    What benefits do we get from Jib
    Reproducibility

    View full-size slide

  37. github.com/GoogleContainerTools/jib
    Pure Java

    View full-size slide

  38. github.com/GoogleContainerTools/jib
    A container
    image is a
    directory of files

    View full-size slide

  39. github.com/GoogleContainerTools/jib
    Docker
    Image
    Format
    Tarballs that compose into a
    single filesystem
    Tarball A Tarball B Tarball C
    /bin
    /usr
    /tmp
    /var
    /jdk /app.jar

    View full-size slide

  40. github.com/GoogleContainerTools/jib
    Docker
    Image
    Format
    Tarballs that compose into a
    single filesystem
    And a container configuration
    Tarball A Tarball B Tarball C
    /bin
    /usr
    /tmp
    /var
    /jdk /app.jar
    Environment variables, entrypoint, etc.
    Container configuration

    View full-size slide

  41. github.com/GoogleContainerTools/jib
    {
    "architecture" : "amd64",
    "os": "linux",
    "config": {
    "Env": [],
    "Entrypoint" : [
    "java",
    "-cp",
    "/app/libs/*:/app/resources/:/app/classes/" ,
    "com.test.HelloWorld"
    ]
    },
    "rootfs": {
    "type": "layers",
    "diff_ids" : [
    "sha256:46e7865bff73b5a0c610bf9f20c91dfafa2518ace8703faaffff551a4773b947" ,
    "sha256:6189abe095d53c1c9f2bfc8f50128ee876b9a5d10f9eda1564e5f5357d6ffe61" ,
    "sha256:e8292403028e724f0c7686ede4cd89180faa85aeb63cd0e7d560e8a459d83afe" ,
    "sha256:ff7666ffd3d45500f4af71f091a603413acb04d028ba03a6698f63819d246cb5" ,
    "sha256:db22fdca5c6344265d841ec106e683fb39914f356fb1d8e69accb466a396dc62" ,
    "sha256:9aa41c013edd2a6311dcdd4d26129b01b3ba0b08c8adb51759c63501a69d27f5"
    ]
    }
    }
    checksums

    View full-size slide

  42. github.com/GoogleContainerTools/jib
    Docker
    Image
    Format
    Tarballs that compose into a
    single filesystem
    And a container configuration
    And a manifest
    Tarball A Tarball B Tarball C
    /bin
    /usr
    /tmp
    /var
    /jdk /app.jar
    Tarballs A, B, C, and the configuration
    Manifest
    Environment variables, entrypoint, etc.
    Container configuration

    View full-size slide

  43. github.com/GoogleContainerTools/jib
    {
    "schemaVersion"
    : 2,
    "mediaType": "application/vnd.docker.distribution.manifest.v2+json"
    ,
    "config": {
    "mediaType": "application/vnd.docker.container.image.v1+json"
    ,
    "digest": "sha256:181b9f9c20bb2f7f485ffd038140551a758507d6255d46f4f62b3e504948fb86"
    ,
    "size": 635
    },
    "layers": [
    {
    "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip"
    ,
    "digest": "sha256:eb05f3dbdb543cc610527248690575bacbbcebabe6ecf665b189cf18b541e3ca"
    ,
    "size": 7695857
    },
    {
    "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip"
    ,
    "digest": "sha256:ba7c544469e514f1a9a4dec59ab640540d50992b288adbb34a1a63c45bf19a24"
    ,
    "size": 622796
    },
    {
    "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip"
    ,
    "digest": "sha256:15705ab016593987662839b40f5a22fd1032996c90808d4a1371eb46974017d5"
    ,
    ...
    ]
    }
    Unique identifiers

    View full-size slide

  44. github.com/GoogleContainerTools/jib
    distroless
    application layers
    application layers
    Jib image
    github.com/GoogleContainerTools/distroless

    View full-size slide

  45. github.com/GoogleContainerTools/jib
    Speed

    View full-size slide

  46. github.com/GoogleContainerTools/jib
    Docker
    registry
    Set of layers, container
    configurations, and manifests
    build
    100MB layer
    50MB layer
    registry
    send

    View full-size slide

  47. github.com/GoogleContainerTools/jib
    Docker
    registry
    Set of layers, container
    configurations, and manifests
    build
    100MB layer
    50MB layer
    registry
    cached
    send

    View full-size slide

  48. github.com/GoogleContainerTools/jib
    Docker
    registry
    Set of layers, container
    configurations, and manifests
    build
    100MB layer
    40MB layer
    registry
    9MB layer
    1MB layer

    View full-size slide

  49. github.com/GoogleContainerTools/jib
    1MB layer
    Docker
    registry
    Set of layers, container
    configurations, and manifests
    build
    100MB layer
    40MB layer
    registry
    9MB layer
    send

    View full-size slide

  50. github.com/GoogleContainerTools/jib
    Jib does an optimized build like
    FROM gcr.io/distroless/java
    COPY target/dependencies /app/dependencies
    COPY target/resources /app/resources
    COPY target/classes /app/classes
    ENTRYPOINT java -cp /app/dependencies/*:/app/resources:/app/classes my.app.Main

    View full-size slide

  51. github.com/GoogleContainerTools/jib
    Jib vs Docker

    View full-size slide

  52. github.com/GoogleContainerTools/jib
    Jib vs Docker

    View full-size slide

  53. github.com/GoogleContainerTools/jib
    Reproducibility

    View full-size slide

  54. github.com/GoogleContainerTools/jib
    Why
    reproducible
    ?
    Version Control Reduce variation between
    prod and dev

    View full-size slide

  55. github.com/GoogleContainerTools/jib
    How?
    Wipe metadata that vary
    between builds
    Timestamps
    Users
    Groups

    View full-size slide

  56. github.com/GoogleContainerTools/jib
    Possibilities for a container
    “compiler”

    View full-size slide

  57. github.com/GoogleContainerTools/jib
    Possibilities for a container
    “compiler”
    Smart inferences Container optimizations Even faster builds Smaller images
    Tools for running the container
    Run and debug on Kubernetes

    View full-size slide

  58. github.com/GoogleContainerTools/jib
    Demo
    $ gcloud container clusters create jib-demo

    View full-size slide

  59. github.com/GoogleContainerTools/jib
    Project
    kubernetes-dev:debug

    View full-size slide

  60. github.com/GoogleContainerTools/jib
    The Future
    More containerization tools and Java library for building container images
    Release Maven/Gradle plugin for run and debug on Kubernetes (with Skaffold)
    Be able to write code and have it run automatically in a distributed container cluster
    ...

    View full-size slide

  61. github.com/GoogleContainerTools/jib
    github.com/GoogleContainerTools/distroless
    saturnism.me/talk/docker-tips-and-tricks

    View full-size slide