Docker run but no output?
docker run
a container, but it displayed nothing, no output, no error? docker ps
shows no running container?
$ docker run -d -p 80:8080 -p 443:8443 -t markdownhtml:1.1
8eba06d44bf236109cf65b7b93842e9c898370cac1740aa2bab557a0fc8e52b9
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Solution
Most of the time, the container hit an error and exited automatically. We can use docker logs <container id>
to check the log file and find out what errors caused the exit.
1. docker ps -a
The default docker ps
show only running containers. To list out the exited container, we can use docker ps -a
.
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8eba06d44bf2 markdownhtml:1.1 "java -XX:+UnlockCom…" 5 hours ago Exited (1) 23 seconds ago unruffled_aryabhata
2f2318e3ffe5 8da9bbdb3a1e "java -jar app.jar" 8 hours ago Exited (143) About a minute ago focused_napier
3d1588519433 markdownhtml:0.1 "java -jar app.jar" 8 hours ago Exited (143) 3 hours ago gracious_haibt
a5df11a07a0e markdownhtml:0.1 "java -jar app.jar" 10 hours ago Exited (143) 3 hours ago stoic_bhaskara
// many containers...omitted
Find the most recent exited container; in this case, it is 8eba06d44bf2
.
2. docker logs
We can use docker logs
to check the log file of a container.
$ docker logs 8eba06d44bf2
Unrecognized VM option 'UnlockCommercialFeatures'
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
In this example, this Unrecognized VM option 'UnlockCommercialFeatures
caused the error exit during startup.
3. Fix the error
This Java option UnlockCommercialFeatures
is only available in OracleJDK, not other JDK, not even OpenJDK. The openjdk:8-jdk-alpine
image is using OpenJDK 8.
Review the Dockerfile
.
FROM openjdk:8-jdk-alpine
ARG JAR_FILE=target/markdown.jar
WORKDIR /opt/app
COPY ${JAR_FILE} app.jar
# -XX:+UnlockCommercialFeatures -XX:+FlightRecorder
ENTRYPOINT ["java","-XX:+UnlockCommercialFeatures","-XX:+FlightRecorder", "-jar","app.jar"]
However, the FlightRecorder
is open source in OpenJDK 11, try this openjdk11:alpine
.
#FROM openjdk:8-jdk-alpine
FROM adoptopenjdk/openjdk11:alpine
ARG JAR_FILE=target/markdown.jar
WORKDIR /opt/app
COPY ${JAR_FILE} app.jar
# OpenJDK 11 no need -XX:+UnlockCommercialFeatures -XX:+FlightRecorder
ENTRYPOINT ["java", "-jar","app.jar"]