Open In App

Build And Deploy Java Application With Docker | Step-By-Step Tutorial

Docker is an OS-level virtualization that helps to build and deploy any program. Docker is used to utilize the resources, and it is compatible with all operating systems.

What Is Docker Container?

The Docker container is the part of Docker that provides a lightweight isolation environment for running applications. It is used because it takes fewer resources and helps to build, test, and deploy the application in a very small and easy way.



Prerequisite

Step-By-Step Process To Building Java application

import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpExchange;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
public class HelloWorld {
public static void main(String[] args) throws IOException {
// Create a new HTTP server listening on port 8001
HttpServer server = HttpServer.create(new InetSocketAddress(8001), 0);

// Create a context for handling requests
server.createContext("/", new HttpHandler() {
@Override
public void handle(HttpExchange exchange) throws IOException {
// Set the response headers
exchange.getResponseHeaders().set("Content-Type", "text/html");
exchange.sendResponseHeaders(200, 0);

// Get the response body stream
OutputStream responseBody = exchange.getResponseBody();

// Write the HTML content to the response body
String htmlResponse = "<html><body><h1>Hello, World!</h1></body></html>";
responseBody.write(htmlResponse.getBytes());

// Close the response body stream
responseBody.close();
}
});

// Start the server
server.start();

// Output a message indicating the server has started
System.out.println("Server is listening on port 8001...");
}
}

Now, Before deploying this application with the Docker we have to build it with the help of Dockerfile.

Dockerfile For Java Application | Step-By- Step Guide

It is a file where we writes all the process of building and accessing the application. In this file we use “YML” language which is a easy to learn and also called human language.



Step 1: Write the Dockerfile For Java Application.

Following is the Dockerfile for this application, the process will be same for all your java applications.

# Use a specific version of OpenJDK
FROM openjdk:11

# Set the working directory
WORKDIR /javaapp

# Copy only the necessary source files
COPY HelloWorld.java .

# Compile the Java program
RUN javac HelloWorld.java

# Set the default command to run the Java program
CMD ["java", "HelloWorld"]

Step 2: Build The Docker image by using dockerfile.

Now, After creating the Dockerfile we have to use Docker for building the image and container for deployment.

docker build -t "any name for your docker image"  .
docker build -t helloworld

Step 3: List all the Docker images.

Docker images 

Steps To Deplo The Java Application

After building the Docker image, now we are ready to deploy the application with Docker.For deployment we have to create the Docker container.

Step 1: Run The Docker container using Docker image.

docker run -p "port" -d  "your image name"
docker run -p 8001:8001 -d helloworld 

Step 2: List all the containers in docker.

docker ps

Now, We can access the java application with the help of localhost:port that you have given while creating the docker container.

Open browser and type

127.0.0.1:8001

Conclusion

We can build and deploy any application with Docker. Docker is a very optimized way to build a application and deploy on the server. In the Docker the important thing to learn is how the application will Compile and creating Dockerfile. Docker is very efficient way to deploy a application because it takes less resources.

Deploying Java Applications With Docker – FAQ’s

Can we build and Deploy any application with Docker?

Yes, we can build and deploy any application with Docker

Is the localserver is good for practice?

Yes, For practice on docker you can use localserver.

Is it necessary to create Dockerfile?

Yes, For building and Deploying any custom application then you have to create your custom Dockerfile


Article Tags :