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

Build containers faster with Jib, a Google im...

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 Me Java Developer Building website for pet clinic Wants

    to containerize the backend Wants container on registry ilovejava.io/petclinic-app
  2. 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" ]
  3. 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/
  4. github.com/GoogleContainerTools/jib ... <build> <plugins> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin </artifactId> <version>1.0.0</version> <configuration>

    <imageName>ilovejava.io/petclinic-app </imageName> <baseImage>gcr.io/distroless/java </baseImage> <entryPoint>["java", "-jar", "/${project.build.finalName}.jar" ]</entryPoint> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory} </directory> <include>${project.build.finalName}.jar </include> </resource> </resources> </configuration> </plugin> </plugins> </build> ...
  5. 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
  6. github.com/GoogleContainerTools/jib I’m a Java developer, I don’t want to have

    to care about Dockerfiles Some Java Developer Somewhere
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. github.com/GoogleContainerTools/jib 1MB layer Docker registry Set of layers, container configurations,

    and manifests build 100MB layer 40MB layer registry 9MB layer send
  16. 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
  17. 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
  18. 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 ...