Skip to content

Docker Cron: A lightweight, Alpine-based Docker image for running cron jobs and startup commands with automatic log rotation. Easily schedule tasks and manage logs in containerized environments.

License

Notifications You must be signed in to change notification settings

monlor/docker-cron

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Docker Cron

GitHub Packages GitHub release GitHub license GitHub issues GitHub stars

A lightweight Docker image based on Alpine Linux for running cron jobs and startup commands with automatic log rotation.

Features

  • Based on Alpine Linux 3.18
  • Runs cron jobs defined through environment variables
  • Executes startup commands
  • Automatic log rotation using logrotate
  • Real-time log output

Usage

Running the container

docker run -d \
-e CRON_JOB_EXAMPLE1="* * * * * echo 'Hello, World!'" \
-e CRON_JOB_EXAMPLE2="0 2 * * * echo 'Hourly task'" \
-e STARTUP_COMMAND_EXAMPLE1="echo 'Container started!'" \
-e STARTUP_COMMAND_EXAMPLE2="echo 'Initializing services...'" \
-e STARTUP_CONDITION="curl -s http://localhost:3000" \
monlor/docker-cron:main

Environment Variables

  • CRON_JOB_<name>: Define cron jobs. Format: "<schedule> <command>"
  • STARTUP_COMMAND_<name>: Define commands to run at container startup
  • STARTUP_CONDITION: Define a condition that must be met before starting services

Cron Jobs

Cron jobs are added to the container's crontab. The format is:

RON_JOB_<name>="<schedule> <command>"

Example:

CRON_JOB_HELLO="* * * * * echo 'Hello, World!'"
CRON_JOB_BACKUP="0 2 * * * curl -sSf https://example.com/api/backup"

Startup Commands

Startup commands are executed at container startup. The format is:

STARTUP_COMMAND_<name>="<command>"

Example:

STARTUP_COMMAND_INIT="echo 'Initializing...'"
STARTUP_COMMAND_CONFIG="curl -sSf https://example.com/api/config"

Startup Condition

You can set a startup condition that must be met before the container starts its services. This is useful for ensuring dependencies are ready. The format is:

STARTUP_CONDITION="<command>"

Example:

STARTUP_CONDITION="curl -sSf https://example.com/api/health &> /dev/null"

Log Management

  • Logs are stored in /var/log/cron/ and /var/log/
  • Log rotation is configured to:
    • Rotate files when they reach 10MB
    • Keep 5 rotated log files
    • Compress old log files

Building the Image

To build the Docker image:

docker build -t your-image-name .

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is open source and available under the MIT License.

About

Docker Cron: A lightweight, Alpine-based Docker image for running cron jobs and startup commands with automatic log rotation. Easily schedule tasks and manage logs in containerized environments.

Resources

License

Stars

Watchers

Forks

Packages