Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker-compose json logging driver labels/env

I have been trying to get logs in format:

{"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"}}

I am using 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

But I do not get "attrs" key in logs. What am I doing wrong?

like image 567
nalin Avatar asked Apr 19 '17 04:04

nalin


People also ask

How do I change the Docker logging driver?

When you start a container, you can configure it to use a different logging driver than the Docker daemon's default, using the --log-driver flag. If the logging driver has configurable options, you can set them using one or more instances of the --log-opt <NAME>=<VALUE> flag.

How do I run a container that uses a json-file driver in Docker?

To use the json-file driver as the default logging driver, set the log-driver and log-opts keys to appropriate values in the daemon. json file, which is located in /etc/docker/ on Linux hosts or C:\ProgramData\docker\config\ on Windows Server. If the file does not exist, create it first.

Where are Docker json logs?

By default, Docker stores log files in a dedicated directory on the host using the json-file log driver. The log file directory is /var/lib/docker/containers/<container_id> on the host where the container is running. In the above output, we can see that the data is in JSON format.

What is Depends_on in Docker compose?

depends_on is a Docker Compose keyword to set the order in which services must start and stop. For example, suppose we want our web application, which we'll build as a web-app image, to start after our Postgres container.


1 Answers

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
like image 136
smythie Avatar answered Oct 04 '22 15:10

smythie