Git Product home page Git Product logo

minimum-docker-image-java's Introduction

Chapter 2: Packaging Application using Docker

2.1 Docker Workflow

Slides

  1. Explain the Docker workflow of CLI, Engine and Registry

2.2 Docker Image and Container

Slides

  1. Concepts to be explained:

    1. Dockerfile

      1. FROM, ADD, CMD

        FROM debian
        
        CMD echo "hello world!"

        Another Dockerfile:

        FROM openjdk
        
        CMD java -version
      2. Amazon Corretto

      3. Multi-stage Dockerfile

    2. Build context

    3. Image tagging

    4. Run a container

    5. Port forward

2.3 Build Docker Image using Dockerfile

Code

  1. Change to app directory

  2. Show and explain multi-stage Dockerfile

  3. Create local repo:

    mvn -Dmaven.repo.local=./repository clean package
    tar cf repository.tar.gz ./repository
  4. Create Docker image:

    docker image build -t arungupta/greeting .
  5. Show Docker image:

    docker image ls

2.4 Run Docker Container

  1. Run container and show logs:

    docker container run -p 8080:8080 arungupta/greeting
  2. Access application:

    curl http://localhost:8080/hello
  3. Show list of containers:

    docker container ls
  4. Terminate container:

    docker container stop <name>
    docker container rm <name>
  5. Alternatively, start the container:

    docker container run -p 8080:8080 --name greeting -d arungupta/greeting
  6. Show logs:

    docker container logs greeting
  7. Access application:

    curl http://localhost:8080/hello
  8. Remove container:

    docker container rm -f greeting

    Explain forced termination.

2.5 Build Docker Image using Jib

Slides

The benefits of using Jib over a multi-stage Dockerfile build include:

  1. Fast because it leverages Docker image layer caching

  2. No need for Docker daemon

  3. No need to write a Dockerfile or build the archive of m2 dependencies

  4. Maven and Gradle plugin

Code

  1. Explain Jib maven profile

  2. Create Docker image to Docker daemon:

    mvn package -Pjib

    Alternatively, Jib can also build without Docker daemon, and directly to a registry:

    mvn jib:build -Pjib
  3. Show image history:

    docker image history arungupta/greeting

2.6 Minimal Docker Image using Custom JRE

Prereq

  1. Create an Ubuntu EC2 instance

  2. Login:

    ssh -i ~/.ssh/arun-us-east1.pem [email protected]
  3. Install default JDK:

    sudo apt-get update
    sudo apt-get install -y default-jdk
  4. Clone the repo:

    git clone https://github.com/arun-gupta/lil-kubernetes-for-java
  5. Build the application:

    cd lil-kubernetes-for-java/app
    sudo apt install -y maven
    mvn package
  6. Install Docker:

    sudo apt install -y docker.io
  7. Build the Docker image:

    mvn -Dmaven.repo.local=./repository clean package
    tar cf repository.tar.gz ./repository
    sudo docker image build -t arungupta/greeting .

Code

We noticed that our Docker image was 489MB. Even though we used OpenJDK:8-JRE as the base image, but it still contains a lot of JDK functionality that is not needed by our application. JDK 9 introduced module systems that allows you to selectively include the functionality needed for your application, and leave everything else behind. We’ll look at how the Docker image size for our Java application can be reduced using the tools provided by JDK 9 onwards.

jlink is a tool available in JDK 9 onwards, and allows to assemble and optimize a set of modules and their dependencies into a custom runtime image.

  1. Create a custom JRE for the Spring Boot application:

    cp target/greeting.war target/greeting.jar
    jlink \
    	--output myjre \
    	--add-modules $(jdeps --print-module-deps target/greeting.jar),\
    	java.xml,jdk.unsupported,java.sql,java.naming,java.desktop,\
    	java.management,java.security.jgss,java.instrument
  2. Dockerfile.jre

    FROM debian:9-slim
    COPY target/greeting.war /root
    COPY myjre /root/myjre
    EXPOSE 8080 5005
    WORKDIR /root
    CMD ["./myjre/bin/java", "-jar", "greeting.war"]
  3. Build Docker image using this custom JRE:

    sudo docker image build -f Dockerfile.jre -t arungupta/greeting:jre-slim .
  4. List Docker images and show the difference in sizes:

    ubuntu@ip-172-31-35-132:~$ sudo docker image ls
    REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
    arungupta/greeting   jre-slim            4334d1ebec46        2 minutes ago       160MB
    arungupta/greeting   latest              079dd8de2731        5 minutes ago       489MB
  5. Run the container:

    sudo docker container run -d -p 8080:8080 arungupta/greeting:jre-slim
  6. Access the application:

    curl http://localhost:8080/hello

minimum-docker-image-java's People

Contributors

supunuom avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.