Code Monkey home page Code Monkey logo

docker-crontab's Introduction

docker-crontab

A simple wrapper over docker to all complex cron job to be run in other containers.

Supported tags and Dockerfile links

Why?

Yes, I'm aware of mcuadros/ofelia (>250MB when this was created), it was the main inspiration for this project. A great project, don't get me wrong. It was just missing certain key enterprise features I felt were required to support where docker is heading.

Features

  • Easy to read schedule syntax allowed.
  • Allows for comments, cause we all need friendly reminders of what update_script.sh actually does.
  • Start an image using image.
  • Run command in a container using container.
  • Run command on a instances of a scaled container using project.
  • Ability to trigger scripts in other containers on completion cron job using trigger.

Config file

The config file can be specifed in any of json, toml, or yaml, and can be defined as either an array or mapping (top-level keys will be ignored; can be useful for organizing commands)

  • name: Human readable name that will be used as the job filename. Will be converted into a slug. Optional.
  • comment: Comments to be included with crontab entry. Optional.
  • schedule: Crontab schedule syntax as described in https://en.wikipedia.org/wiki/Cron. Ex @hourly, @every 1h30m, * * * * *. Required.
  • command: Command to be run on in crontab container or docker container/image. Required.
  • image: Docker images name (ex library/alpine:3.5). Optional.
  • project: Docker Compose/Swarm project name. Optional, only applies when contain is included.
  • container: Full container name or container alias if project is set. Ignored if image is included. Optional.
  • dockerargs: Command line docker run/exec arguments for full control. Defaults to .
  • trigger: Array of docker-crontab subset objects. Subset includes: image,project,container,command,dockerargs
  • onstart: Run the command on crontab container start, set to true. Optional, defaults to falsey.

See config-samples for examples.

[{
 	"schedule":"@every 5m",
 	"command":"/usr/sbin/logrotate /etc/logrotate.conf"
 },{
 	"comment":"Regenerate Certificate then reload nginx",
 	"schedule":"43 6,18 * * *",
 	"command":"sh -c 'dehydrated --cron --out /etc/ssl --domain ${LE_DOMAIN} --challenge dns-01 --hook dehydrated-dns'",
 	"dockerargs":"--env-file /opt/crontab/env/letsencrypt.env -v webapp_nginx_tls_cert:/etc/ssl -v webapp_nginx_acme_challenge:/var/www/.well-known/acme-challenge",
 	"image":"willfarrell/letsencrypt",
 	"trigger":[{
 		"command":"sh -c '/etc/scripts/make_hpkp ${NGINX_DOMAIN} && /usr/sbin/nginx -t && /usr/sbin/nginx -s reload'",
 		"project":"conduit",
 		"container":"nginx"
 	}],
 	"onstart":true
 }]

How to use

Command Line

docker build -t crontab .
docker run -d \
    -v /var/run/docker.sock:/var/run/docker.sock:ro \
    -v ./env:/opt/env:ro \
    -v /path/to/config/dir:/opt/crontab:rw \
    -v /path/to/logs:/var/log/crontab:rw \
    crontab

Use with docker-compose

  1. Figure out which network name used for your docker-compose containers
    • use docker network ls to see existing networks
    • if your docker-compose.yml is in my_dir directory, you probably has network my_dir_default
    • otherwise read the docker-compose docs
  2. Add dockerargs to your docker-crontab config.json
    • use --network NETWORK_NAME to connect new container into docker-compose network
    • use --rm --name NAME to use named container
    • e.g. "dockerargs": "--network my_dir_default --rm --name my-best-cron-job"

Dockerfile

FROM willfarrell/crontab

COPY config.json ${HOME_DIR}/

Logrotate Dockerfile

FROM willfarrell/crontab

RUN apk add --no-cache logrotate
RUN echo "*/5 *	* * *  /usr/sbin/logrotate /etc/logrotate.conf" >> /etc/crontabs/logrotate
COPY logrotate.conf /etc/logrotate.conf

CMD ["crond", "-f"]

Logging - In Dev

All stdout is captured, formatted, and saved to /var/log/crontab/jobs.log. Set LOG_FILE to /dev/null to disable logging.

example: e6ced859-1563-493b-b1b1-5a190b29e938 2017-06-18T01:27:10+0000 [info] Start Cronjob **map-a-vol** map a volume

grok: CRONTABLOG %{DATA:request_id} %{TIMESTAMP_ISO8601:timestamp} \[%{LOGLEVEL:severity}\] %{GREEDYDATA:message}

TODO

  • Have ability to auto regenerate crontab on file change (signal HUP?)
  • Run commands on host machine (w/ --privileged?)
  • Write tests
  • Setup TravisCI

docker-crontab's People

Contributors

ddimick avatar iusmac avatar jamesfielder avatar kendokan avatar martinec avatar willfarrell avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

docker-crontab's Issues

Host specific config

Just started using this, got it working on one host (I don't use swarm). And tend to run all my docker infrastructure in git.
How might I implement a machine specific config.json

So host A might have job 1 and 2
Host B from have jobs 3 and 4

Is there a way of doing this out of the box, or have I created my own problem, and need to manage the different config files.
I guess I could map $PWD/crontab/config.hosta.json::/opt/crontab/config.json in the docker config.

[Suggestion] Replace wget with GNU-wget

I often use crontab with wget command to run my wordpress cronjobs wget -O /dev/null https://mysite.com/wp-cron.php , and my website is used SSL connection, nowadays many people are using SSL for their site to get a better security.

wget binary default is not supported SSL, I think it will better if we use GNU wget as replacement

Dockerfile

FROM library/docker:stable

ENV HOME_DIR=/opt/crontab
RUN apk add --no-cache --virtual .run-deps bash jq wget \
    && mkdir -p ${HOME_DIR}/jobs ${HOME_DIR}/projects \
    && adduser -S docker -D \
    && sed -i "s/999/99/" /etc/group

...

Cannot start container "addgroup: gid '999' in use"

After cloning this repo, edit config.json and run docker-compose up, I got this error

 docker-compose up   
Creating network "dockercrontab_default" with the default driver
Building crontab
Step 1/7 : FROM library/docker:stable
stable: Pulling from library/docker
ff3a5c916c92: Already exists
1a649ea86bca: Already exists
ce35f4d5f86a: Already exists
b6206661264b: Already exists
b8b71dba24d3: Already exists
3873004a68ee: Already exists
Digest: sha256:e9f04ffabbb1be195f25271116a52b2b560483abd4dc2ad54d6a90046fd1f397
Status: Downloaded newer image for docker:stable
 ---> 26c47276c586
Step 2/7 : ENV HOME_DIR=/opt/crontab
 ---> Running in b387e8e27cad
Removing intermediate container b387e8e27cad
 ---> 440cf51a26ba
Step 3/7 : RUN apk add --no-cache --virtual .run-deps bash jq     && mkdir -p ${HOME_DIR}/jobs ${HOME_DIR}/projects     && adduser -S docker -D
 ---> Running in 6c3c5bc01fab
fetch http://dl-cdn.alpinelinux.org/alpine/v3.7/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.7/community/x86_64/APKINDEX.tar.gz
(1/9) Installing pkgconf (1.3.10-r0)
(2/9) Installing ncurses-terminfo-base (6.0_p20171125-r0)
(3/9) Installing ncurses-terminfo (6.0_p20171125-r0)
(4/9) Installing ncurses-libs (6.0_p20171125-r0)
(5/9) Installing readline (7.0.003-r0)
(6/9) Installing bash (4.4.19-r1)
Executing bash-4.4.19-r1.post-install
(7/9) Installing oniguruma (6.6.1-r0)
(8/9) Installing jq (1.5-r4)
(9/9) Installing .run-deps (0)
Executing busybox-1.27.2-r7.trigger
OK: 15 MiB in 21 packages
Removing intermediate container 6c3c5bc01fab
 ---> b0faa62c08fc
Step 4/7 : COPY docker-entrypoint /
 ---> 9e1135a8f39e
Step 5/7 : ENTRYPOINT ["/docker-entrypoint"]
 ---> Running in ac8f89693299
Removing intermediate container ac8f89693299
 ---> 0e33a177185c
Step 6/7 : HEALTHCHECK --interval=5s --timeout=3s     CMD ps aux | grep '[c]rond' || exit 1
 ---> Running in 723d815aca58
Removing intermediate container 723d815aca58
 ---> 45ffdf0c69f4
Step 7/7 : CMD ["crond","-f"]
 ---> Running in ac500595a0b8
Removing intermediate container ac500595a0b8
 ---> 781ff2511daa
Successfully built 781ff2511daa
Successfully tagged dockercrontab_crontab:latest
WARNING: Image for service crontab was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating dockercrontab_crontab_1 ... done
Attaching to dockercrontab_crontab_1
crontab_1  | addgroup: gid '999' in use
dockercrontab_crontab_1 exited with code 1

Please help me

Default to current project when using docker-compose

We use this image alongside a couple others via docker-compose, and our config for every job currently has to include "project": "project-name".

Could project default to the name of the project that the Cron container is a part of?

I know the project name can be determined with something like the following from within the Cron container:

docker inspect -f '{{index .Config.Labels "com.docker.compose.project"}}' $HOSTNAME

Feature Request: run docker task as a different user

Problem

When executing a command inside a docker container, it is possible to specify a user. Some tasks require running with a specific user, e.g. the nextcloud fulltext search re-index job. For example:

docker exec -u www-data nextcloud php occ fulltextsearch:index

When I'm trying to add this job to docker-crontab like this to run it every night at 1:30h:

- name: nextcloud-fulltext
  command: php occ fulltextsearch:index
  comment: re-indexing nextcloud fulltext search for documents
  container: nextcloud
  schedule: '30  1 * * *'

I get an error from the nextcloud container that I have to run the command as user www-data.

Proposed solution

It would be great to be able to specify a user in config.yml which will be translated into a docker run -u user command in the generated *.sh file for the cronjob. I think this would solve this issue.

Workaround

Right now I'm doing a workaround by scheduling a docker command running within the docker-crontab container which then executes a command within the nextcloud container.

- name: nextcloud-fulltext
  command: docker exec -u www-data nextcloud php occ fulltextsearch:index
  comment: re-indexing nextcloud fulltext search for documents
  container: crontab
  schedule: '30  1 * * *'

This generates a docker exec command which then runs again a docker exec command but with specified user.

Logs not written to file

Hey! Love this project, exactly what I needed. Got it mostly set up now but facing an issue with the jobs.log not getting written to and some strange errors in the logs.

 jq: error (at <stdin>:1): Cannot iterate over null (null)

Docker Compose File

version: "3.7"

services:
  ftp_mount:
    image: ubuntu
    restart: always
    command: "sh -c 'while :; do sleep 1; done'"
    volumes:
      - "/mnt/disks/Unprotected_Data/FTP/reolink:/ftp"
      - "/mnt/disks/ntfs_backup_drive_500gb/BACKUPS_CRONTAB:/backups"
      - "/mnt/user/appdata/:/data:ro"
      - "/mnt/user/docker/:/docker:ro"
      - "${PWD}/scripts:/scripts:Z"
      - "${PWD}/logs:/var/log/crontab:rw"

  crontab:
    build: .
    restart: always
    volumes:
     - "/var/run/docker.sock:/var/run/docker.sock:ro"
     - "${PWD}/config/config.yaml:/opt/crontab/config.yaml:ro"
     - "${PWD}/logs:/var/log/crontab:rw"

config.yaml

purge_camera_files:
  command: echo Purging Camera Files
  comment: Removes camera footage until disk is 95% full
  image: alpine:3.5
  schedule: '@every 24h'
  dockerargs: --rm
  trigger:
    - command: /bin/bash /scripts/purge_files.sh /ftp
      project: crontab
      container: ftp_mount

Container Logs:

root@Tower:/mnt/user/docker/crontab# coml
Attaching to crontab_ftp_mount_1, crontab_crontab_1
crontab_1    | 2021-01-01T06:09:49.378156977Z jq: error (at <stdin>:1): Cannot iterate over null (null)
crontab_1    | 2021-01-01T06:09:49.416839397Z jq: error (at <stdin>:1): Cannot iterate over null (null)
crontab_1    | 2021-01-01T06:09:49.454677066Z jq: error (at <stdin>:1): Cannot iterate over null (null)
crontab_1    | 2021-01-01T06:09:49.566032634Z jq: error (at <stdin>:1): Cannot iterate over null (null)
crontab_1    | 2021-01-01T06:09:50.083452378Z ##### crontab generation complete #####
crontab_1    | 2021-01-01T06:09:50.083854115Z # Removes camera footage until disk is 95% full
crontab_1    | 2021-01-01T06:09:50.083868869Z */1 * * * * /bin/bash /opt/crontab/jobs/548a081a-c6ec-4db1-b270-82434eda680a.sh
crontab_1    | 2021-01-01T06:09:50.083920757Z ##### run commands with onstart #####
crontab_1    | 2021-01-01T06:09:50.083968699Z crond -f -d 6 -c /etc/crontabs
crontab_1    | 2021-01-01T06:09:50.084371880Z crond: crond (busybox 1.31.1) started, log level 6
crontab_1    | 2021-01-01T06:10:00.084930783Z crond: USER docker pid 135 cmd /bin/bash /opt/crontab/jobs/548a081a-c6ec-4db1-b270-82434eda680a.sh
crontab_1    | 2021-01-01T06:10:00.085997105Z Start Cronjob **548a081a-c6ec-4db1-b270-82434eda680a** Removes camera footage until disk is 95% full
crontab_1    | 2021-01-01T06:10:02.233314641Z Purging Camera Files
crontab_1    | 2021-01-01T06:10:04.056799272Z Current capacity at 91 %
crontab_1    | 2021-01-01T06:10:04.072716733Z End Cronjob **548a081a-c6ec-4db1-b270-82434eda680a** Removes camera footage until disk is 95% full

error starting on macOS

macOS version: 10.13.3
docker version: Docker version 18.03.1-ce
docker-compose.yml:

version: "3"

services:
  crontab:
    image: willfarrell/crontab
    container_name: cron
    volumes:
     - "/var/run/docker.sock:/var/run/docker.sock:ro"
     - "./config.json:/opt/crontab/config.json:rw"

docker logs cron gives

addgroup: gid '0' in use

on local machine

lrwxr-xr-x  1 root  daemon    58B Apr 26 21:55 /var/run/docker.sock -> /Users/...

When using the actual socket instead of the symlink the error is still there.

stat: can't stat '/var/run/docker.sock': No such file or directory

Time ago I've built an image based on willfarrell/crontab which basically adds a config.json file and openssh & rsync packages and works like a charm, until yesterday :(. Thats because I built another version of my extended image, with a minimal change in config.json, but this time, if the lastest ("the real latest") image is used, the container don't run giving the error:

stat: can't stat '/var/run/docker.sock': No such file or directory

The last "latest" image id, which works is aa079ab29c75

Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock in macOS

On macOS, the crontab container runs with the below error:

crontab-1 | crond: USER docker pid 242 cmd /bin/bash /opt/crontab/jobs/6ad06749-351e-459c-8f70-0291a9833c2e.sh
crontab-1 | Start Cronjob 6ad06749-351e-459c-8f70-0291a9833c2e test curl
crontab-1 | Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.40/containers/json: dial unix /var/run/docker.sock: connect: permission denied

This has to do with an issue mounting the docker socket onto the container, have you experienced this before?

Allow environment variables in options

First of, thanks for this great project. Much simpler and smaller in size than alternatives while keep configurability.

I'm interested in using this, but i have a somewhat blocking issue. Internally i have multiple environments of the same application. A development, testing and acceptation environment, and they're all running on the same server. Their project name is suffixed by the shorthand of their environment. So myapp_php_dev, myapp_php_acc`, etc.

Since i can't use variables in the project option, i have to make multiple config files just for every environment. This is of course a solution, but it creates duplicate and unnecessary files. It'd be nice if we could access variables in options so we can create dynamic options based on variables.

Their are obviously more solutions to this problem, like generating a config file before starting the docker project, but I'm just interested if this would be possible and would like to hear your/others about it.

Cron not working for scheduler in Laravel app (different container)

I pulled your image and tried to configure cron to run laravel scheduler. To test the setup, I am logging message through scheduler but it is not working.

Here is the configuration I did -

docker-compose.yml -


-Cron-job container
cron:
build:
context: ./crontab
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
networks:
- app-network

-PHP Service - laravel project container
app:
build:
context: .
dockerfile: Dockerfile
image: digitalocean.com/php
container_name: app
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
working_dir: /var/www
volumes:
- ./:/var/www
- ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
networks:
- app-network

-Docker Networks
networks:
app-network:
driver: bridge


Crontab (directory - cron configuration)

config.json -

[{ "comment":"cron with triggered commands", "schedule":"* * * * *", "command":"php /var/www/artisan schedule:run >> /dev/null 2>&1", "project":"paula", "container":"app" }]

Dockerfile -

`FROM willfarrell/crontab

COPY config.json ${HOME_DIR}/

Please help me with this problem.

Dash '-' vs. underscore '_' after project name in container naming

Great work thanks!

When I start a container with the v1.xx version of docker-compose:

docker-compose up -d <service>

the resulting container name will be: <project_name>_<service>_1

Job runs great.

When i use the go version or a v2.xx version:

docker compose up -d <service>

the resulting container name will be: <project_name>-<service>-1

See dash vs. underscore in container name.

I think docker-crontab might have the underscore (_) burnt in somewhere and tries to find the container to run the script in using that separator (/opt/crontab/projects/*.sh):

CONTAINERS=$(docker ps --format '{{.Names}}' | grep -E "^<project_name>_<container_name>.[0-9]+")

And the job won't run as would not find a running container.

docker-compose version 1.29.2, build unknown
Docker Compose version v2.15.1
Docker version 24.0.5, build ced0996

jobs don't start, seems crond not working

start command:
docker run --name cron -d
-v /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime:ro
-v $base_dir/conf/cron:/opt/crontab
-v /var/run/docker.sock:/var/run/docker.sock:ro
willfarrell/crontab
config.json:

[{
     "schedule":"@every 5m",
     "command":"/usr/sbin/logrotate /etc/logrotate.conf"
 },{
     "comment":"update index",
     "schedule":"10 17 * * * *",
     "command":"index_to_tsdb.py http://127.0.0.1:8085/dam/v1/tsdb/ 192.168.1.72",
     "dockerargs":"--rm --net container:dam",
     "image":"registry.cn-hangzhou.aliyuncs.com/jinchongzi/dam-scripts_0_6:20170920",
     "onstart":true
 },{
     "comment":"update stock",
     "schedule":"20 17 * * * *",
     "command":"mysql_to_tsdb.py http://127.0.0.1:8085/dam/v1/tsdb/ 192.168.1.72",
     "dockerargs":"--rm --net container:dam",
     "image":"registry.cn-hangzhou.aliyuncs.com/jinchongzi/dam-scripts_0_6:20170920",
     "onstart":true
},{
     "comment":"update tdate",
     "schedule":"00 20 * * * *",
     "command":"tdates_m2tsdb.py http://127.0.0.1:8085/dam/v1/tsdb/ 192.168.1.72",
     "dockerargs":"--rm --net container:dam",
     "image":"registry.cn-hangzhou.aliyuncs.com/jinchongzi/dam-scripts_0_6:20170920",
     "onstart":true
}]

jobs started when container start, because of the onstart settng. But never be start at all afterwards.

addgroup: gid '999' in use

Hi!
It seems the last change in handling the gid and fixing the issues #18 and #17 by getting the gid from the socket has somehow broken my setup.

With Ubuntu 16.04 kernel 4.4.0-130-generic
Docker version 18.03.1-ce, build 9ee9f40

since I pulled the latest version the crontab containers enter a restart loop.
It seems it fetches the correct gid, but another group (ping) already got 999.

Maybe the fix for #13 Commit b89e312 (adding && sed -i "s/999/99/" /etc/group) should be reapplied.

At least readding the line fixed the issue for me.

best regards and thanks for your great work!
Paul

Making a job without a command

How would I go about making a job that launches a container and then waits for the container to finish on its own? I have a container that will start, execute its tasks, and stop. Should the command parameter wait for the container to finish execution somehow?

Does not work with DOCKER_HOST

I'm using the configuration below, and it complains about docker.sock:
stat: can't stat '/var/run/docker.sock': No such file or directory

  crontab:
    image: willfarrell/crontab
    container_name: crontab
    restart: unless-stopped
    depends_on:
      - socket-proxy
    networks:
      - socket-proxy
    environment:
      - DOCKER_HOST=tcp://socket-proxy:2375
    volumes:
      - ./Volumes/crontab/logs:/var/log/crontab
      - ./Volumes/crontab/config:/opt/crontab

I suspect line 262 of docker-entrypoint, as this function will be called, regardless of whether we want to talk to the docker socket or when providing a DOCKER_HOST.

Massive BLOCK I/O

BLOCK I/O
13.1 GB / 91.1 MB

Related to DIND? Duplicating logs and volumes inside?

dial unix /var/run/docker.sock: connect: permission denied

Start Cronjob 6691da1d-2ed8-4afb-aca8-cec94c5bf680 update stock
docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.32/containers/create: dial unix /var/run/docker.sock: connect: permission denied.

Timezone Needed

Please add tz data in Dockerfile

ENV TZ="Zone/City"

RUN apk add --no-cache --virtual .run-deps gettext bash jq \
&& mkdir -p ${HOME_DIR}/jobs ${HOME_DIR}/projects \
&& adduser -S docker -D \
&& apk add -U tzdata \
&& cp /usr/share/zoneinfo/${TZ} /etc/localtime

When creating cron file we used to be mentally think in our own time zone, instead doing math calculating into UTC

Thank you

Start/Stop other containers

Is there a way to start or stop an other container in the docker-environment with this? If so, could someone give a basic example config please?

Where can I find the job in the list?

I tried to find the job with crontab -l but it only shows the run-parts commands.

Where can I find my job? It should be running but I don't know where I can check it.

Doesn't work in swarm on remote containers

This doesn't seem to work when executing commands on swarm task containers running on a different docker host than docker-crontab.

below assumes the container is running on the same host

CONTAINERS=$(docker ps --format '{{.Names}}' | grep -E "^[project]_[alias].[0-9]+")
for CONTAINER_NAME in $CONTAINERS; do
    docker exec ${CONTAINER_NAME} [command]
done

You could do something like below to get the container name from the docker host running the swarm task.

# deploy {ca,cert,key}.pem as secrets
function set_docker_env {
        export DOCKER_CERT_PATH=/run/secrets
        export DOCKER_HOST=tcp://$1:2376
        export DOCKER_TLS_VERIFY=1
}

function unset_docker_env {
        unset DOCKER_CERT_PATH
        unset DOCKER_HOST
        unset DOCKER_TLS_VERIFY
}

function container_host_from_docker {
        docker service ps --format '{{.Node}}' --filter 'desired-state=running' $1 | head -n 1
}

function container_name_from_docker {
        set_docker_env $1
        docker ps --format '{{.Names}}' | grep -E "^$2.[0-9]+" | head -n 1
}

CONTAINER_HOST=$( container_host_from_docker [project]_[alias] )
if [ ! -z "${CONTAINER_HOST// }" ]
then
    CONTAINER_NAME=$( container_name_from_docker $CONTAINER_HOST [project]_[alias] )
    docker exec ${CONTAINER_NAME} [command]
fi
unset_docker_env

timezone

Hi,

How can I set the timezone of the container?

License file for project missing

Hey, thanks for making this! Would be good to have a LICENSE.md file added to the project if possible. Have you thought about what license you might give this?

Configuration cleanup/jobs clean up

Is there a method for controlling how the config.working.json is updated, cleaned up after editing the config.json/yml?

And same with the jobs folder, this seems to hold a lot of script files that do not seem to get cleaned up. Unsure if there's a method for doing this.

Getting offered a new latest docker image each day

For a while now I have a script running that checks on my Docker host if there is any updated image waiting to be pulled.
This works fine for all docker images I use, a regularly get notified that there is a new image to roll out.

This is how I noticed your image tagged latest seems to be updated daily. Even though OS updates could justify such image update, I cant image there are actual changes each day.

How to prevent duplicate job ?

Hi,
i am concerning about how we prevent duplicate jobs ?
EG: @every 2m , we would call command " curl address", and this takes 3m to finish , how could job know previous job is running and wait for that to finish.
Normal case, i use "flock" , but i am not sure do we have that option in config.json?

Doesn't start on MacOs

Hi I pulled your project and folloewd your guideline but i have an error when i run docker-compose up -d --build.

here all log

Removing dockercron2_crontab_1
Building crontab
Step 1/7 : FROM library/docker:stable
---> 639de9917ae1
Step 2/7 : ENV HOME_DIR=/opt/crontab
---> Using cache
---> 626d82b7a0ef
Step 3/7 : RUN apk add --no-cache --virtual .run-deps bash jq && mkdir -p ${HOME_DIR}/jobs ${HOME_DIR}/projects && adduser -S docker -D && sed -i "s/999/99/" /etc/group
---> Using cache
---> ae9856510125
Step 4/7 : COPY docker-entrypoint /
---> Using cache
---> f7cc213f40e9
Step 5/7 : ENTRYPOINT ["/docker-entrypoint"]
---> Using cache
---> edfddc9d4c28
Step 6/7 : HEALTHCHECK --interval=5s --timeout=3s CMD ps aux | grep '[c]rond' || exit 1
---> Using cache
---> b9a08bdcb3a6
Step 7/7 : CMD ["crond","-f"]
---> Using cache
---> 3aa28dd68b79
Successfully built 3aa28dd68b79
Successfully tagged dockercron2_crontab:latest
dockercron2_myapp_1 is up-to-date
Recreating 3d0331691c7b_dockercron2_crontab_1 ... error

ERROR: for 3d0331691c7b_dockercron2_crontab_1 Cannot start service crontab: OCI runtime create failed: container_linux.go:344: starting container process caused "exec: "/docker-entrypoint": permission denied": unknown

ERROR: for crontab Cannot start service crontab: OCI runtime create failed: container_linux.go:344: starting container process caused "exec: "/docker-entrypoint": permission denied": unknown
ERROR: Encountered errors while bringing up the project.

I hope that can help you. Thanks for the support

Carmine

Add example to detect and fix sock timeouts.

ERROR: An HTTP request took too long to complete. Retry with --verbose to obtain debug information.
If you encounter this issue regularly because of slow network conditions, consider setting COMPOSE_HTTP_TIMEOUT to a higher value (current value: 60).

docker/compose#3633

Why the CPU usage will have a spike every few seconds?

When I am using the docker image, I notice that the CPU usage has a spike every few seconds. It will raise to 15 - 20%. There is only a cron job running every few hours. The cron job calls a 3rd party to update redis cache, which is not calculation intensive at all.

Why is it happening?

CVE-2016-4074 in jp

Hi @willfarrell,

first, thank you for the image. Before I start using the image I did a security scan with clair and figured out that your image on docker hub is affected by following CVE:

This issue could easily be fixed by triggering a new build and pushing it to docker hub.

If you need more information, please let me know.

Cheers,
Balthasar

No Cronjobs executed

After setting up a simple example no cronjob is executed. Only the onstart parameter triggers it to run once on startup.

config.json

[
  {
    "comment": "My Comment",
    "schedule": "1 * * * *",
    "command": "echo hello world >> /var/log/crontab/jobs.log",
    "onstart":true
  }
]

container output

crontab_1        | ##### crontab generation complete #####
crontab_1        | # My Comment
crontab_1        | */1 * * * * /bin/bash /opt/crontab/jobs/344a4d30-46cf-4a0d-9dfe-3a45b2ce4e86.sh
crontab_1        | ##### run commands with onstart #####
crontab_1        | /bin/bash /opt/crontab/jobs/344a4d30-46cf-4a0d-9dfe-3a45b2ce4e86.sh
crontab_1        | crond -f
crontab_1        | Start Cronjob **344a4d30-46cf-4a0d-9dfe-3a45b2ce4e86** My Comment
crontab_1        | End Cronjob **344a4d30-46cf-4a0d-9dfe-3a45b2ce4e86** My Comment

Name Config param does not allow whitespaces

The docs should be updated to reflect that the name param cannot contain whitespaces.

Example

config.json

[
  {
    "name" : "My Name",
    "comment": "My Comment",
    "schedule": "*/1 * * * *",
    "command": "echo hello world >> /var/log/crontab/jobs.log",
    "onstart":true
  }
]

container output

crontab_1        | /docker-entrypoint: line 159: ${HOME_DIR}/jobs/${SCRIPT_NAME}.sh: ambiguous redirect

Add running date into logs

Hi,
When I try to debug which script have been run at a precise date, I'm not able to do it because logs have no date.
Is that possible to re-enable it (at start end end) ? I saw some "date commands" commented into the entrypoint.

Best regards
Az

--cron: line 1: syntax error: unterminated quoted string

When running on Mac:

crontab_1     | ##### crontab generation complete #####
crontab_1     | */5 * * * * /usr/sbin/logrotate /etc/logrotate.conf
crontab_1     | # Regenerate Certificate then reload nginx
crontab_1     | 43 6,18 * * * docker run --env-file /opt/env/letsencrypt.env -v conduit_nginx_tls_cert:/etc/ssl -v conduit_nginx_acme_challenge:/var/www/.well-known/acme-challenge willfarrell/letsencrypt sh -c 'dehydrated --cron --out /etc/ssl --domain ${LE_DOMAIN} --challenge dns-01 --hook dehydrated-dns' && /bin/bash /opt/crontab/projects/9741f8dc-32fd-4d3b-a0be-1ab5961443a4.sh
crontab_1     | ##### run commands with onstart #####
crontab_1     | docker run --env-file /opt/env/letsencrypt.env -v conduit_nginx_tls_cert:/etc/ssl -v conduit_nginx_acme_challenge:/var/www/.well-known/acme-challenge willfarrell/letsencrypt sh -c 'dehydrated --cron --out /etc/ssl --domain ${LE_DOMAIN} --challenge dns-01 --hook dehydrated-dns' && /bin/bash /opt/crontab/projects/9741f8dc-32fd-4d3b-a0be-1ab5961443a4.sh
crontab_1     | crond -f
crontab_1     | sh -c 'dehydrated --cron --out /etc/ssl --domain ${LE_DOMAIN} --challenge dns-01 --hook dehydrated-dns' && /bin/bash /opt/crontab/projects/9741f8dc-32fd-4d3b-a0be-1ab5961443a4.sh
crontab_1     | --cron: line 1: syntax error: unterminated quoted string

Logs not written on startup execution

After setting up a simple example the output of my command is not written to /var/log/crontab/jobs.log.

config.json

[
  {
    "comment": "My Comment",
    "schedule": "1 * * * *",
    "command": "echo hello world",
    "onstart":true
  }
]

container output

crontab_1        | ##### crontab generation complete #####
crontab_1        | # My Comment
crontab_1        | */1 * * * * /bin/bash /opt/crontab/jobs/MyName.sh
crontab_1        | ##### run commands with onstart #####
crontab_1        | /bin/bash /opt/crontab/jobs/MyName.sh
crontab_1        | crond -f
crontab_1        | Start Cronjob **MyName** My Comment
crontab_1        | hello world
crontab_1        | End Cronjob **MyName** My Comment

jobs.log

... empty ...

crond: user docker: process already running

crond: user docker: process already running: /bin/bash /opt/crontab/jobs/d317c185-2cc4-41ad-99a3-d8f0c9de4fe4.sh

crontab generation complete
          • /bin/bash /opt/crontab/jobs/d317c185-2cc4-41ad-99a3-d8f0c9de4fe4.sh

how fix this ?

/etc/crontabs/docker not found

When I am trying to run the image to perform a crontab job on my other running container, I get that message:

NO CONFIG FILE FOUND

crontab generation complete

cat: can't open '/etc/crontabs/docker': No such file or directory

I am using this command to run the container:
docker run -v /var/run/docker.sock:/var/run/docker.sock:ro -v config.yaml:/opt/crontab:rw willfarrell/crontab

It's difficult to debug this because the documention lacks information about this file.

Crond since Version 1.0.0 not running

Hello,

when I pulled the latest container from dockerhub cron does not run in the container.

I have a working setup on version 0.6.0, to what I had to rollback to.

When I update the image to 1.0.0 or latest cron does not run in the container. The container does report, that it is healthy, but investigating inside the container if found that /docker-entrypoint autoheal is running. I could not find a crond process.
Also it seems the shellscripts to execute the commands and the crontab entries are not created.

Another point is, that the docker-entrypoint Script does not look anything like the docker-entrypoint on this repo.

Could it be, that the wrong image is pushed to the registry as docker-crontab?
Since I found no other changes here, except of the ones in .github and the documentation, it seems to me the only plausible explanation.

I just dug around and found your docker-autoheal. There in .github/workflows/github-build.yml is the tag willfarrell/crontab: in use.
I have no idea about github workflows but I guess this could be the problem.

Thanks for your great work!
Paul

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.