0

I have a spring boot application with a micro services architecture. With something like 10 spring boot containers running.

The setup for loading the logback.xml that I am aiming to do is looks something like this. logback-xml

How do I mount and load the logback.xml on a spring boot embedded jar / container when I start the containers up.

My thoughts are something like this.

This will run the spring boot app ok.

docker run -d --name=app1 app_image

Something like this is what I am looking to implement load the logback.xml

docker run -d --name=app1 --volume=/home/host-path/log-back-conig-dir/:/container-app-dir/ app_image

docker run -d --name=app2 --volume=/home/host-path/log-back-conig-dir/:/container-app-dir/ app_image

docker run -d --name=app3 --volume=/home/host-path/log-back-conig-dir:/container-app-dir/

Further info

I am using the Dockerfile example taken from the official spring boot

spring.io/guides/gs/spring-boot-docker

Dockerfile

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
ADD ${JAR_FILE} app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

logback.xml looks like this

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <springProperty scope="context" name="springAppName" source="spring.application.name"/>
    <include resource="org/springframework/boot/logging/logback/base.xml" />

    <springProfile name="default,dev,staging">
        <logger name="guru.springframework.controllers" level="DEBUG" additivity="false">
            <appender-ref ref="CONSOLE" />
        </logger>
    </springProfile>

    <springProfile name="dev">
        <appender name="stash" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
                <level>info</level>
            </filter>
            <file>/varlog/log-tracing-demo/build/logs/${springAppName}.log</file>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <fileNamePattern>/var/log/log-tracing-demo/build/logs/${springAppName}.log.%d{yyyy-MM-dd}</fileNamePattern>
                <maxHistory>30</maxHistory>
            </rollingPolicy>
            <encoder class="net.logstash.logback.encoder.LogstashEncoder" >
                <includeContext>false</includeContext>
                <fieldNames>
                    <message>msg</message>
                </fieldNames>
                <customFields>{"application_name":"${springAppName}"}</customFields>
            </encoder>
        </appender>
        <root level="info">
            <appender-ref ref="CONSOLE" />
            <appender-ref ref="stash" />
        </root>
    </springProfile>
</configuration>

Short version of question is..

How do make multiple spring boot docker containers to mount and share the same single logback.xml file.

Robbo_UK
  • 11,351
  • 25
  • 81
  • 117
  • You can add the logback.xml to your Dockerfile and have all your apps use the same config file. However, Docker already produces logs. So it might be a good idea to log spring apps to STDOUT and use something like ELK stack to gather all your logs to one place. – Urosh T. Nov 24 '17 at 15:16
  • I want to keep the STDOUT as human readable and not json. The ones output to the file are in json format... json format means I do not have to write grok parsers and is less brittle to change. – Robbo_UK Nov 24 '17 at 15:18
  • Alternatively , You can create logback.xml as lib project and upload to private artifactory and add dependency on app project. – Panup Pong Nov 25 '17 at 04:00

1 Answers1

0

I would suggest to place logback.xml file in your images using Dockerfile command:

ADD logback.xml container-app-dir/

If you don't want to place your xml config in Dockerfile/image you can use named volume shared across all containers, here is nice info: Docker Compose - Share named volume between multiple containers So you can donwload this config for example in runtime (by making wget) and place into shared named volume.

Jakub Bujny
  • 4,400
  • 13
  • 27