# Using a Cron Job to Run Rake Tasks Inside Your Docker Container

DevFeed: [Using a Cron Job to Run Rake Tasks Inside Your Docker Container](<https://devfeed.tech/articles/using-a-cron-job-to-run-rake-tasks-inside-your-docker-container-28157.md>)

Original publisher: [Read original article](<http://fuzzyblog.io/blog/docker/2019/11/29/using-a-cron-job-to-run-rake-tasks-inside-your-docker-container.html>)

Author: Fuzzygroup

Published: 2019-11-29T00:00:00Z

Content type: tutorial

Language: en

Sources: [Scott Johnson](<https://devfeed.tech/sources/scott-johnson.md>)

Topics: [Docker](<https://devfeed.tech/topics/docker.md>), [container](<https://devfeed.tech/topics/container.md>), [Bash](<https://devfeed.tech/topics/bash.md>), [Job](<https://devfeed.tech/topics/job.md>)

Tags: [bash](<https://devfeed.tech/tags/bash.md>), [container](<https://devfeed.tech/tags/container.md>), [docker](<https://devfeed.tech/tags/docker.md>), [job](<https://devfeed.tech/tags/job.md>), [rails](<https://devfeed.tech/tags/rails.md>), [rake](<https://devfeed.tech/tags/rake.md>), [run](<https://devfeed.tech/tags/run.md>), [scheduling](<https://devfeed.tech/tags/scheduling.md>), [shell](<https://devfeed.tech/tags/shell.md>), [shell-script](<https://devfeed.tech/tags/shell-script.md>)

## AI overview

This tutorial explains how to use an OS-level cron job to run a Rake task inside a Docker container. It shows how to execute a shell in the container, run the task with Docker, and use a shell script to obtain the container hash dynamically because the hash changes when the container restarts.

## Source excerpt

Despite cron being an age old *nix technology, it alway seems like scheduling tasks is an annoying thing. Note: Cron is the standard *nix technology for running tasks on a recurring basis like "every monday at 5 am execute this program". We are now doing things like running tasks within containers and that raises the question of where does cron exist: within the container? at the OS level? what about multiple instances of the container across different machines? I'm not going to try and address the bigger picture questions here like multiple instances but I can show you how to use cron at the OS level to drive a rake task within a container. How Do You Run Something Within in a Container The first thing to understand is that you can easily execute a shell with in a container, from the outside, with this syntax: docker exec -it CONTAINER_HASH /bin/bash The CONTAINER_HASH is the docker equivalent of a *nix pid (process identifier). How to Run a Rake Task And if you can run a shell like /bin/bash within a container from the outside then, drum roll please, you can easily run anything from outside including a Rake task. And if you have that then you can easily schedule it. Here's an example: docker exec -it 8d76da4ab481 bundle exec rake data_source:update_or_initial_load --trace The 8d76da4ab481, shown above, is an example of the CONTAINER_HASH. But Docker Container Hashes Change... If you think about this a bit there is one real problem - that damn CONTAINER_HASH isn't consistent. It changes every time the container is restarted. The trick to making a cron job that runs your rake task is to use a shell script which pulls the container hash dynamically. And to figure that you we are going to start with my dockerbash blog post. Here's the shell script from the dockerbash post: #!/bin/bash if [ -z $1 ]; then echo "You need to specify the name of the container you want to get into like:" echo "dockerbash police" else pid=`docker ps | grep $1 | awk '{print $1}'` docker exec -i