dockerloggingansiblemax-size

how to specify log-opt of docker run in ansible to limit the log size of a container?


I am using ansible playbook to deploy my app with docker in Jenkins, Recently I want to limit the log size of the app container, with "docker run" has "–log-opt max-size=xxxm" option, but How can I add the same function in ansible playbook?

I tried to set ansible playbook as below, but the test shows it not works.

  - name: start container
    docker_container:
      name: "{{docker_container}}"
      image: "{{docker_image}}"
      log_options:
        max-size: 256m
        max-file: 2
      volumes:
        - "/etc/timezone:/etc/timezone"
        - "/etc/localtime:/etc/localtime"
      published_ports:
        - "{{published_ports}}"
      recreate: yes

The location of log file of container which I want to limit the size is as the file below:

/var/lib/docker/containers/2fdcae9fb35f4954639a3ff254873f0ae8518f2ede011807/2fdcae9fb35f4954639a3ff254873f0ae8518f2ede0118072304077918deb20f-json.log

Thanks for your reply first :)


Solution

  • I don't know wether you see this warning

    [WARNING]: log_options is ignored when log_driver is not specified
    

    you can try set log_driver:json-file like below

    - name: Run docker container
      docker_container:
        name: "{{ docker_name }}"
        image: "test/test:{{ image_tag }}"
        log_driver: json-file
        log_options:
          max-size: 100m
        recreate: yes