Wednesday 22 May 2019

Create Java docker hello world image


In my previous post, I explained how to create a simple image, that echoes message to the console. In this post, I am going to explain how to build a docker image that runs java application in it.

Step 1: Create a directory ‘helloWorldJava’

Step 2: Create ‘Dockerfile’.
$mkdir helloWorldJava
$
$cd helloWorldJava/
$touch Dockerfile
$
$ls
Dockerfile
$

Step 3: Update Dockerfile with below content.


Dockerfile
FROM openjdk

CMD java -version

Step 4: Build the docker image by executing below command from the directory where the 'Dockerfile' is located.

docker image build -t hellojava .

$ls
Dockerfile
$
$docker image build -t hellojava .
Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM openjdk
latest: Pulling from library/openjdk
35defbf6c365: Pull complete 
5362bf1d5ff8: Pull complete 
074486834918: Pull complete 
Digest: sha256:f0de92462537be994d15bbd6423e432caa699c61c131d3351ee10eb279dcb5f4
Status: Downloaded newer image for openjdk:latest
 ---> 831a029b6add
Step 2/2 : CMD java -version
 ---> Running in 8c3d41c012f4
Removing intermediate container 8c3d41c012f4
 ---> c8cb97224038
Successfully built c8cb97224038
Successfully tagged hellojava:latest


Step 5: Execute the command ‘docker image ls’ to see all the available images. You can see two images ‘openjdk’ and ‘hellojava’.
$docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hellojava           latest              c8cb97224038        52 seconds ago      470MB
openjdk             latest              831a029b6add        6 days ago          470MB


Step 6: Run the container with image hellojava by executing the command ‘docker run hellojava’.
$docker run hellojava
openjdk version "12.0.1" 2019-04-16
OpenJDK Runtime Environment (build 12.0.1+12)
OpenJDK 64-Bit Server VM (build 12.0.1+12, mixed mode, sharing)

That’s it you are done with your first Java application.

Since I do not specify any tag for openjdk, it will use latest jdk image. If you want to use a specific version of the image, append the : followed by tag name.

Example
FROM openjdk:7u201-alpine
FROM openjdk:8u201-alpine
FROM openjdk:13-jdk-windowsservercore

You can see all the available tags for openjdk from below location.



Previous                                                 Next                                                 Home

No comments:

Post a Comment