Skip to main content

Configuration Example 1 (Global Mode)

GitHub-logo.png logo (1).png Swarm-Cronjob.png

 

 

 

 

 

Before starting, you must have a swarm-cronjob instance up and running using docker.

Docker System Prune - Global Mode

This guide will help you set up a global Docker system prune task using Swarm Cronjob. This task will run on every node in your Docker Swarm cluster to remove unused data daily at 01:00 AM.

Step 1: Create a Docker Compose YAML

Create a Docker Compose file to define the system prune task:

version: "3.2"

services:
  prune-nodes:
    image: docker
    command: ["docker", "system", "prune", "-f"]
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock"
    deploy:
      mode: global
      labels:
        - "swarm.cronjob.enable=true"
        - "swarm.cronjob.schedule=0 0 1 * * *"
        - "swarm.cronjob.skip-running=false"
      restart_policy:
        condition: none

Explanation of Configuration:

  • mode: global: Ensures the task runs on every node in the cluster.
  • swarm.cronjob.schedule=0 0 1 * * *: Schedules the task to run daily at 01:00 AM.
  • swarm.cronjob.skip-running=false: Prevents skipping the task if it is already running.
  • restart_policy.condition=none: Ensures the task does not restart automatically after completion.

Step 2: Deploy the Stack

Deploy the global stack to your Docker Swarm cluster:

docker stack deploy -c prune-nodes.yml prune-nodes

Once deployed, the Docker system prune task will execute on every node in the swarm at the scheduled time.