I have a Dockerfile which looks like this:

FROM alpine:3.9

RUN apk add --update openjdk8
RUN mkdir /var/generator/
COPY generator.jar /var/generator
EXPOSE 8080
ENTRYPOINT [ "/bin/sh" ]

Dockerfile is inside generator/ folder. I am building it using:

docker build -t generator generator/

It builds successfully:

Successfully built 878e81f622cc
Successfully tagged generator:latest

but when I am trying to run this image with

docker run -d -p 8080:8080 generator

it dies immediately. docker logs gives no output.

What is wrong with my Dockerfile? Why is the container dying?

2 Answers

1
vivekyad4v On

Try to run the JAR. Currently, it just runs sh command and exits. Make it something as below to run the JAR in foreground -

FROM alpine:3.9

RUN apk add --update openjdk8
RUN mkdir /var/generator/
COPY generator.jar /var/generator
EXPOSE 8080
ENTRYPOINT ["java","-jar","/var/generator/generator.jar"]
0
shaped On

Beside your entrypoint is wrong (sh exits immediately) I would also recommend to start with an appropriate base image instead of starting with alpine and installing the openjdk package. Since you want to run a java application just use the JRE and not a full JDK and start the application as a foreground process.

Here's a minimal version which is also more efficient in disksize as the image will be smaller.

FROM openjdk:8-jre-alpine

COPY generator.jar /opt/generator.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","/opt/generator.jar"]