Lets say i have a spring boot Jar file, and i want to run it in directly. We can run it by using below command.
$ java -jar existing-app.jar
But somewhere in the future i want to containerized them so that we can run it anywhere have to worry about infrastructure dependencies. For that purpose I need to have a Dockerfile, copy, and run my jar file inside it.
FROM openjdk:11.0.7-jre-slim-buster
LABEL base-image="openjdk:11.0.7-jre-slim-buster" \
java-version="11.0.7" \
purpose="Hello World with Java and Dockerfile"
MAINTAINER Muhammad Edwin < edwin at redhat dot com >
# set working directory at /deployments
WORKDIR /deployments
# copy my jar file
COPY existing-app.jar app.jar
# gives uid
USER 185
EXPOSE 8080
# run it
CMD ["java", "-jar","app.jar"]
I can build the container by running below command,
$ docker build -t hello-world-snowdrop .
And run it.
$ docker run -p 8080:8080 hello-world-snowdrop
In order to have above commands runs well, we need to have below structure in our folder.
$ tree . +--- Dockerfile +--- existing-app.jar
The same concept we can use when we want to deploy our app into Openshift. The only difference is the docker build process is being done in Openshift.
$ oc new-build --strategy docker --binary \ --docker-image openjdk:11.0.7-jre-slim-buster \ --name hello-world-snowdrop $ oc start-build hello-world-snowdrop \ --from-dir . --follow
And below are the commands we can use for deploy, run and expose our app into Openshift.
$ oc new-app hello-world-snowdrop $ oc create route edge --service hello-world-snowdrop
Dont forget to run above command within the same folder with our Dockerfile and jar files.
Hope it helps, and dont forget to have fun with Openshift 4.