docker-compose json 日志记录驱动程序标签/环境

nal*_*lin 6 logging json attr docker-compose

我一直在尝试以以下格式获取日志:

{"log":"Using CATALINA_BASE:   /opt/apache-tomcat-
7.0.76\r\n","stream":"stdout","time":"2017-04-
19T04:28:33.608418994Z","attrs":
{"production_status":"testing","os":"ubuntu"}}
Run Code Online (Sandbox Code Playgroud)

我正在使用 docker-compose.yml :

version: '2.1'
services:
  web:
    image: hello-world/web:latest
    container_name: api
    ports:
      - "80:8080"
logging:
  driver: "json-file"
  options:
    max-size: 10m
    max-file: "3"
    labels: testing
    env: ubuntu
Run Code Online (Sandbox Code Playgroud)

但是我没有在日志中得到“attrs”键。我究竟做错了什么?

smy*_*hie 10

Based on my testing, there are 2 pieces to get the labels/environment variables show up in the logs.

  1. Specify which labels/environment variables to display in the logs
  2. Set those labels/environment variables on the container

So, to get what you want, you need to set the docker-compose.yml to:

version: '2.1'
services:
  web:
    image: hello-world/web:latest
    container_name: api
    ports:
      - "80:8080"
    logging:
      driver: "json-file"
      options:
        max-size: 10m
        max-file: "3"
        labels: "production_status"
        env: "os"
    labels:
      production_status: "testing"
    environment:
      - os=ubuntu
Run Code Online (Sandbox Code Playgroud)