Skip to content

Schedule docker-cloud services from within your stack.

Notifications You must be signed in to change notification settings

benoitquette/docker-cloud-cron

Repository files navigation

docker-cloud-cron

Docker Automated buil Docker Pulls Docker Stars

Schedule Docker Cloud services from within your stack.

The goal of this project is to facilitate the starting of services in a cron-like fashion. This will only work in Docker Cloud. This is inspired from the Rancher Cron Service project that already exists but for Rancher only.

Setup

Setting up an environment can be done in 2 steps:

  1. Create the cron service
  2. Schedule services to be started

Create the cron service

Adding a service based on the docker-cloud-cron image in one of your stack. You need to ensure that the service has a 'global' role.

cron:
  image: benoitquette/docker-cloud-cron
  restart: always
  roles:
    - global

If you do not wish to assign this type of role, you will have to manually pass the username and API key as environment variables. You can get an API key from your Docker Cloud account.

cron:
  image: benoitquette/docker-cloud-cron
  restart: always
  environment:
    - 'DOCKERCLOUD_USER=[your username here]'
    - 'DOCKERCLOUD_API_KEY=[your API key here]'

Schedule services to be started

This can be don by setting a label on a service of your stack. The label that will specify the schedule of the service in a crontab syntax. In the example below, the 'hello'service will be started every minute.

hello:
  image: hello-world
  labels:
    - 'docker-cloud-cron.schedule=1 * * * * *'

For now, the service can inspect all the services in your nodes. No restrictions on nodes or stacks.

I have only tested this on a single node setup. Please let me know how it behaves on multiple nodes.

Deploy to Docker Cloud

CRON syntax

Note: this section is copied from the underlying implemetation: node-cron.

This is a quick reference to cron syntax and also shows the options supported.

Allowed fields

┌────────────── second (optional)
│ ┌──────────── minute
│ │ ┌────────── hour
│ │ │ ┌──────── day of month
│ │ │ │ ┌────── month
│ │ │ │ │ ┌──── day of week
│ │ │ │ │ │
│ │ │ │ │ │
* * * * * *

Allowed values

field value
second 0-59
minute 0-59
hour 0-23
day of month 1-31
month 1-12 (or names)
day of week 0-7 (or names, 0 or 7 are sunday)

Using multiples values

You may use multiples values separated by comma:

  labels:
    - 'docker-cloud-cron.schedule=1,2,4,5 * * * *'

Using ranges

You may also define a range of values:

  labels:
    - 'docker-cloud-cron.schedule=1-5 * * * *'

Using step values

Step values can be used in conjunction with ranges, following a range with '/' and a number. e.g: 1-10/2 that is the same as 2,4,6,8,10. Steps are also permitted after an asterisk, so if you want to say “every two minutes”, just use */2.

  labels:
    - 'docker-cloud-cron.schedule=*/2 * * * *'

Using names

For month and week day you also may use names or short names. e.g:

  labels:
    - 'docker-cloud-cron.schedule=* * * January,September Sunday'

Or with short names:

  labels:
    - 'docker-cloud-cron.schedule=* * * Jan,Sep Sun'

Configuration

Several environment variables can be used to configure the service:

variable desc default
LABEL the name/key of the label used to specify the schedule docker-cloud-cron.schedule
API_LIMIT the limit used when calling the docker cloud API to introspect the services 100
DOCKERCLOUD_USER the docker cloud username NA
DOCKERCLOUD_API_KEY the docekr cloud API key NA

What's left

  • Linting
  • Unit tests
  • API key authentication
  • Test on multinode environment
  • Implement scopping: global vs stack
  • Audetect orchestration platform: Docker Cloud, Swarm, Rancher etc..
  • Implement for Rancher