Java Basics With Docker Containers

Mohamad MahmoodMohamad Mahmood
4 min read

[1] Hello World

[1.1] Create project directory

mkdir helloworld

cd helloworld

[1.2] Create Java file

// HelloWorld.java
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

[1.3] Compile to Java class

javac HelloWorld.java

[1.4] Define Docker image (in Dockerfile)

# Use the OpenJDK 17 base image
FROM openjdk:17

# Set the working directory in the container
WORKDIR /app

# Copy the compiled Java class into the container
COPY HelloWorld.class .

# Command to run the Java application
CMD ["java", "HelloWorld"]

[1.5] Build Docker image (execute Dockerfile)

docker build -t hello-world-java .

[1.6] Run Docker container

docker run -t hello-world-java

[1.7] Stop container

docker ps -a

docker stop <container_id>

[1.8] Remove container

docker rm <container_id>

[1.9] Remove image

docker rmi hello-world-java

.


[2] Conditional Expression

[2.1] Create project directory

mkdir EvenOddChecker
cd EvenOddChecker

[2.2] Create Java file

// EvenOddChecker.java
import java.util.Scanner;

public class EvenOddChecker {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Prompt the user to enter a number
        System.out.print("Enter an integer: ");
        int number = scanner.nextInt();

        // Check if the number is even or odd
        if (number % 2 == 0) {
            System.out.println(number + " is even.");
        } else {
            System.out.println(number + " is odd.");
        }

        // Close the scanner
        scanner.close();
    }
}

[2.3] Compile to Java class

javac EvenOddChecker.java

[2.4] Define Docker container (in Dockerfile)

# Use the OpenJDK 17 base image
FROM openjdk:17

# Set the working directory in the container
WORKDIR /app

# Copy the Java source file into the container
COPY EvenOddChecker.java .

# Compile the Java program
RUN javac EvenOddChecker.java

# Command to run the Java program
CMD ["java", "EvenOddChecker"]

[2.5] Build Docker image (execute Dockerfile)

docker build -t even-odd-checker .

[2.6] Run Docker container

docker run -it even-odd-checker

[2.7] Stop container

docker ps -a

docker stop <container_id>

[2.8] Remove container

docker rm <container_id>

[2.9] Remove image

docker rmi even-odd-checker

.


[3] Array List

[3.1] Create project directory

mkdir EmployeeListApp
cd EmployeeListApp

[3.2] Create Java file

// EmployeeList.java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class Employee {
    private int id;
    private String name;

    // Constructor
    public Employee(int id, String name) {
        this.id = id;
        this.name = name;
    }

    // Getters
    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }
}

public class EmployeeList {
    private List<Employee> employees;

    public EmployeeList() {
        employees = new ArrayList<>();
        employees.add(new Employee(1, "Alice"));
        employees.add(new Employee(2, "Bob"));
        employees.add(new Employee(3, "Charlie"));
        employees.add(new Employee(4, "David"));
        employees.add(new Employee(5, "Eve"));
    }

    // Function to find an employee by ID
    public Employee findEmployeeById(int id) {
        for (Employee employee : employees) {
            if (employee.getId() == id) {
                return employee;
            }
        }
        return null; // Return null if no employee found
    }

    public static void main(String[] args) {
        EmployeeList employeeList = new EmployeeList();
        Scanner scanner = new Scanner(System.in);

        // Print all employees
        employeeList.employees.forEach(employee -> 
            System.out.println("ID: " + employee.getId() + ", Name: " + employee.getName())
        );

        // Prompt the user to enter an employee ID
        System.out.print("Enter an employee ID to search: ");
        int searchId = scanner.nextInt();

        // Find the employee by ID
        Employee foundEmployee = employeeList.findEmployeeById(searchId);

        if (foundEmployee != null) {
            System.out.println("Found Employee: ID: " + foundEmployee.getId() + ", Name: " + foundEmployee.getName());
        } else {
            System.out.println("Employee with ID " + searchId + " not found.");
        }

        // Close the scanner
        scanner.close();
    }
}

[3.3] Compile to Java class

javac EmployeeList.java

[3.4] Define Docker container (in Dockerfile)

# Use the OpenJDK 17 base image
FROM openjdk:17

# Set the working directory in the container
WORKDIR /app

# Copy the Java source file into the container
COPY EmployeeList.java .

# Compile the Java program
RUN javac EmployeeList.java

# Command to run the Java program
CMD ["java", "EmployeeList"]

[3.5] Build Docker image (execute Dockerfile)

docker build -t employee-list-app .

[3.6] Run Docker container

docker run -it employee-list-app

[3.7] Stop container

docker ps -a

docker stop <container_id>

[3.8] Remove container

docker rm <container_id>

[3.9] Remove image

docker rmi employee-list-app

.


.

0
Subscribe to my newsletter

Read articles from Mohamad Mahmood directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mohamad Mahmood
Mohamad Mahmood

Mohamad's interest is in Programming (Mobile, Web, Database and Machine Learning). He studies at the Center For Artificial Intelligence Technology (CAIT), Universiti Kebangsaan Malaysia (UKM).