Try   HackMD

Other Options For Building Containers

Creating A Dockerfile

  • Used to describe how to build a container for your app
  • Since Spring Boot just requires Java all you need is a JDK and your jar
  • Create a file in the root of your app called Dockerfile with the below content
FROM openjdk:8-jdk-alpine
VOLUME /tmp
COPY target/k8s-demo-app-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
The above is not intended for general purpose production use. It is the quickest, dirtiest way to get a Spring Boot app into a container. See this guide for more detail.


Choosing A JDK

  • JDK 8u192 introduced support for containers
  • The JDK will be able to see the memory and CPUs available to the container as opposed to what the host has available

Building A Container

  • To build a container for our app we need to run docker build
$ docker build -t my-dockerfile-k8s-demo ./
  • Running docker images will allow you to see the built container
$ docker images
REPOSITORY                            TAG                 IMAGE ID            CREATED             SIZE
my-dockerfile-k8s-demo                latest              ab449be57b9d        5 minutes ago       124MB

>

Jib It!

  • Jib is a tool from by Google to make building Docker containers easier
  • Runs as a Maven or Gradle plugin
  • Does not require a Docker deamon
  • Implements many Docker best practices automatically
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

Add Jib To Your POM

  • Open the POM file of your app and add the following plugin to the build section
...
<build>
   <plugins>
      ...
      <plugin>
          <groupId>com.google.cloud.tools</groupId>
          <artifactId>jib-maven-plugin</artifactId>
          <version>1.8.0</version>
          <configuration>
             <from>
                <image>openjdk:8u222-slim</image>
             </from>
             <to>
                <image>k8s-demo-app</image>
             </to>
             <container>
                <user>nobody:nogroup</user>
             </container>
          </configuration>
       </plugin>
       ...
   </plugins>
</build>
...

Using Jib To Build A Container

  • Now building a container is as simple as building our app
$ ./mvnw clean compile jib:dockerBuild
  • NOTE: This still uses your local Docker dameon to build the container

Putting The Container In A Registry

  • Up until this point the container only lives on your machine
  • It is useful to instead place the container in a registry
    • Allows others to use the container
  • Docker Hub is a popular public registry
  • Private registries exist as well

Using Jib To Push To A Registry


Modify Jib Plugin Configuration

  • Back in your POM file, modify the Jib plugin, specifically the image name
    • We also add an execution goal in order to do the container build and push whenever we run our Maven build
  • Be sure to replace USERNAME in the image name with your supplied user name
<plugin>
    <groupId>com.google.cloud.tools</groupId>
    <artifactId>jib-maven-plugin</artifactId>
    <version>1.8.0</version>
    <configuration>
        <from>
            <image>openjdk:8u222-slim</image>
        </from>
        <to>
            <image>harbor.workshop.demo.ryanjbaxter.com/[USERNAME]/k8s-demo-app</image>
        </to>
        <container>
            <user>nobody:nogroup</user>
        </container>
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>build</goal>
            </goals>
        </execution>
    </executions>
</plugin>