Step 1: Launch an EC2 Instance
With the help of this: https://lalitakashyapblog.hashnode.dev/step-by-step-guide-to-setting-up-an-ec2-instance
Step 2: Install docker
With the help of this: https://lalitakashyapblog.hashnode.dev/from-zero-to-docker-a-beginners-introduction-to-containers
Step 3: Create directory and move into it
mkdir projects
cd projects/
Step 4: Create project name directory and move into it
mkdir java-app
cd java-app/
Step 5: Create file "Hello.java" using vim
vim Hello.java
Step 6: Write code in java file
import java.util.*;
public class Hello {
public static void main(String[] args) {
System.out.println("Hello");
Date currentdate =new Date ();
System.out.println("Today's date:" + currentdate);
}
}
Step 7: Now create docker file
#Get a base image with jdk installed
FROM openjdk:11
#Create a working directory to run a java app
WORKDIR /app
#Copy code from local machine to docker container
COPY Hello.java .
#Compile the code
RUN javac Hello.java
#Run the java app
CMD ["java", "Hello"]
Step 8: Build image
t- tag and "." use for build image in same directory
docker build -t java-app:latest .
Step 9: Check image created or not
docker images
Step 10: Create and run docker container
docker run java-app:latest
By following these steps, you should be able to successfully deploy your simple Java code using Docker.
Thank you ๐
Keep Learning..
ย