Robbo_UK Robbo_UK - 12 days ago 7
Java Question

docker exec java app background attach logs

For a Java app running inside a docker container where should I output my logs to so that docker logs -f can read the log output of the java application after its been running for a while?

I execute the java app that is inside the active docker container with something like the following

docker exec -d container-name java -jar /path-to-jar/java-app-1.0-SNAPSHOT.jar


So far so good it runs my Spring Boot java app.

Would either of the following approaches work?

docker exec -d container-name java -jar /path-to-jar/java-app-1.0-SNAPSHOT.jar > /var/log/docker-id.log


Or setting the java log path settings to a particular destination?

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>
<include resource="org/springframework/boot/logging/logback/file-appender.xml" />
<root level="INFO">
<appender-ref ref="FILE" />
</root>
</configuration>

Answer

To run an application in docker and be able to view the logs then running it like the following will work.

Dockerfile

FROM base-image
COPY app.jar /opt/app.jar
CMD ["java","-jar","/opt/rate-upload-tool.jar"]

Build Docker Image

docker build -t java-app-image-name .

Run docker image in background

docker run --name app-container-name -d java-app-image-name

Following this you will be able to view logs of docker

docker logs -f app-container-name