# 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 ```Dockerfile FROM openjdk:8-jdk-alpine VOLUME /tmp COPY target/k8s-demo-app-0.0.1-SNAPSHOT.jar app.jar ENTRYPOINT ["java","-jar","/app.jar"] ``` :exclamation: 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](https://spring.io/guides/topicals/spring-boot-docker) 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` ```bash $ docker build -t my-dockerfile-k8s-demo ./ ``` * Running `docker images` will allow you to see the built container ```bash $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE my-dockerfile-k8s-demo latest ab449be57b9d 5 minutes ago 124MB ``` --- --> --- ### Jib It! * [Jib](https://github.com/GoogleContainerTools/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 ![](https://i.imgur.com/fkzRqe2.png) ![](https://i.imgur.com/b4uolYy.png) --- ### Add Jib To Your POM * Open the POM file of your app and add the following `plugin` to the `build` section ```xml ... <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 ```bash $ ./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](https://hub.docker.com/) is a popular public registry * Private registries exist as well --- ### Using Jib To Push To A Registry * By default [Jib will try and push the image to Docker Hub](https://github.com/GoogleContainerTools/jib/tree/master/jib-maven-plugin#using-docker-hub-registry) * We will push our container to a registry associated with our Kubernetes cluster * Login to [https://harbor.workshop.demo.ryanjbaxter.com/](https://harbor.workshop.demo.ryanjbaxter.com/) with your supplied username and password * Once logged in you will see two projects, one called `library` and the other with the same name as your user, we will use the one with the same name as your user --- ### 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** ```xml <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> ``` ---