Low Orbit Flux Logo 2 F

Docker How To Run Java Jar

Running a Java jar file inside a Docker container is easier than you might think. We are going to show you how you can define an image and run a container based on that image using a jar file as the main application or command.

We’re actually kind of cheating by using a base image that already has Java installed.

You can define an image using a Dockerfile like this:


FROM anapsix/alpine-java
COPY test1.jar /data/test1.jar
CMD ["java","-jar","/data/test1.jar"]

Build it like this:


docker build -t my-image1 .

Run it like this:


docker run -d my-image1

Entire Alternate Example

Create a project dir:



mkdir test1
cd test1

Create a docker file:

vi Dockerfile
FROM anapsix/alpine-java MAINTAINER userone COPY my-project.jar /home/my-project.jar RUN java -jar /home/my-project.jar

Build an image:



docker build -t mytestimage .

Run your image as a container:



docker run -i -t mytestimage java -jar /home/my-project.jar