smeeb
smeeb

Reputation: 29537

Dockerized Java app dies with no error message but runs fine standalone

Please note: I know this questions is very similar to this one however you'll note that the solution in that case was to EXPOSE the port, which I am already doing. Hence although this questions sounds similar, I think its simply a different problem altogether with similar symptoms as the other question.


Docker Version 17.12.0-ce-mac49 (21995) here. I am experimenting with Docker for the first time and have built my first Docker image. My Dockerfile is:

FROM openjdk:8

RUN mkdir /opt/myapp

ADD build/libs/myapp.jar /opt/myapp
ADD application.yml /opt/myapp
ADD logback.groovy /opt/myapp
WORKDIR /opt/myapp
EXPOSE 9200
ENTRYPOINT java -Dspring.config=. -jar myapp.jar

I build it via:

docker build -t myapp .

Everything succeeds. I then tag it as if I'm going to push it to Quay:

docker tag <imageId> quay.io/myregistry/myapp:0.1.0-SNAPSHOT

However before I publish to Quay I want to run it locally to make sure it works:

docker run -it -p 9200:9200 -d --env-file /Users/myuser/myapp-local.env --name myapp myapp

When I run this I get an indication that the container is running, and I can even see it for a few seconds via docker ps:

CONTAINER ID        IMAGE               COMMAND                  CREATED                  STATUS              PORTS                    NAMES
f3fa8f7a4288        myapp               "/bin/sh -c 'java -D…"   Less than a second ago   Up 7 seconds        0.0.0.0:9200->9200/tcp   myapp

However after a few seconds it stops running and disappears from docker ps altogether:

CONTAINER ID        IMAGE               COMMAND                  CREATED                  STATUS              PORTS                    NAMES

Furthermore I'm not able to SSH into the container:

docker exec -it f3fa8f7a4288 bash
Error: No such container: f3fa8f7a4288

...or see any logs/console output.

When I run myapp.jar outside of Docker (as a typical Spring Boot app, it starts up and runs beautifully without exceptions). How can I troubleshoot what is going on?

Upvotes: 2

Views: 1438

Answers (2)

Matt
Matt

Reputation: 74761

The docker logs command will show you the output a container is generating when you run it detached (with -d). This is likely to include the error message.

docker logs --tail 50 --follow --timestamps container

You can run the image in the foreground without the -d to see the output like when you run myapp.jar outside of Docker.

docker run my/image

So in this specific case:

docker run -it -p 9200:9200 --env-file /Users/myuser/myapp-local.env --name myapp myapp

Upvotes: 1

Frelling
Frelling

Reputation: 3507

If I am not mistaken, the issue you are experiencing is because you are using the shell form of ENTRYPOINT. Change it to use the exec version, as follows:

ENTRYPOINT ["java", "-Dspring.config=.", "-jar", "myapp.jar"]

The shell form will launch Java as a separate process just like a shell command. This causes PID 1 to return making Docker believe the container is finished. Using the exec form, the Java process replaces PID 1 and the container will continue running.

Upvotes: 1

Related Questions