Git Product home page Git Product logo

docker-alpine-cron's Introduction

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

docker-alpine-cron's People

Contributors

corvus-sapiens avatar

Watchers

 avatar

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.