Scheduled jobs¶
The built-in job scheduler allows you to run containers/services defined in your docker compose files as scheduled jobs based on cron-like schedules or predefined intervals. This is useful for running periodic tasks such as backups, maintenance scripts, or any recurring workloads without needing an external scheduler.
Schedule formats¶
- Cron expressions without seconds (
minute hour day-of-month month day-of-week) - Predefined schedules like
@hourly,@daily,@weekly,@monthly,@yearly - Intervals like
@every <duration>(for example@every 30m)
Tip
Use an online cron expression generator like crontab.guru to create and validate cron expressions.
Schedule examples
Execution modes¶
The execution mode determines how scheduled jobs are run and managed by doco-cd and can be configured using the cd.doco.job.execution_mode label on the service.
restart¶
By default, scheduled jobs will be executed in restart mode, which means the service will be created on deployment
and then re-/started at the scheduled time without being removed after completion.
one_shot¶
Alternatively, you can configure scheduled jobs to run in one_shot mode, which means a new ephemeral container will
be created for each scheduled run and removed after completion.
Note
You won't be able to see the container or its logs after the job has completed, so make sure to configure appropriate logging (e.g., log to a persistent file or logging service like Loki) if you need to keep track of job runs and notifications if needed.
one_shot behavior in Docker Swarm
In Docker Swarm, one_shot does not modify the source service mode permanently.
Instead, doco-cd creates a temporary job service for each scheduled run, waits for completion,
and removes that temporary service afterwards.
This means the original service may still show replicated/global when inspected,
while each one-shot execution runs as a temporary replicated-job/global-job service.
See also Swarm deploy.mode configuration for how the original service's deploy mode affects the temporary job service's deploy mode in one-shot executions.
Behavior summary
cd.doco.job.execution_mode |
What doco-cd acts on | Service mode after run |
|---|---|---|
restart |
Existing service | Unchanged |
one_shot |
Temporary clone | Source unchanged |
Configuration¶
How to set service labels in a docker compose file
To set service labels in a docker compose file, include them in the labels section of your service definition:
Restart policy constraints
- Docker (Standalone): service
restartmust be unset orno - Docker Swarm: service
deploy.restart_policy.conditionmust be unset ornone
Use the following service labels to configure scheduled jobs:
| Label | Type | Description | Default |
|---|---|---|---|
cd.doco.job.enabled |
boolean | Enable scheduling for this service/container | false |
cd.doco.job.schedule |
string | Schedule format to use | |
cd.doco.job.execution_mode |
string | restart (default behavior) or one_shot (ephemeral execution) |
restart |
cd.doco.job.skip_running |
boolean | Do not run the job if a previous scheduled run is still active/running | false |
cd.doco.job.notify_on |
string | Notification behavior for scheduled runs: none, success, failure, all |
all |
cd.doco.job.swarm.replicas |
integer | Number of completions/concurrency for swarm one-shot jobs in replicated deploy mode |
1 |
Swarm deploy.mode¶
When using Docker Swarm, you can configure the deploy mode for scheduled jobs using the deploy.mode field in your docker compose file.
The following mapping applies to scheduled runs in one_shot mode:
- If the service uses
deploy.mode: global, the job run is created asglobal-job - If the service uses
deploy.mode: replicatedor does not specify a deploy mode, the job run is created asreplicated-jobwith the number of completions/concurrency determined by thecd.doco.job.swarm.replicaslabel.
Examples¶
Prune Docker system every hour on all swarm nodes using a global one-shot job service
services:
prune:
image: docker:latest
command: ["docker", "system", "prune", "-f"]
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
deploy:
mode: global
restart_policy:
condition: none
labels:
cd.doco.job.enabled: "true"
cd.doco.job.schedule: "@hourly"
cd.doco.job.execution_mode: "one_shot"
Timezone¶
Scheduled jobs are triggered based on the timezone of the doco-cd instance, which is determined by the TZ environment variable or defaults to UTC if not set.
You can find a list of all possible timezone values on wikipedia.
Daylight saving time (DST)¶
When DST changes occur in the configured timezone, scheduled jobs will adjust accordingly:
- If a scheduled time is skipped due to DST (e.g., clocks move forward), the job will not run at that time.
- If a scheduled time occurs twice due to DST (e.g., clocks move backward), the job will run at both occurrences of that time.