###

alias hbin me

Run Crob Jobs In Docker Container

One container, One responibility, One Process

This blog introduce how to run cron jobs in Docker container.

  1. Install cron job

    Edit the Dockfile with your favorite editer, add follow snippet to install cron

     RUN apt-get update && apt-get install -y --no-install-recommends cron \
     && rm -r /var/lib/apt/lists/*
    
  2. Add a new task to docker-compose.yml file

     cron:
       depends_on:
         - db
         - redis
       build: .
       command: sh bin/start-cron.sh
       volumes:
         - .:/app
    
  3. Create start script /path-to-your-app/bin/start-cron.sh

     #!/bin/sh
     bundle exec whenever -w --set environment=development
     cron -f
    

You may got an exception like failed to load command: rake. The reason is the PATH environment not set properly.

The solution is trival, add the following two line of code to the beginning of `config/schedule.rb` file.

```
env :PATH, ENV['PATH']
env :BUNDLE_PATH, ENV['BUNDLE_PATH']
```

In case you need to set the bundle command explicitly, run the command with a --set variable,

bundle exec whenever -w --set 'environment=development&bundle_command=bundle exec'