In this tutorial you will see how to run a HelloWorld Java program using Docker or how to dockerize your Java application to run it with in a Docker container.
Steps to run Java hello world program with Docker
- We'll start by creating a directory where we can put the Java file and DockerFile.
- Create a Java file HelloWorld.java with in this directory.
HelloWorld.java
public class HelloWorld { public static void main(String[] args){ System.out.print("Java program Hello world From Docker"); } }
- Create a file named Dockerfile with in the same directory. Do use the exact name for the file with 'D'
capitalized and no extension. This is the file which defines the steps needed to create the image and run it.
Dockerfile is read from top to bottom and each instruction in a Dockerfile creates a layer in the image. When you change the Dockerfile and rebuild the image, only those layers which have changed are rebuilt.
Dockerfile
FROM openjdk:12-alpine COPY . /usr/src/myjavaapp WORKDIR /usr/src/myjavaapp RUN javac HelloWorld.java CMD ["java", "HelloWorld"]
Explanation for these steps is as given below-
- Docker container runs as a stand-alone container having the application code as well as other dependencies required to run the code. In order to run Java code with in the container JDK is also needed to provide both the build and runtime environment. Often, an image is based on another image and as explained our image is based on the Java image so that's what the first line does. There already is a Java image hosted in Docker hub, path is https://hub.docker.com/_/openjdk, that we can pull by giving the name of the image and tag (which includes Java version).
- COPY instruction is used to copy files or directories from source path to the to the filesystem of the container at the destination path. In our Dockerfile, files from the current directory (.) are copied to the path /usr/src/myjavaapp in the container.
- The WORKDIR instruction sets the working directory
- The RUN instruction will execute any commands, in our case it compiles the Java file using the javac tool.
- CMD is used to provide defaults for an executing container. One of the forms for providing CMD instructions is ["executable","param1","param2"]. This form is used in our Dockerfile where "java" is passed as executable and "HelloWorld" is passed as parameter making the command to be executed as java HelloWorld when the container is run.
- Build image
docker build command is used to build an image from the Dockerfile. One of the option you can use with build command is -t which is used to pass a name for your docker image and optionally a tag in a name:tag format.
Change directory to the path where Dockerfile is stored and run the following command.
D:\NETJS\java-docker>docker build -t java-hello-world:1.0 .
. means using the current directory as context
Build command goes through the instructions in the Dockerfile one by one and executes them as you can notice from the screen shot given below 1/4, 2/4, 3/4 and 4/4 which means 1 out of 4 instructions, 2 out of 4 instructions and so on.
D:\NETJS\java-docker>docker build -t java-hello-world:1.0 . => [internal] load build definition from Dockerfile => => transferring dockerfile: 172B => [internal] load .dockerignore => => transferring context: 2B => [internal] load metadata for docker.io/library/openjdk:12-alpine => [internal] load build context => => transferring context: 355B => [1/4] FROM docker.io/library/openjdk:12-alpine@sha256:fecd532eaee349b4d9e329148e99de77ffaf803e66e184a0e4d6b946bb97ffa3 => => resolve docker.io/library/openjdk:12-alpine@sha256:fecd532eaee349b4d9e329148e99de77ffaf803e66e184a0e4d6b946bb97ffa3 => => sha256:fecd532eaee349b4d9e329148e99de77ffaf803e66e184a0e4d6b946bb97ffa3 433B / 433B => => sha256:37b8b402893091d9120401a3d9c87d81a6fa967d96ca81e06a80d01605845c79 741B / 741B => => sha256:0c68e7c5b7a0cb1612ea7b14c460d1f165ae7250b8aa7a0e5e53ae6cdc846310 3.44kB / 3.44kB => => sha256:6c40cc604d8e4c121adcb6b0bfe8bb038815c350980090e74aa5a6423f8f82c0 2.75MB / 2.75MB => => sha256:9716b977a99b5983f66063ce42eaa529af94f6265b6908558041581c3ae5b4ac 197.66MB / 197.66MB => => extracting sha256:6c40cc604d8e4c121adcb6b0bfe8bb038815c350980090e74aa5a6423f8f82c0 => => extracting sha256:9716b977a99b5983f66063ce42eaa529af94f6265b6908558041581c3ae5b4ac => [2/4] COPY . /usr/src/myjavaapp => [3/4] WORKDIR /usr/src/myjavaapp => [4/4] RUN javac HelloWorld.java => exporting to image => => exporting layers => => writing image sha256:89ea194bd4b5fc9c571367caffc6e63279b05d68027e8b92a611b3389833dc80 => => naming to docker.io/library/java-hello-world:1.0
- Run Docker Image
Once the Docker image is created successfully you can run it using the docker run command by providing the name of the image which you want to run. Note that a container is a runnable instance of an image so this process can also be described as creating and starting a container where command is run.
D:\NETJS\java-docker>docker run java-hello-world:1.0 Java program Hello world From Docker
As you can see on running the image CMD instruction given in Dockerfile is executed and output is displayed.
That's all for this topic Run Java Application in Docker Container. If you have any doubt or any suggestions to make please drop a comment. Thanks!
Related Topics
You may also like-
No comments:
Post a Comment