Docker Java Example

We shall learn following items in this Docker Java Example :

Build Docker Image with Java Application

1. Create a directory

A separate directory is useful to organise docker applications. For this Java Example, create a directory somewhere with name of your choice. We shall use the name java-application

2. Create Java Application

Create a simple Java File, in the directory java-application, with name HelloWorld.java containing the following content.

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("HelloWorld from Java Application running in Docker.");
    }
}

3. Dockerfile

Create a file with name Dockerfile. Dockerfile contains instructions to prepare Docker image with our Java Application.

Following is the content of Dockerfile.

FROM java:8
RUN javac HelloWorld.java
CMD ["java", "HelloWorld"]

4. Verify contents of java-application directory

java-application$ ls
Dockerfile  Hello.java

5. Build docker image

Login as root user. Navigate into java-application directory and run the following command. Instructions in the Dockerfile are executed.

$ docker build -t java-application .
Docker Java Example Build

Please observe that there is dot (.) at the end of the command. Docker image is successfully built.

6. Check the docker image

To display available docker images, run the following command.

$ sudo docker images
show available docker images list

Run Docker Java Example

Run the following command to run the java-application Docker image in a container.

$ sudo docker run java-application
Run Docker Java Example

The Java Application has run, and the print statement could be seen in the console.

Save Docker Image to a tar file

Save the Docker Image file to a tar file, so that the image file could be copied to other machines through disk storage devices like pen-drive, etc.

Run the following command to save Docker image as a tar file.

$ docker save -o /home/arjun/workspace/docker/java-application.tar java-application

Saving might take few seconds. Wait for the command to complete.

Docker Java Application Image Saved to tar file

Copy and Run the Docker Image file in another machine

You may copy the Docker image tar file to another computer.

Run the following command to load the Docker image into the Docker.

$ docker load -i /home/arjun/workspace/java-application.tar

Replace /home/arjun/workspace/java-application.tar with your file location.

root@arjun-VPCEH26EN:/home/arjun/workspace/docker# docker load -i /home/arjun/workspace/java-application.tar
cae669473e3f: Loading layer [==================================================>]  5.632kB/5.632kB
e2a73a5de6c4: Loading layer [==================================================>]  5.632kB/5.632kB
Loaded image: java-application:latest
root@arjun-VPCEH26EN:/home/arjun/workspace/docker#

You may run the image using the same command we used to run the image file after building.

$ sudo docker run java-application

Conclusion

In this Docker Tutorial – Docker Java Example, we have learnt to build a Docker Image with Java Application and also how to save the image to a file and transfer it to other computers or servers.