"Creating a Docker Image for Java Applications: Step-by-Step Guide"

  1. Create an EC2 Instance and Connect:

    • Go to the AWS Management Console.

    • Launch a new EC2 instance, choosing your preferred Amazon Machine Image (AMI) and instance type.

    • Configure the instance settings (security groups, key pair, etc.).

    • Launch the instance and connect to it using SSH.

  2. Install Docker:

    • Once connected to the EC2 instance, run the following commands to install Docker:
    sudo apt update
    sudo apt install docker.io
  1. Create a Project Directory:

    • Navigate to your home directory or another suitable location and create a directory for your Docker project:
    mkdir docker-project
    cd docker-project
  1. Grant User Permission to Docker:

    • Add your user to the 'docker' group to allow running Docker commands without using 'sudo':
    sudo usermod -aG docker $USER
  1. Reboot to Reflect Docker Permissions:

    • To apply the group changes, reboot the EC2 instance:
    sudo reboot
  1. Verify Docker Installation:

    • After the instance restarts and you've reconnected to it, verify that Docker is installed and accessible by running:
    docker ps

This should show the currently running containers.

  1. Create a java file HelloWorld.java

    public class HelloWorld {

    public static void main(String[] args)

    { System.out.println("Hello, World!");

    } }

  2. Now in order to run this java file we need compiler jdk create a docker file: creating a Docker base image that compiles and runs a Java program named HelloWorld.java. Here's a breakdown of each instruction:

     FROM openjdk:11
    
     # Set the working directory inside the container
     WORKDIR app/
    
     # Copy the Java source file to the container
     COPY Helloworld.java .
    
     # Compile the Java source file
     RUN javac Helloworld.java
    
     # Specify the command to run when the container starts
     CMD ["java", "HelloWorld"]
    
    • FROM openjdk:11: This sets the base image for your Docker image to OpenJDK 11, which is the Java Development Kit.

    • WORKDIR /app/: This instruction sets the working directory inside the container to /app/, where you'll be copying your Java source file and compiling it.

    • COPY HelloWorld.java .: This copies theHelloWorld.java file from the directory where your Dockerfile is located into the /app/ directory inside the container.

    • RUN javac HelloWorld.java: This command compiles the HelloWorld.java file inside the container.

    • CMD ["java", "HelloWorld"]: This is the command that will be executed when the container starts. It runs the compiled Java program with the class name HelloWorld. However, there is a minor issue with this line; it should be CMD ["java", "HelloWorld"] instead, as your class is named HelloWorld.

After building an image using this Dockerfile, running a container from that image will compile and execute the Java program inside the container, printing "Hello, World!" to the console.

  1. To run docker file to create image:

    docker build . -t java-app

    It will build a Docker image from the Dockerfile located in the current directory (.), and tag the image with the name java-app using the -t flag.

    Here's a breakdown of the command:

    • docker build: This is the command to build a Docker image.

    • .: This specifies the build context, which is the directory containing the Dockerfile and any files that are needed during the build process.

    • -t java-app: This specifies the name and optional tag of the image you're creating. In this case, the image will be tagged as java-app.

After successfully running this command, Docker will execute the instructions in the Dockerfile, creating a new Docker image based on those instructions. You'll be able to see the progress of the build process, including each step defined in the Dockerfile. Once the build is complete, you'll have an image named java-app that you can use to create containers.

  1. To run docker image to make container:

    docker run java-app