Categories
DevOps

How to alter task history retention inside Docker Swarm cluster

Alter task history retention inside the Docker Swarm cluster to ensure that there are no stopped containers.

By default, task history retention is set to 5.

$ docker node ps --filter "name=blog_staging" swarm-cerberus
ID                  NAME                 IMAGE                                                NODE                DESIRED STATE       CURRENT STATE             ERROR               PORTS
ormho76nb30u        blog_staging.2       registry.example.com/websites/blog:staging           swarm-cerberus      Running             Running 14 minutes ago                        
ttrmtuh5xpv4         \_ blog_staging.2   registry.example.com/websites/blog:staging           swarm-cerberus      Shutdown            Shutdown 16 minutes ago                       
p2of726hvvbg         \_ blog_staging.2   registry.example.com/websites/blog:staging           swarm-cerberus      Shutdown            Shutdown 15 hours ago                         
nvk4xr78td9m         \_ blog_staging.2   registry.example.com/websites/blog:staging           swarm-cerberus      Shutdown            Shutdown 38 hours ago                         
a0n2lcmhjx6f         \_ blog_staging.2   registry.example.com/websites/blog:staging           swarm-cerberus      Shutdown            Shutdown 38 hours ago       

Task history retention can be changed globally.

$ docker swarm update --help
Usage:	docker swarm update [OPTIONS]
Update the swarm
Options:
      --autolock                        Change manager autolocking setting (true|false)
      --cert-expiry duration            Validity period for node certificates (ns|us|ms|s|m|h) (default 2160h0m0s)
      --dispatcher-heartbeat duration   Dispatcher heartbeat period (ns|us|ms|s|m|h) (default 5s)
      --external-ca external-ca         Specifications of one or more certificate signing endpoints
      --max-snapshots uint              Number of additional Raft snapshots to retain
      --snapshot-interval uint          Number of log entries between Raft snapshots (default 10000)
      --task-history-limit int          Task history retention limit (default 5)

Set task history retention to 0 to disable it.

This change will not be applied immediately. You need to update the service to see the difference.
$ docker swarm update --task-history-limit 0
Swarm updated.

Update service to reapply task history retention.

$ docker service update --with-registry-auth --image registry.example.com/websites/blog:staging blog_staging | grep ^verify:\ Service"
verify: Service converged

There are no stopped containers.

$ docker node ps --filter "name=blog_staging" swarm-cerberus
ID                  NAME                IMAGE                                                NODE                DESIRED STATE       CURRENT STATE              ERROR               PORTS
lqblu9mgjhxl        blog_staging.2      registry.example.com/websites/blog:staging           swarm-cerberus      Running             Preparing 49 seconds ago

Additional information

You can set this option during swarm initialization.

$ docker swarm init --help
Usage:	docker swarm init [OPTIONS]
Initialize a swarm
Options:
      --advertise-addr string                  Advertised address (format: <ip|interface>[:port])
      --autolock                               Enable manager autolocking (requiring an unlock key to start a stopped manager)
      --availability string                    Availability of the node ("active"|"pause"|"drain") (default "active")
      --cert-expiry duration                   Validity period for node certificates (ns|us|ms|s|m|h) (default 2160h0m0s)
      --data-path-addr string                  Address or interface to use for data path traffic (format: <ip|interface>)
      --data-path-port uint32                  Port number to use for data path traffic (1024 - 49151). If no value is set or is set to 0, the default port (4789) is used.
      --default-addr-pool ipNetSlice           default address pool in CIDR format (default [])
      --default-addr-pool-mask-length uint32   default address pool subnet mask length (default 24)
      --dispatcher-heartbeat duration          Dispatcher heartbeat period (ns|us|ms|s|m|h) (default 5s)
      --external-ca external-ca                Specifications of one or more certificate signing endpoints
      --force-new-cluster                      Force create a new cluster from current state
      --listen-addr node-addr                  Listen address (format: <ip|interface>[:port]) (default 0.0.0.0:2377)
      --max-snapshots uint                     Number of additional Raft snapshots to retain
      --snapshot-interval uint                 Number of log entries between Raft snapshots (default 10000)
      --task-history-limit int                 Task history retention limit (default 5)