Docker

How to Build an Image for a Java Application by Using a Dockerfile

Docker is a forum that is designed for helping developers build, deploy, and run container applications. For that purpose, Docker images are used. Docker images are basically, an executable package of an application that contains all required to run an application. Developers can build different images for all types of applications, such as Java and many more.

The outcomes from this blog are:

How to Build an Image Using a Dockerfile for a Java Application?

Check out the following steps to build an image for a Java application through the Dockerfile.

Step 1: Access Java Folder

First of all, launch your preferred source code editor and access the folder where your Java application exists. For instance, we opened the Visual Studio Code source code editor and click on the open Folder… option:


Now, choose the particular folder from your local machine and hit the Select Folder button. Here, we selected the Java1 folder:


Step 2: Open Java Application File

Then, open your Java application folder and check out the existing files. In our case, only one file exists named demo2.java that contains the following code:

class demo1 {
    public static void main(String[] args) {
        System.out.println("Hi WelCome to my LinuxHint Page");
    }
}

 

Step 3: Create Dockerfile

Next, click on the below-highlighted icon to make a Dockerfile:


As you can see, the Dockerfile has been created successfully:


Step 4: Edit Dockerfile

Afterward, paste the following code into the Dockerfile:

FROM openjdk:11
WORKDIR /app
COPY . .
CMD ["java", "./demo1.java"]

 
Here:

    • FROM command is used to set the base image for subsequent instructions. Our base image is openjdk:11.
    • WORKDIR command is utilized for specifying the Docker container working directory at any given time. Here, /app is a working directory.
    • COPY command is utilized for copying files from the host system into the newly created Docker image. In our case, it copies the file from the current working directory and paste it into the current container path.
    • CMD command is utilized for specifying the command that is to be executed when a Docker container starts. Here, java is the executable and the demo1.java file is a parameter:

 

Step 5: Open New Terminal

Next, click on the below-highlighted three dots, choose the Terminal option, and hit New Terminal to launch a new terminal:


Step 6: Build Docker Image

After doing so, run the provided command to build a Docker image for a Java application:

docker build -t demo1 .

 
In the above-stated command:

    • docker build command is utilized to generate an image.
    • -t tag is utilized for specifying the image name.
    • demo1 is our image name.
    • . used to load the image:

 

Step 7: List Images

To verify new Docker image has been built or not for the Java application, run the following command:

docker images

 
According to the below-provided output, the new docker image exists in the list:


Step 8: Run Build Docker Image

Lastly, run the build Docker image through the docker run command along with the image name:

docker run demo1

 
As a result, it will execute the image and display all the instructions that exist:

Which type of Issues Often Occurs While Building Docker Images?

While building a new image on Docker, users often encounter multiple issues, which are listed below:

    • If developers used any powerful frameworks of programming language to conveniently create applications, they may find it hard to write Dockerfile for building application images.
    • Generated images may be large and consume more space because when users are trying to build an image, each command inside the Dockerfile generates a layer of the image which makes the image structure more complex and enlarges the image size.
    • If developers package their application source code in the final image, it may lead to code leakage.

That’s it! We have described the method to build an image for a Java application through Dockerfile.

Conclusion

Docker images contain the set of instructions that are used for running the application in a container like Java. To build an image using a Dockerfile for any Java application, first, access the particular folder which contains Java source code files. Then, create a new Dockerfile and add the required commands. After that, open the terminal and execute the docker build -t <image-name> command to build an image. In this guide, we have illustrated the procedure for building a new image using the Dockerfile.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.