Skip to content

corvus-migratorius/docker-alpine-cron

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

docker-alpine-cron

A Linux Alpine-based image with dcron.

Based on https://github.com/xordiv/docker-alpine-cron project which has not been updated in a while.

Example

  1. Build the base image:
docker build -f alpine-cron.Dockerfile . -t corvus/alpine-cron:3.15.0
  1. Extend the base image:
FROM corvus/alpine-cron:3.15.0

RUN apk update && \
    apk add --no-cache mariadb-client
  1. Build the modified image:
docker build -f alpine-cron.Dockerfile . -t corvus/mariadb-backups-cron:3.15.0
  1. Set up a Docker Compose file
my_container:
  image: corvus/mariadb-backups-cron:3.15.0
  restart: unless-stopped
  hostname: backups-cron
  container_name: backups-cron
  volumes:
    - /srv/backups:/backups:rw
    - /srv/database/secrets:/run/secrets:ro
    - ./backup-db.sh:/backup-db.sh:ro
  environment:
    MYSQL_HOSTNAME: my-database
    MYSQL_USERNAME_FILE: /run/secrets/user-name.txt
    MYSQL_PASSWORD_FILE: /run/secrets/user-password.txt
    CRON_STRINGS: "0 */2 * * * bash /backup-db.sh"  # notice this is UTC time
  1. Make sure that user-name.txt and user-password.txt files exist under /srv/database or whatever other secure location.

Tip: Creating the files with secrets, use printf or some other means of emitting strings without a trailing newline character.

Explanation

The example above uses the alpine-cron image to automate periodic backups of a MariaDB database. After we have extended the base image to include the toolset required for such backups, we configure the extended image. We mount a host volume containing database secrets and another host volume that the container will use to output database dumps. In addition, we mount a shell script that will:

  • read the mounted secrets,
  • connect to a remote database (possibly, residing in another container), and
  • invoke mysqldump, directing its output to /srv/backus:/backups

Finally, we set up a string that the dcron daemon will consume, directing the daemon to call our script once every 2 hours.

Note that you can supply multiple Cron strings, if you want to use this container to set up multiple Cron jobs. Make sure to use the '|'-syntax, because you want to preserve newline characters:

CRON_STRINGS: |
    0 */2 * * * bash /do-one-thing.sh
    0 */12 * * * bash /do-another-thing.sh

About

A Linux Alpine-based image with Crond

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published