简体   繁体   中英

Spring boot docker mount shared logback config files

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM