Sachin Malhotra
Sachin Malhotra

Reputation: 1251

Docker compose global level logging

I know for the latest docker compose, we can specify logging on a per service basis. eg :-

version: '2'

services:
  Sachin:
   image: hike/ubuntu:14.04
   volumes:
     - .:/testDocker
   working_dir: /testDocker
   logging:
    driver: "json-file"
    options:
     max-size: "25m"
     max-file: "2"
command: python -u test.py

I have a large number of containers in my compose file. I can specify the logging config for the docker daemon itself. I just wanted to know if it is possible to specify the logging configuration on the global level for the docker compose file. Something like this

version: '2'

services:
  Sachin:
   image: hike/ubuntu:14.04
   volumes:
     - .:/testDocker
   working_dir: /testDocker
logging:
 driver: "json-file"
 options:
  max-size: "25m"
  max-file: "2"
command: python -u test.py

Upvotes: 19

Views: 21273

Answers (2)

Thomas Decaux
Thomas Decaux

Reputation: 22691

You could also configure the Docker default for this, all your container will have the configuration (that you can override per container).

Here an example of solution with YAML anchor:

version: "2"

services:

  proxy:
    build: proxy
    image: kinoulink/proxy
    ports:
      - 80:80
      - 443:443
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
    container_name: ktv_manager_proxy
    environment:
        - HTTP_AUTH_PASSWORD=$KTV_MANAGER_PASSWORD
    logging: &logging
      driver: "awslogs"
      options:
      awslogs-region: eu-west-1
      awslogs-group: docker

  rancher:
    image: rancher/server:v1.1.3
    volumes:
      - rancher_mysql:/var/lib/mysql
      - rancher_cattle:/var/lib/cattle
    labels:
      ktv.infra.proxy.domain: 'rancher'
      ktv.infra.proxy.port: '8080'
    logging:
      <<: *logging

From v3.4 (as @tekHedd said), you can use "extension field" syntax:

version: "3.4"

x-logging: 
      &default-logging
      driver: "awslogs"
      options:
      awslogs-region: eu-west-1
      awslogs-group: docker

services:
proxy:
    build: proxy
    image: kinoulink/proxy
    ports:
      - 80:80
      - 443:443
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
    container_name: ktv_manager_proxy
    environment:
        - HTTP_AUTH_PASSWORD=$KTV_MANAGER_PASSWORD
    logging: *default-logging

  rancher:
    image: rancher/server:v1.1.3
    volumes:
      - rancher_mysql:/var/lib/mysql
      - rancher_cattle:/var/lib/cattle
    labels:
      ktv.infra.proxy.domain: 'rancher'
      ktv.infra.proxy.port: '8080'
    logging: *default-logging

Upvotes: 31

dnephin
dnephin

Reputation: 28160

It is not possible at this time. You can use YAML anchors to inject the same struct into each service instead of repeating it.

Upvotes: 2

Related Questions