Wild Tang
Wild Tang

Reputation: 321

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 :)

Upvotes: 4

Views: 2390

Answers (2)

BIllyLiu
BIllyLiu

Reputation: 36

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

Upvotes: 0

David MacCallum
David MacCallum

Reputation: 321

Why not log to syslog?

Your ansible playbook would look like this:

  - name: start container
docker_container:
    name: "{{docker_container}}"
    image: "{{docker_image}}"
    volumes:
      - "/etc/timezone:/etc/timezone"
      - "/etc/localtime:/etc/localtime"
    published_ports:
      - "{{published_ports}}"
    recreate: yes
    log_driver: syslog
    log_options:
      tag: "{{docker_container}}"
      syslog-facility: local4

And your syslog config could look like this (/etc/rsyslog.d/22-docker.conf):

local4.*         /var/log/docker.log

In my case (Ubuntu 16.04) syslog is maintaining the log file size automatically.

Upvotes: 0

Related Questions