Robbo_UK Robbo_UK - 1 year ago 85
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"?>
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${}}/}spring.log}"/>
<include resource="org/springframework/boot/logging/logback/file-appender.xml" />
<root level="INFO">
<appender-ref ref="FILE" />

Answer Source

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


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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download