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.
-
Edit the
build.gradle
file to include the plug-in. In the respective blocks of yourbuild.gradle
along with the entries for existing repositories and plug-ins:- 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' } }
- Add the
jcenter
root level repository ( the plug-in dependencies are distributed throughjcenter
).repositories { jcenter() }
- Apply the plug-in.
apply plugin: 'application' apply plugin: 'com.bmuschko.docker-java-application' mainClassName = 'hello.Application'
- 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' }
- 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 port8080
on the container, and then tag the image asomc-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' } }
- Add the build script changes to pull in the plug-in.
- Edit
gradle/wrapper/gradle-wrapper.properties
and change the value ofdistributionUrl
to update the gradle version:distributionUrl=https\://services.gradle.org/distributions/gradle-4.8-bin.zip
- 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