Create a Docker Image for the Java Application

You can build Java applications as Docker containers in multiple ways. However, in this solution, you'll be using gradle.

Use a gradle plug-in called gradle-docker-plugin to generate the Dockerfile.

  1. Edit the build.gradle file to include the plug-in. In the respective blocks of your build.gradle along with the entries for existing repositories and plug-ins:

    1. Add the build script changes to pull in the plug-in.
      buildscript {
              repositories {
                  maven {
                      url 'https://plugins.gradle.org/m2/'
                  }
              }
              dependencies {
                 classpath 'com.bmuschko:gradle-docker-plugin:4.3.0'
              }
          }
      
    2. Add the jcenter root level repository ( the plug-in dependencies are distributed through jcenter).
      
      repositories {
          jcenter()
      }
    3. Apply the plug-in.
      apply plugin: 'application'
      apply plugin: 'com.bmuschko.docker-java-application'
      
      mainClassName = 'hello.Application'
    4. Add the jar task, because this creates the jar file that you'll pull in to the Docker image.
      
      jar {
          enabled = true
          baseName = 'gs-accessing-data-rest'
          version = '0.1.0'
      }
    5. Configure the gradle plug-in to retrieve metadata. This tells instructs the plug-in to generate a Dockerfile that is based on the openjdk:8 image, expose the port 8080 on the container, and then tag the image as omc-sample-app:0.1. Replace the tenancy and repository name with valid values. If you use a repository name that does not exist, it will be created for you, as long as you have repository creation privileges.
      docker {
          javaApplication {
              baseImage = 'openjdk:8'
              maintainer = 'Your Name "your.email@company.com"'
              ports = [8080]
              tag = 'region-code.ocir.io/my-tenant/my-repo/omc-sample-app:latest'
          }
      }
      
  2. Edit gradle/wrapper/gradle-wrapper.properties and change the value of distributionUrl to update the gradle version:

    distributionUrl=https\://services.gradle.org/distributions/gradle-4.8-bin.zip

  3. Build the image.

    ./gradlew clean jar dockerBuildImage

The preceding addition to build.gradle generates the application and the Dockerfile and runs the Docker build to generate and tag the image for the container.

You can verify this by listing the Docker images on your local repository. Run the following from a terminal:

docker images