Open In App

How to create a Java Docker Container?

Last Updated : 11 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Java is one of the most popular languages and supports many enterprise applications. Running Java on local machines requires the installation of Java IDE, Java JDK, and Java JRE, and requires the setting of paths and environment variables. This might seem to be a hefty task especially if you just want to run a simple program. In this article, we will discuss how to run Java inside Docker Containers.

Build Your Docker Java Image

Building your own customized docker image requires a dockerfile. Dockerfile is a source code of a docker image dockerfile consisting of instructions required for the docker image to build.

Sample Dockerfile for Java

FROM java:8
WORKDIR /var/www/java
COPY . /var/www/java
RUN javac Sample.java
CMD ["java", "Sample"]


After writing the docker file you use the command mentioned below to build the docker image.

docker run -it <name of dockerfile>

Test The Java Web Application Without Docker

Follow the steps mentioned below to test and deploy the Java web application without docker.

Step 1: Pull the sourcecode which is available in the github by the https URL.

Step 2: Install maven in the virtual machine which is package manager of java application use the following command to build the .war application which is going to deploy in the form virtualization form.

mvn clean package

Step 3: You can tomcat to deploy the webapplication or you can use the local host to test the application by using the http://localhost:8080.

Create a Dockerfile for Java

To deploy java application in the form of container by using docker first you need to create dockerfile as shown in below.

Step 1: We need to select the base image you need to pull the base image by using the following instruction you can start your dockerfile. to know the syntax of dockerfile refer to What is Dockerfile Syntax?

FROM java:8

Step 2: Set the working directory for the image. With this command, Docker is told to make this path the default location for all ensuing commands. By doing this, we can use relative paths depending on the working directory rather than having to spell out entire file paths.

WORKDIR /var/www/java

Step 3: Copy the files of java into the required path here we can say to our working directory and give the command to run the java process by using the CMD command and start the process by using the following command. To know more docker commands refer to Docker – Instruction Commands.

COPY . /var/www/java

RUN javac Sample.java

CMD [“java”, “Sample”]

Create a Dockerfile For Java Web Application

If you want to run the java application in the form of containers then you need to first build an image of the application to build an image you need dockerfile following is the sample dockerfile for java web application.

Dockefile To Run Java Application As Container

Before writing the dockerfile mentioned below you need to build an .war file package using maven with it download all the dependencies required to download for the application and make it ready to deploy the application.

FROM tomcat:8.0.20-jre8
COPY target/java-web-app*.war /usr/local/tomcat/webapps/java-web-app.war

We are using the tomcat as an base image and coping the java-web-app*.war file which is build using maven will copied to the /usr/local/tomcat/webapps/java-web-app.war.

Create a .dockerignore File

While creating an docker image there will be some files which not required to include in the docker image and also if u include all this file in the docker image than the size of the image will get increased which will increase the latancey of the application.

.dockerignore file will help us to ignore all the file which not required for the docker image. As shown in the following.

To exclude the files which is shown below .file extension and all the files which are already present in the log directory.

logs/*

*.png

Build An Image

We will create a simple Java application with a print statement inside it. Refer to the program below. Note that your file name and Main class name should exactly match each other.

Java




class Sample{
     public static void main(String args[]){
         System.out.println("Welcome to GeeksForGeeks");
     }
}


Step 2: Create the Dockerfile

Have a look at the Dockerfile below.

FROM java:8
WORKDIR /var/www/java
COPY . /var/www/java
RUN javac Sample.java
CMD ["java", "Sample"]

In the above Dockerfile, we have pulled the Java base Image from DockerHub. We have set the working directory and copied the files to the working directory. After that, we have compiled our Java application and run the executable.

Note that your directory structure should look like this.

Create the Dockerfile

Step 3: Build the Docker Image

Now, you can use the Docker build command to build the Docker Image.

sudo docker build -t java-demo .

Build the Docker Image

Step 4: Running the Docker Container

After you have built your Docker Image, you can run your Docker container using the Docker run command.

sudo docker run -it java-demo

Running the Docker Container

You can see that the program has been executed successfully and the result has been printed after running the Container.

View Local Images

Lists all the pulled images which are present in our system.

$ docker images

Tag Images

f you use the following command then you can push only the latest tag of docker image to the registry.

 docker push my_image

If you want to push all the tags of docker image then you need tot use the following command.

docker push my-image -a

Docker Java Image – FAQ’s

1. What Is A Docker In Java?

A Docker in Java is a Java library that allows you to create, run, and manage Docker containers.

2. Why Use Docker With Java?

Docker containers are portable, meaning that they can be run on any machine that has Docker installed. This makes it easy to deploy Java applications to different environments, such as development, staging, and production.

3. Does Docker Need Java?

Java Web Application which is goign to run in the form of containers then it required java as an base image.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads