'spring boot application in docker terminates immediately
Simple spring boot application trying to run in docker, however after docker run, exits immediately. Here is my Dockerfile
FROM anapsix/alpine-java:latest
VOLUME /tmp
RUN mkdir -p /opt/app
ADD app.jar /opt/app/app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/opt/app/app.jar"]
Docker run command
docker run -t -p 8080:8080 --name app -d app
In the container logs, the only output is the spring banner, and then the container terminates.
Am I missing something here.
Solution 1:[1]
I don't find any reason to use anapsix/alpine-java:latest
I recommand you to use the openjdk:8-jre-alpine
, light weight and less in size comparitively.
create a war files instead of jar.
use the following
Dockerfile
FROM openjdk:8-jre-alpine
ENV SPRING_OUTPUT_ANSI_ENABLED=ALWAYS \
SLEEP_TIME=0 \
JAVA_OPTS=""
# add directly the war
ADD *.war /app.war
EXPOSE 8080
CMD echo "The application will start in ${SLEEP_TIME}s..." && \
sleep ${SLEEP_TIME} && \
java ${JAVA_OPTS} -Djava.security.egd=file:/dev/./urandom -jar /app.war
Solution 2:[2]
In my case the reason container was failing to start was that the app couldn't connect to one of the external services and the reason I only saw Spring logo was that I had logback configured to work only under specific Spring profile and I overrode spring active profile with an environment variable but forgot to define that variable, so it was trying to start with an empty active profile.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Jinna Balu |
Solution 2 | Vitaly Chura |