Tuesday, April 2, 2024

Create Docker image with Java app running with Oracle JDK

 HOWTO


Create HelloWorld app


dave@dave:/git/devops-docker/docker-java$ vi HelloWorld.java
$ cat HelloWorld.java
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
dave@dave:/git/devops-docker/docker-java$  javac HelloWorld.java

dave@dave:/git/devops-docker/docker-java$ mkdir META-INF

$ cat META-INF/MANIFEST.MF 
Manifest-Version: 1.0
Created-By: 11.0.22 (Oracle Corporation)
Main-Class: HelloWorld
 

$ find
.
./HelloWorld.java
./HelloWorld.class
./META-INF
./META-INF/MANIFEST.MF
./HelloWorld.jar
./Dockerfile


dave@dave:/git/devops-docker/docker-java$ jar -cvfm HelloWorld.jar META-INF/MANIFEST.MF *.class
added manifest
adding: HelloWorld.class(in = 427) (out= 289)(deflated 32%)


Create Docker file
$ cat Dockerfile
FROM oracle/jdk:11-ol7
COPY *.jar /tmp
WORKDIR /tmp
ENTRYPOINT ["java","-jar","HelloWorld.jar"]

Build and run Docker image
$ docker build -t helloworld .
DEPRECATED: The legacy builder is deprecated and will be removed in a future release.
            Install the buildx component to build images with BuildKit:
            https://docs.docker.com/go/buildx/

Sending build context to Docker daemon  7.168kB
Step 1/4 : FROM oracle/jdk:11-ol7
 ---> fea3c351284b
Step 2/4 : COPY *.jar /tmp
 ---> b71b147ea17b
Step 3/4 : WORKDIR /tmp
 ---> Running in b9b9ef2965ac
Removing intermediate container b9b9ef2965ac
 ---> 2520b41db41a
Step 4/4 : ENTRYPOINT ["java","-jar","HelloWorld.jar"]
 ---> Running in 5f728643508e
Removing intermediate container 5f728643508e
 ---> 562422b29f37
Successfully built 562422b29f37
Successfully tagged helloworld:latest
dave@dave:/git/devops-docker/docker-java$ docker run --name helloworld helloworld
Hello, World!

No comments:

Post a Comment