DEV Community

Aditya Kanekar
Aditya Kanekar

Posted on

Creating base docker image for hosting your Java 8 application

Creating clean image is very important from trusted sources. This article demonstrates how you can create a docker image for hosting your Java 8 application using Alpine Linux distro.

Dockerfile for Java8

FROM alpine

RUN apk update && \
    apk upgrade 
RUN apk add openjdk8=8.252.09-r0

ARG JAR_FILE=build/libs/*.jar
COPY ${JAR_FILE} app.jar
EXPOSE 8080
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
Enter fullscreen mode Exit fullscreen mode

The above Dockerfile creates image for hosting Java8 application using alpine distro. Lets go through each instruction one by one,

FROM alpine -
FROM instruction gets the baseimage for your application which in this case is alpine. You can also specify tags if you want a specific image of alpine e.g. alpine:3.9.

RUN apk update && apk upgrade
Runs update and upgrade instructions to update the sources and install any upgrade available.

RUN apk add openjdk8=8.252.09-r0
Installs specific version of JDK 8 on alpine.

ARG JAR_FILE=build/libs/*.jar
Argument for getting all jar files

COPY ${JAR_FILE} app.jar
Copy the jar file in the docker image

EXPOSE 8080
Exposes port 8080 on the docker image, if your application should be accessible on this port. (This is only applicable if your application should be accessible on certain port e.g. REST API)

ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

This instruction is responsible for running your application. So that's how you can create a lightweight clean image for your Java8 application. We have not touched on the safety aspect of this image, we will see how to secure this image in our next article.

Happy coding!

Top comments (0)