0

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?

hc0re
  • 1,806
  • 2
  • 26
  • 61
  • You docker container execute the command `/bin/sh` and exit. You should have a process running to prevent it from the exit – Serge Apr 24 '19 at 07:21
  • Because you use ENTRYPOINT [ "/bin/sh" ] , which meas a null shell , you does not execute generator.jar – howie Apr 24 '19 at 07:21
  • 2
    Possible duplicate of [Why docker container exits immediately](https://stackoverflow.com/questions/28212380/why-docker-container-exits-immediately) – leopal Apr 24 '19 at 07:24

2 Answers2

1

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"]
vivekyad4v
  • 13,321
  • 4
  • 55
  • 63
0

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"]
shaped
  • 306
  • 2
  • 4