A quick demo for Java Application container
To run this you must have a docker installed .
I'm going to use this on Ubuntu/Linux On windows subsystem.
1) You can create a directory for this demo using linux command
mkdir docker-demo-java
2) Change directory to new folder using cd command as
cd docker-demo-java
3) create a sample java file DemoJavaContainer.java and print a sample message as
"A sample docker java application demo"
class DemoJavaContainer{
public static void main(String ar[]) {
System.out.println("A sample docker java application demo");
}
}
4) Then write a sample docker file named Dockerfile
and write the below content
from java:8
COPY . /var/java
WORKDIR /var/java
RUN javac DemoJavaContainer.java
CMD ["java","DemoJavaContainer"]
5) save the file in the project directory
6) run command :
docker build -t sample-docker-java-app .
7) Above command will run the 5 steps mentioned in the docker file which are
a) build an image from java 8 version
b) copy the source to /var/java directory
c) change working directory to /var/java
d) compile it using standard java command javac DemoJavaContainer.java
e) run it using command java DemoJavaContainer
8) One the build is complete, it will create an image with
the name specified in tag( -t argument) "sample-docker-java-app" ,
you can re run the application using below command , it will print the output
docker run sample-docker-java-app
Comments
Post a Comment