36

I am trying to specify the max file size for json-file in docker-compose.yml, like this,

log-opt:
  max-size=50m

but when I tried to docker-compose up, it threw me an error,

ERROR: In file './docker-compose.yml', service 'log-opt' must be a mapping not a string.

How to fix it?

ps. I am using docker 1.11.2

Brian Burns
  • 20,575
  • 8
  • 83
  • 77
daiyue
  • 7,196
  • 25
  • 82
  • 149

2 Answers2

70

Your yaml syntax isn't quite correct. The documentation says it should be options not log-opt (https://docs.docker.com/compose/compose-file/#logging). Try this?

services:
    service_name:        
        logging:
            driver: "json-file"
            options:
                max-size: "50m"

You should define logging section in each one of your services not directly in root of docker-compose.

Marat Mkhitaryan
  • 844
  • 2
  • 11
  • 25
Matthew
  • 10,361
  • 5
  • 42
  • 54
  • Hi, tried that and got a different error, `ERROR: In file './docker-compose.yml', service 'options' must be a mapping not an array.` – daiyue Aug 22 '16 at 13:12
  • got an error, `ERROR: The Compose file './docker-compose.yml' is invalid because: Unsupported config option for options: 'max-size' ` – daiyue Aug 22 '16 at 13:24
  • Are you using docker-compose version 2? Did you set the driver to json-file? I've updated my example to be more complete. – Matthew Aug 22 '16 at 13:31
  • I am using `docker 1.11.2`, I have updated the op. I think `json-file` is the default `logging driver`. – daiyue Aug 22 '16 at 13:46
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/121538/discussion-between-matthew-and-daiyue). – Matthew Aug 22 '16 at 14:17
  • 1
    For a running container, after editing compose file, recreate the container: `docker-compose -f docker-compose.yml up -d --force-recreate $SVCNAME` – Gh0sT Mar 25 '20 at 11:28
  • you want to also limit the file count - https://stackoverflow.com/a/58571932 – Dave Ankin Jul 14 '22 at 04:30
  • 1
    Up to date docs reference for these parameters: https://docs.docker.com/config/containers/logging/json-file/ – M1ke Mar 27 '23 at 14:24
15

max-size store log files until they reach a max-size of VALUE (eg: "2048m").

Example docker-compose file config with max-size.

version: '3.2'
services:
  logstash:
    image: docker.elastic.co/logstash/logstash:7.8.0
    command:  --config.reload.automatic  
    user: savio:savio
    restart: unless-stopped
    logging:
      driver: "json-file"
      options:
        max-size: "2048m"
    ports:
      - "9600:9600" 

If you are making a change for an already running container, you need to stop and remove the container and start again.

check log settings

]# docker container inspect -f '{{.HostConfig.LogConfig}}' <ContainerName>

For more detail: link

Savio Mathew
  • 719
  • 1
  • 7
  • 14