How to run a cron job inside a docker container? - Stack Overflow (https://cdn.sstatic.net/Sites/stackoverflow/Img/favicon.ico?v=ec617d715196) (https://cdn.sstatic.net/Sites/stackoverflow/Img/apple-touch-icon.png?v=c78bd457575a) (https://cdn.sstatic.net/Sites/stackoverflow/Img/apple-touch-icon.png?v=c78bd457575a) (Stack Overflow) (/opensearch.xml) (https://stackoverflow.com/questions/37458287/how-to-run-a-cron-job-inside-a-docker-container) (https://cdn.sstatic.net/Shared/stacks.css?v=619122b796ae) (https://cdn.sstatic.net/Sites/stackoverflow/primary.css?v=02dcb5186b90) (Feed for question 'How to run a cron job inside a docker container?') (/feeds/question/37458287) (https://cdn.sstatic.net/Shared/Channels/channels.css?v=5981bb1a5bd7) (https://accounts.google.com/gsi/style) (site logo) Join Stack Overflow By clicking “Sign up”, you agree to our (/legal/terms-of-service/public) terms of service and acknowledge you have read our (/legal/privacy-policy) privacy policy . (e2a58aec89e0105273f31cf44dffd45d868462dc661de60a7d67e669a7370ceb) (1) (undefined) (2.0) Sign up with Google Sign up with GitHub OR (e2a58aec89e0105273f31cf44dffd45d868462dc661de60a7d67e669a7370ceb) (1) (undefined) Email Password (8+ characters (at least 1 letter & 1 number)) Sign up Already have an account? (/users/login) Log in (e2a58aec89e0105273f31cf44dffd45d868462dc661de60a7d67e669a7370ceb) () Skip to main content (https://stackoverflow.com) Stack Overflow (https://stackoverflow.co/) About Products (https://stackoverflow.co/teams/ai/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=top-nav-bar&utm_content=overflowai) OverflowAI (https://stackoverflow.co/teams/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=top-nav&utm_content=stack-overflow-for-teams) Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers (https://stackoverflow.co/advertising/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=top-nav&utm_content=stack-overflow-advertising) Advertising & Talent Reach devs & technologists worldwide about your product, service or employer brand (https://stackoverflow.co/teams/ai/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=top-nav&utm_content=overflow-ai) OverflowAI GenAI features for Teams (https://stackoverflow.co/api-solutions/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=top-nav&utm_content=overflow-api) OverflowAPI Train & fine-tune LLMs (https://stackoverflow.co/labs/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=top-nav&utm_content=labs) Labs The future of collective knowledge sharing (https://stackoverflow.co/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=top-nav&utm_content=about-the-company) About the company (https://stackoverflow.blog/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=top-nav&utm_content=blog) Visit the blog (Search…) () Loading… (https://stackoverflow.com) current community (https://stackoverflow.com) (Stack Overflow) Stack Overflow (https://stackoverflow.com/help) help (https://chat.stackoverflow.com/?tab=site&host=stackoverflow.com) chat (https://meta.stackoverflow.com) (Meta Stack Overflow) Meta Stack Overflow your communities (https://stackoverflow.com/users/signup?ssrc=site_switcher&returnurl=https%3a%2f%2fstackoverflow.com%2fquestions%2f37458287%2fhow-to-run-a-cron-job-inside-a-docker-container) Sign up or (https://stackoverflow.com/users/login?ssrc=site_switcher&returnurl=https%3a%2f%2fstackoverflow.com%2fquestions%2f37458287%2fhow-to-run-a-cron-job-inside-a-docker-container) log in to customize your list. (https://stackexchange.com/sites) more stack exchange communities (https://stackoverflow.blog) company blog (Click to show search) (https://stackoverflow.com/users/login?ssrc=head&returnurl=https%3a%2f%2fstackoverflow.com%2fquestions%2f37458287%2fhow-to-run-a-cron-job-inside-a-docker-container) Log in (https://stackoverflow.com/users/signup?ssrc=head&returnurl=https%3a%2f%2fstackoverflow.com%2fquestions%2f37458287%2fhow-to-run-a-cron-job-inside-a-docker-container) Sign up (/) Home (/questions) Questions (/tags) Tags (/users) Users (https://stackoverflow.com/jobs/companies?so_medium=stackoverflow&so_source=SiteNav) Companies Labs (/jobs?source=so-left-nav) Jobs (/beta/discussions) Discussions Collectives Communities for your favorite technologies. (/collectives-all) Explore all Collectives Teams Now available on Stack Overflow for Teams! AI features where you work: search, IDE, and chat. (https://stackoverflow.co/teams/ai/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=side-bar&utm_content=overflowai-learn-more) Learn more (https://stackoverflow.co/teams/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=side-bar&utm_content=explore-teams) Explore Teams Teams Ask questions, find answers and collaborate at work with Stack Overflow for Teams. (https://stackoverflow.co/teams/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=side-bar&utm_content=explore-teams-compact) Explore Teams Collectives™ on Stack Overflow Find centralized, trusted content and collaborate around the technologies you use most. (/collectives) Learn more about Collectives Teams Q&A for work Connect and share knowledge within a single location that is structured and easy to search. (https://stackoverflow.co/teams/) Learn more about Teams Get early access and see previews of new features. (https://stackoverflow.co/labs/) Learn more about Labs (https://cdn.sstatic.net/Sites/stackoverflow/Img/apple-touch-icon.png?v=c78bd457575a) (/questions/37458287/how-to-run-a-cron-job-inside-a-docker-container) How to run a cron job inside a docker container? (/questions/ask) Ask Question (2016-05-26 10:32:53Z) Asked 8 years, 3 months ago Modified (?lastactivity) (2024-07-04 07:33:23Z) 2 months ago (Viewed 761,541 times) Viewed 762k times This question shows research effort; it is useful and clear (70:3:31e,16:a47212d504d95109,10:1725308455,16:ac92ed3fe4a1f219,8:37458287,19be1b8abfa94bb8008453a60cb34cd77dfc7bd96e96917cd47cd9ffa099c786) 543 (This question does not show any research effort; it is unclear or not useful) (70:3:31e,16:d25d71d4cf99df0b,10:1725308455,16:fabfd21d6074f755,8:37458287,19cc4ae3c67d3e132af7d465a2416e91637d768c61a00435fd61657b5abe8c03) Save this question. (/posts/37458287/timeline) Show activity on this post. I am trying to run a cronjob inside a docker container that invokes a shell script. How can I do this? (/questions/tagged/docker) (show questions tagged 'docker') docker (/questions/tagged/cron) (show questions tagged 'cron') cron (/questions/tagged/containers) (show questions tagged 'containers') containers (/questions/tagged/sh) (show questions tagged 'sh') sh (/q/37458287) (Short permalink to this question) Share Share a link to this question Copy link (https://creativecommons.org/licenses/by-sa/4.0/) (The current license for this post: CC BY-SA 4.0) CC BY-SA 4.0 Follow (70:3:31e,16:33a8a66d54e9098e,10:1725308455,16:05d8b684e5749493,8:37458287,1431103842264f66881b563d47268b404a0f7ac516d33b63c4c0b8f7ab4e3206) Follow this question to receive notifications (/posts/37458287/revisions) (show all edits to this post) edited (2024-01-23 06:35:55Z) Jan 23 at 6:35 (/users/1773706/omer-dagan) (Omer Dagan's user avatar) (/users/1773706/omer-dagan) Omer Dagan (reputation score 15,728) 15.7k (16 gold badges) 16 16 gold badges (46 silver badges) 46 46 silver badges (61 bronze badges) 61 61 bronze badges asked (2016-05-26 10:32:53Z) May 26, 2016 at 10:32 (/users/6268839/c-heyer) (C Heyer's user avatar) (/users/6268839/c-heyer) C Heyer C Heyer (reputation score) 5,433 (3 gold badges) 3 3 gold badges (11 silver badges) 11 11 silver badges (8 bronze badges) 8 8 bronze badges 1 I would not recommand this at all. There are a few cases like "docker-registry" should clean up itself when its running as a container BUT! The setup is complicated and a job inside a container gives bad maintainability and eats resources. Instead: simply let your ROOT system handle the cronjobs instead. Go on your root-machine, and put your desired command into crontab using docker exec -it – (/users/2012636/steini) (2,773 reputation) Steini Commented (2024-05-03 07:25:50Z, License: CC BY-SA 4.0) May 3 at 7:25 (this comment was edited 2 times) (Use comments to ask for more information or suggest improvements. Avoid answering questions in comments.) Add a comment | (Expand to show all comments on this post) 32 Answers 32 Sorted by: (/questions/37458287/how-to-run-a-cron-job-inside-a-docker-container?answertab=scoredesc#tab-top) Reset to default (scoredesc) Highest score (default) (trending) Trending (recent votes count more) (modifieddesc) Date modified (newest first) (createdasc) Date created (oldest first) 1 (/questions/37458287/how-to-run-a-cron-job-inside-a-docker-container?page=2&tab=scoredesc#tab-top) (Go to page 2) 2 (/questions/37458287/how-to-run-a-cron-job-inside-a-docker-container?page=2&tab=scoredesc#tab-top) (Go to page 2) Next This answer is useful (70:3:31e,16:ef239d74eafbb767,10:1725308455,16:9bd1e950ec70b00f,8:37458519,db379710a4da65b821679c319607c63d172b23f16a5afd40e1c5a6e93abb9722) 639 (This answer is not useful) (70:3:31e,16:2f99dfd8e0dfc3f1,10:1725308455,16:6c88f5573f53a40f,8:37458519,fac36928a0fbf41cd472d7fcf047264aee14b94b29641a97453055d4fed7acf3) Save this answer. (Loading when this answer was accepted…) (/posts/37458519/timeline) Show activity on this post. You can copy your crontab into an image, in order for the container launched from said image to run the job. Important : as noted in (https://github.com/Ekito/docker-cron/issues/3) docker-cron issue 3 : use (https://stackoverflow.com/q/1552749/6309) LF, not CRLF for your cron file. See "(https://github.com/Ekito/docker-cron) Run a cron job with Docker " from (https://github.com/julienboulay) Julien Boulay in his (https://github.com/Ekito/docker-cron) Ekito/docker-cron : Let’s create a new file called "hello-cron " to describe our job. # must be ended with a new line "LF" (Unix) and not "CRLF" (Windows) * * * * * echo "Hello world" >> /var/log/cron.log 2>&1 # An empty line is required at the end of this file for a valid cron file. If you are wondering what is 2>&1 , (https://stackoverflow.com/users/40005/ayman-hourieh) Ayman Hourieh (https://stackoverflow.com/a/818284) explains . The following Dockerfile describes all the steps to build your image FROM ubuntu:latest MAINTAINER docker@ekito.fr RUN apt-get update && apt-get -y install cron # Copy hello-cron file to the cron.d directory COPY hello-cron /etc/cron.d/hello-cron # Give execution rights on the cron job RUN chmod 0644 /etc/cron.d/hello-cron # Apply cron job RUN crontab /etc/cron.d/hello-cron # Create the log file to be able to run tail RUN touch /var/log/cron.log # Run the command on container startup CMD cron && tail -f /var/log/cron.log But: if cron dies, the container (https://gist.github.com/x-yuri/f17b2cefc6e673e979850d79b265f345) keeps running . (see (https://stackoverflow.com/users/3716153/gaafar) Gaafar 's (https://stackoverflow.com/questions/37458287/how-to-run-a-cron-job-inside-a-docker-container/37458519?noredirect=1#comment67411829_37458519) comment and (https://askubuntu.com/questions/258219/how-do-i-make-apt-get-install-less-noisy#comment326577_258226) How do I make apt-get install less noisy? :apt-get -y install -qq --force-yes cron can work too) As noted by (https://stackoverflow.com/users/3417592/nathan-lloyd) Nathan Lloyd in (https://stackoverflow.com/questions/37458287/how-to-run-a-cron-job-inside-a-docker-container/37458519#comment107451752_37458519) the comments : Quick note about a gotcha:If you're adding a script file and telling cron to run it, remember toRUN chmod 0744 /the_script Cron fails silently if you forget . Warning : as noted in (https://stackoverflow.com/questions/37458287/how-to-run-a-cron-job-inside-a-docker-container/37458519#comment138118649_37458519) the comments by (https://stackoverflow.com/users/8046323/user8046323) user8046323 This config schedules tasks two times. One time with crontab and one time with cron.d Please use only one of the ways to evade scheduling your tasks twice True: the problem is with those two lines in the above Dockerfile: COPY hello-cron /etc/cron.d/hello-cron RUN crontab /etc/cron.d/hello-cron By placing the hello-cron file in the /etc/cron.d directory, you automatically schedule the cron jobs contained in this file. The cron daemon checks this directory for any files containing cron schedules and automatically loads them. The crontab command with /etc/cron.d/hello-cron takes the contents of the hello-cron file and loads them into the main crontab. This means the same jobs are now scheduled directly in the crontab as well, effectively duplicating them. you should choose one method to manage your cron jobs, depending on your specific needs: If you prefer using /etc/cron.d (often easier for managing multiple separate cron job files): COPY hello-cron /etc/cron.d/hello-cron RUN chmod 0644 /etc/cron.d/hello-cron If you prefer using crontab (gives you a consolidated view of all cron jobs and can be easier for a single or a few jobs): ADD hello-cron /etc/cronjob RUN crontab /etc/cronjob OR, make sure your job itself redirect directly to stdout/stderr instead of a log file, as described in (https://stackoverflow.com/users/7906071/hugoshaka) hugoShaka 's (https://stackoverflow.com/a/46220104/6309) answer : * * * * * root echo hello > /proc/1/fd/1 2>/proc/1/fd/2 Replace the last Dockerfile line with CMD ["cron" , "-f" ] But: it doesn't work if you want to run tasks (https://gist.github.com/x-yuri/336fd3185de7e168023867302d5ac99b) as a non-root . See also (about cron -f , which is to say cron "foreground") "(https://stackoverflow.com/q/30529057/6309) docker ubuntu cron -f is not working " Build and run it: sudo docker build --rm -t ekito/cron-example . sudo docker run -t -i ekito/cron-example Be patient, wait for 2 minutes and your command-line should display: Hello world Hello world (https://stackoverflow.com/users/1032870/eric) Eric adds (https://stackoverflow.com/questions/37458287/how-to-run-a-cron-job-inside-a-docker-container/37458519#comment84898391_37458519) in the comments : Do note that tail may not display the correct file if it is created during image build.If that is the case, you need to create or touch the file during container runtime in order for tail to pick up the correct file. See "(https://stackoverflow.com/a/43807880/6309) Output of tail -f at the end of a docker CMD is not showing ". See more in "(https://blog.thesparktree.com/cron-in-docker) Running Cron in Docker " (Apr. 2021) from (https://stackoverflow.com/users/1157633/jason-kulatunga) Jason Kulatunga , as he (https://stackoverflow.com/questions/37458287/how-to-run-a-cron-job-inside-a-docker-container/37458519#comment118918017_37458519) commented below See Jason's image (https://github.com/AnalogJ/docker-cron) AnalogJ/docker-cron based on: Dockerfile installing cronie /crond , depending on distribution. an entrypoint initializing /etc/environment and then calling cron -f -l 2 (/a/37458519) (Short permalink to this answer) Share Share a link to this answer Copy link (https://creativecommons.org/licenses/by-sa/4.0/) (The current license for this post: CC BY-SA 4.0) CC BY-SA 4.0 Follow (70:3:31e,16:5116c18fd69dd0d9,10:1725308455,16:179debbfc26a2af7,8:37458519,47ac6cc85a4ca699c1ff2df51822404209c7fcaa915f4eabcaa72517ffcd2599) Follow this answer to receive notifications (/posts/37458519/revisions) (show all edits to this post) edited (2024-04-18 13:29:25Z) Apr 18 at 13:29 answered (2016-05-26 10:42:39Z) May 26, 2016 at 10:42 (/users/6309/vonc) (VonC's user avatar) (/users/6309/vonc) VonC VonC (reputation score 1,296,737) 1.3m (554 gold badges) 554 554 gold badges (4.6k silver badges) 4.6k 4.6k silver badges (5.5k bronze badges) 5.5k 5.5k bronze badges 51 (number of 'useful comment' votes received) 4 you should probably add -y to installing cron to avoid docker build exiting – (/users/3716153/gafi) (12,579 reputation) gafi Commented (2016-10-16 07:41:14Z, License: CC BY-SA 3.0) Oct 16, 2016 at 7:41 (number of 'useful comment' votes received) 14 Does this solution still work? When I follow the guidelines given, when I log into the container as root and type crontab -l , I get No crontab installed for root , also, my screen remains blank. However, when I check '/etc/cron.d/', I see the crontab fiel is there (and even more surprisingly), when I check /var/log/cron.log , I see that the script is running (the file content is being appended with Hello World ). I'm pulling this image in my Dockerfile: FROM phusion/baseimage:0.10.0 . Any ideas about the discrepancy in behaviour? – (/users/962891/homunculus-reticulli) (67,648 reputation) Homunculus Reticulli Commented (2018-02-02 16:30:03Z, License: CC BY-SA 3.0) Feb 2, 2018 at 16:30 (number of 'useful comment' votes received) 28 As of 2018, this approach no longer works; has anyone been able to get their cronjob to work with Ubuntu as the base image? I'm not interested in the Alpine image which comes with cron running out of the box – (/users/2663059/pelican) (6,156 reputation) pelican Commented (2018-07-26 19:57:13Z, License: CC BY-SA 4.0) Jul 26, 2018 at 19:57 (number of 'useful comment' votes received) 5 Quick note about a gotcha: if you're adding a script file and telling cron to run it, remember to RUN chmod 0744 /the_script . Cron fails silently if you forget. – (/users/3417592/nathan-lloyd) (1,911 reputation) Nathan Lloyd Commented (2020-03-18 00:42:47Z, License: CC BY-SA 4.0) Mar 18, 2020 at 0:42 (number of 'useful comment' votes received) 7 I wrote a blog post that implements this advice (and other issues I found running cron in docker) into working docker images for multiple distros (ubuntu, alpine, centos): (https://blog.thesparktree.com/cron-in-docker) blog.thesparktree.com/cron-in-docker – (/users/1157633/jason-kulatunga) (5,883 reputation) Jason Kulatunga Commented (2021-04-27 05:37:00Z, License: CC BY-SA 4.0) Apr 27, 2021 at 5:37 (Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.) | (Expand to show all comments on this post) Show 46 more comments This answer is useful (70:3:31e,16:063a65f0ad573886,10:1725308455,16:1a10bdc5b3400e25,8:46220104,6bc38d6b3251bf53659019d4a405a00b8785d3928080d2a679f9244fcb8a7111) 272 (This answer is not useful) (70:3:31e,16:cd9202ea0a675b86,10:1725308455,16:5b7997a014eac3ec,8:46220104,ac23b9ab579651b27a5f168d792bfaec9cf29d707b4c77977f7d96b4174bc6ea) Save this answer. (Loading when this answer was accepted…) (/posts/46220104/timeline) Show activity on this post. The accepted answer may be dangerous in a production environment . In docker you should only execute one process per container because if you don't, the process that forked and went background is not monitored and may stop without you knowing it. When you use CMD cron && tail -f /var/log/cron.log the cron process basically fork in order to execute cron in background, the main process exits and let you execute tailf in foreground. The background cron process could stop or fail you won't notice, your container will still run silently and your orchestration tool will not restart it. You can avoid such a thing by redirecting directly the cron's commands output into your docker stdout and stderr which are located respectively in /proc/1/fd/1 and /proc/1/fd/2 . Using basic shell redirects you may want to do something like this : * * * * * root echo hello > /proc/1/fd/1 2>/proc/1/fd/2 And your CMD will be : CMD ["cron", "-f"] But: this doesn't work if you want to run tasks (https://gist.github.com/x-yuri/336fd3185de7e168023867302d5ac99b) as a non-root . (/a/46220104) (Short permalink to this answer) Share Share a link to this answer Copy link (https://creativecommons.org/licenses/by-sa/4.0/) (The current license for this post: CC BY-SA 4.0) CC BY-SA 4.0 Follow (70:3:31e,16:b468ea81af4c75ad,10:1725308455,16:11ff7d319347a890,8:46220104,71276f17ca58aa05e99b30721aa87364b4a9ccca764a0ad173db9ca85a6e0a8a) Follow this answer to receive notifications (/posts/46220104/revisions) (show all edits to this post) edited (2023-02-05 14:33:44Z) Feb 5, 2023 at 14:33 (/users/52499/x-yuri) (x-yuri's user avatar) (/users/52499/x-yuri) x-yuri (reputation score 18,275) 18.3k (15 gold badges) 15 15 gold badges (121 silver badges) 121 121 silver badges (172 bronze badges) 172 172 bronze badges answered (2017-09-14 13:11:11Z) Sep 14, 2017 at 13:11 (/users/7906071/hugoshaka) (hugoShaka's user avatar) (/users/7906071/hugoshaka) hugoShaka hugoShaka (reputation score) 5,337 (3 gold badges) 3 3 gold badges (18 silver badges) 18 18 silver badges (31 bronze badges) 31 31 bronze badges 12 (number of 'useful comment' votes received) 46 Nice: cron -f is for "cron foreground". I have included your answer in mine above, for more visibility. +1 – (/users/6309/vonc) (1,296,737 reputation) VonC Commented (2017-09-14 14:49:14Z, License: CC BY-SA 3.0) Sep 14, 2017 at 14:49 Let's say my program doesn't output anything. Can I still use this method and be sure my process isn't going to stop in the background? – (/users/6284028/arcsector) (1,331 reputation) Arcsector Commented (2018-05-18 19:34:32Z, License: CC BY-SA 4.0) May 18, 2018 at 19:34 (number of 'useful comment' votes received) 3 @Arcsector this method avoid putting a process in the background, thats why it does not fails silently. Having a background process in a docker container is not simple. If you want to have a running background process you might want to use an init process to monitor the multiple processes you run in the container. Another way is to start the process into another container next to the main one called 'sidecar'. The best way is often to avoid multiple processes in the container. – (/users/7906071/hugoshaka) (5,337 reputation) hugoShaka Commented (2018-05-22 14:14:16Z, License: CC BY-SA 4.0) May 22, 2018 at 14:14 (number of 'useful comment' votes received) 9 This is a good solution and works well for us aside from one issue. When the container receives a SIGTERM signal it doesn't seem to wait for the scheduled process to finish and gracefully shutdown, instead it is killing the process which can cause issues. – (/users/400193/james-hulse) (1,561 reputation) James Hulse Commented (2020-01-21 11:03:51Z, License: CC BY-SA 4.0) Jan 21, 2020 at 11:03 This solution worked for me on Debian/Alpine/CentOS containers. This is the most "portable" solution. Thanks for this @hugoShaka – (/users/1503683/pierre) (2,702 reputation) Pierre Commented (2021-09-17 08:32:44Z, License: CC BY-SA 4.0) Sep 17, 2021 at 8:32 (Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.) | (Expand to show all comments on this post) Show 7 more comments This answer is useful (70:3:31e,16:3a413810f2f6e997,10:1725308455,16:11f1b67d70913568,8:47960145,a3e6d3df980248d99fba5234fc52ba9c9915552f71fbf55d23f1dc702ddae32e) 231 (This answer is not useful) (70:3:31e,16:24cb4e454fed2df8,10:1725308455,16:d41d88ba0f007397,8:47960145,5900cc1a0426b5b904e9adc23a5d22723d7bf5aa62349f0b1bb7d9a110e399fd) Save this answer. (Loading when this answer was accepted…) (/posts/47960145/timeline) Show activity on this post. For those who wants to use a simple and lightweight image: FROM alpine:3.6 # copy crontabs for root user COPY config/cronjobs /etc/crontabs/root # start crond with log level 8 in foreground, output to stderr CMD ["crond" , "-f" , "-d" , "8" ] Where cronjobs is the file that contains your cronjobs, in this form: * * * * * echo "hello stackoverflow" >> /test_file 2>&1 # remember to end this file with an empty new line But apparently you won't see hello stackoverflow in docker logs . (/a/47960145) (Short permalink to this answer) Share Share a link to this answer Copy link (https://creativecommons.org/licenses/by-sa/4.0/) (The current license for this post: CC BY-SA 4.0) CC BY-SA 4.0 Follow (70:3:31e,16:27b52b715b5f6f1f,10:1725308455,16:f81aed6b19de3105,8:47960145,34615a64d677e16f9b49bc6f01d9376d695b51e16839371c5dbb97215d481511) Follow this answer to receive notifications (/posts/47960145/revisions) (show all edits to this post) edited (2023-02-05 15:09:17Z) Feb 5, 2023 at 15:09 (/users/52499/x-yuri) (x-yuri's user avatar) (/users/52499/x-yuri) x-yuri (reputation score 18,275) 18.3k (15 gold badges) 15 15 gold badges (121 silver badges) 121 121 silver badges (172 bronze badges) 172 172 bronze badges answered (2017-12-24 11:22:31Z) Dec 24, 2017 at 11:22 (/users/1668849/oscar-fanelli) (Oscar Fanelli's user avatar) (/users/1668849/oscar-fanelli) Oscar Fanelli Oscar Fanelli (reputation score) 3,567 (3 gold badges) 3 3 gold badges (31 silver badges) 31 31 silver badges (40 bronze badges) 40 40 bronze badges 9 (number of 'useful comment' votes received) 26 Simple, light and standard image based. This should be the accepted answer. Also use the > /proc/1/fd/1 2> /proc/1/fd/2 redirection to access cronjobs output directly from the docker logs. – (/users/4717639/henritel) (184 reputation) HenriTel Commented (2018-04-24 08:24:11Z, License: CC BY-SA 3.0) Apr 24, 2018 at 8:24 (number of 'useful comment' votes received) 10 For people not using alpine: The crond supporting the -d 8 parameter is not the standard cron, it is the crond command from busybox. For example from ubuntu, you can run this as busybox crond -f -d 8 . For older versions you have to use -L /dev/stdout/ . – (/users/685551/trendfischer) (7,572 reputation) Trendfischer Commented (2018-04-25 13:44:28Z, License: CC BY-SA 3.0) Apr 25, 2018 at 13:44 (this comment was edited 1 time) (number of 'useful comment' votes received) 3 I would give this +100 if I could. This is by far the best way to run cron jobs in a Docker environment. – (/users/3827514/jitsusama) (725 reputation) Jitsusama Commented (2018-05-16 12:43:22Z, License: CC BY-SA 4.0) May 16, 2018 at 12:43 (number of 'useful comment' votes received) 2 can this be done entirely by docker-compose.yml with an image:alpine ? – (/users/1307108/groostav) (3,220 reputation) Groostav Commented (2019-06-01 08:57:31Z, License: CC BY-SA 4.0) Jun 1, 2019 at 8:57 (number of 'useful comment' votes received) 3 CMD ["crond" or CMD ["cron" ? – (/users/4827363/codesmith) (1,431 reputation) codesmith Commented (2020-06-17 13:08:32Z, License: CC BY-SA 4.0) Jun 17, 2020 at 13:08 (Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.) | (Expand to show all comments on this post) Show 4 more comments This answer is useful (70:3:31e,16:bb6d5d0f83b4d18b,10:1725308455,16:214cf0b1da6aa4c3,8:44958097,0eb2958bc0a8c792a63ccf5463c5898e74d0026bc0cb129bd8364ce545371e3c) 69 (This answer is not useful) (70:3:31e,16:7608c81bfd8c60a6,10:1725308455,16:5c91fccefe1487f8,8:44958097,963521b5930ff40a5b53a1e3fbca6c0a71efac86952a773df94a05e87caecde8) Save this answer. (Loading when this answer was accepted…) (/posts/44958097/timeline) Show activity on this post. What @VonC has suggested is nice but I prefer doing all cron job configuration in one line. This would avoid cross platform issues like cronjob location and you don't need a separate cron file. FROM ubuntu:latest # Install cron RUN apt-get -y install cron # Create the log file to be able to run tail RUN touch /var/log/cron.log # Setup cron job RUN (crontab -l ; echo "* * * * * echo " Hello world" >> /var/log/cron.log" ) | crontab # Run the command on container startup CMD cron && tail -f /var/log/cron.log After running your docker container, you can make sure if cron service is working by: # To check if the job is scheduled docker exec -ti bash -c "crontab -l" # To check if the cron service is running docker exec -ti bash -c "pgrep cron" If you prefer to have ENTRYPOINT instead of CMD, then you can substitute the CMD above with ENTRYPOINT cron start && tail -f /var/log/cron.log But: if cron dies, the container (https://gist.github.com/x-yuri/f17b2cefc6e673e979850d79b265f345) keeps running . (/a/44958097) (Short permalink to this answer) Share Share a link to this answer Copy link (https://creativecommons.org/licenses/by-sa/4.0/) (The current license for this post: CC BY-SA 4.0) CC BY-SA 4.0 Follow (70:3:31e,16:763102883098fb3b,10:1725308455,16:89075f2ae70ed391,8:44958097,0ec31cd35a591908b26ac68bdf43bbce64c356d20c2b3d752d77ad748705fdff) Follow this answer to receive notifications (/posts/44958097/revisions) (show all edits to this post) edited (2023-02-05 15:11:11Z) Feb 5, 2023 at 15:11 (/users/52499/x-yuri) (x-yuri's user avatar) (/users/52499/x-yuri) x-yuri (reputation score 18,275) 18.3k (15 gold badges) 15 15 gold badges (121 silver badges) 121 121 silver badges (172 bronze badges) 172 172 bronze badges answered (2017-07-06 20:15:49Z) Jul 6, 2017 at 20:15 (/users/1572286/youness) (Youness's user avatar) (/users/1572286/youness) Youness Youness (reputation score) 2,050 (24 silver badges) 24 24 silver badges (29 bronze badges) 29 29 bronze badges 5 (number of 'useful comment' votes received) 8 RUN apt-get update && apt-get -y install cron or else it will not be able to find package cron – (/users/3476750/alphabetasoup) (606 reputation) alphabetasoup Commented (2017-08-29 23:08:10Z, License: CC BY-SA 3.0) Aug 29, 2017 at 23:08 (number of 'useful comment' votes received) 3 Thanks Youness, you gave me the idea of doing the following, which worked in my case where each cron is specified in a different file: RUN cat $APP_HOME/crons/* | crontab Like a charm :) – (/users/2839317/marcostvz) (1,278 reputation) marcostvz Commented (2017-11-30 15:51:37Z, License: CC BY-SA 3.0) Nov 30, 2017 at 15:51 (this comment was edited 1 time) adding cron to an entrypoint script seems like the best option: ENTRYPOINT ["entrypoint.sh"] – (/users/488784/bozdoz) (12,800 reputation) bozdoz Commented (2020-05-08 00:45:59Z, License: CC BY-SA 4.0) May 8, 2020 at 0:45 (number of 'useful comment' votes received) 1 Using 2 commands in your ENTRYPOINT is dangerous. I believe the first one (cron ) forks to the background, while 2nd one (tail ) runs in the foreground. If cron stops, you'll never know it. If tail stops, then docker will notice. – (/users/879170/paul-sturm) (2,128 reputation) Paul Sturm Commented (2021-03-05 23:43:08Z, License: CC BY-SA 4.0) Mar 5, 2021 at 23:43 That makes sense to some extent though you can add some monitoring/logging around it (with another entry point or some other monitoring mechanisms) to check the health status of the cron service – (/users/1572286/youness) (2,050 reputation) Youness Commented (2021-03-25 21:55:56Z, License: CC BY-SA 4.0) Mar 25, 2021 at 21:55 (Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.) Add a comment | (Expand to show all comments on this post) This answer is useful (70:3:31e,16:73b2fc1d76ef8cc0,10:1725308455,16:d7ce570481dd6d99,8:46255009,e2aee56c9fca28a121453891c5def03b33363f056b134b9b37a6fd00205aa943) 29 (This answer is not useful) (70:3:31e,16:286e22b137107a87,10:1725308455,16:9e0f548d457cd322,8:46255009,f5c37222422d08d9305fdb54700b7badf96fb60c40e797d0c59cc9a915a911eb) Save this answer. (Loading when this answer was accepted…) (/posts/46255009/timeline) Show activity on this post. There is another way to do it, is to use (http://github.com/opsxcq/tasker) Tasker , a task runner that has cron (a scheduler) support. Why ? Sometimes to run a cron job, you have to mix, your base image (python, java, nodejs, ruby) with the crond. That means another image to maintain. Tasker avoid that by decoupling the crond and you container. You can just focus on the image that you want to execute your commands, and configure Tasker to use it. Here an docker-compose.yml file, that will run some tasks for you version: "2" services: tasker: image: strm/tasker volumes: - "/var/run/docker.sock:/var/run/docker.sock" environment: configuration: | logging: level: ROOT: WARN org.springframework.web: WARN sh.strm: DEBUG schedule: - every: minute task: hello - every: minute task: helloFromPython - every: minute task: helloFromNode tasks: docker: - name: hello image: debian:jessie script: - echo Hello world from Tasker - name: helloFromPython image: python:3-slim script: - python -c 'print("Hello world from python")' - name: helloFromNode image: node:8 script: - node -e 'console.log("Hello from node")' There are 3 tasks there, all of them will run every minute (every: minute ), and each of them will execute the script code, inside the image defined in image section. Just run docker-compose up , and see it working. Here is the Tasker repo with the full documentation: (http://github.com/opsxcq/tasker) http://github.com/opsxcq/tasker (/a/46255009) (Short permalink to this answer) Share Share a link to this answer Copy link (https://creativecommons.org/licenses/by-sa/3.0/) (The current license for this post: CC BY-SA 3.0) CC BY-SA 3.0 Follow (70:3:31e,16:8e41b53b00ee9619,10:1725308455,16:e661ee473a4c2cba,8:46255009,2a1f692ac1a6324e4c0e78e80de3df63009ffe8333a0a2b780dc95e0172e9d7a) Follow this answer to receive notifications answered (2017-09-16 14:33:34Z) Sep 16, 2017 at 14:33 (/users/6131920/opsxcq) (OPSXCQ's user avatar) (/users/6131920/opsxcq) OPSXCQ OPSXCQ (reputation score) 556 (5 silver badges) 5 5 silver badges (6 bronze badges) 6 6 bronze badges 4 Dockerception (running docker containers from another container) is a bad practice and should be limited to continuous integration. A workaround would be to use docker exec on specified containers. – (/users/4717639/henritel) (184 reputation) HenriTel Commented (2018-04-23 09:36:00Z, License: CC BY-SA 3.0) Apr 23, 2018 at 9:36 (number of 'useful comment' votes received) 1 Tasker doesn't use docker in docker (Dind/Dockerception), note that is passed the docker socket as a mapping, all containers spawned are are in spawned in the daemon that tasker runs. And if you don't want to run tasker inside docker, you can just deploy it as any other application. – (/users/6131920/opsxcq) (556 reputation) OPSXCQ Commented (2018-04-27 22:17:24Z, License: CC BY-SA 3.0) Apr 27, 2018 at 22:17 (number of 'useful comment' votes received) 2 I don't get the advantages of using tasker. Seems really a overkill to me using java and sh*** just to run a cron job. – (/users/1059828/karl-adler) (16,628 reputation) Karl Adler Commented (2018-10-30 14:59:14Z, License: CC BY-SA 4.0) Oct 30, 2018 at 14:59 Mixing cron and the base image that you need (python/node for example) create a extra dependency that need to be maintained and deployed, in this scenario all jobs share the same container, it means that you have to worry about cleaning up everything after every job runs. Jobs running on tasker are idempotent, so you have less things to worry about. – (/users/6131920/opsxcq) (556 reputation) OPSXCQ Commented (2019-01-10 13:59:26Z, License: CC BY-SA 4.0) Jan 10, 2019 at 13:59 (Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.) Add a comment | (Expand to show all comments on this post) This answer is useful (70:3:31e,16:745f557796adfe62,10:1725308455,16:e47b07e3522b74d6,8:41186983,e25a17a1e569a9c011ff0b04a3a160f0619379518466ffb173d3e44028d6b634) 20 (This answer is not useful) (70:3:31e,16:0422bff20692d06b,10:1725308455,16:c0cbb0c9130ad9d1,8:41186983,5db2cab27814087b1816450cc8058c170a1e376087ea193a8b80ac3e95e3360e) Save this answer. (Loading when this answer was accepted…) (/posts/41186983/timeline) Show activity on this post. Though this aims to run jobs beside a running process in a container via Docker's exec interface, this may be of interest for you. I've written a daemon that observes containers and schedules jobs, defined in their metadata, on them. Example: version: '2' services: wordpress: image: wordpress mysql: image: mariadb volumes: - ./database_dumps:/dumps labels: deck-chores.dump.command: sh -c "mysqldump --all-databases > /dumps/dump-$$(date -Idate) " deck-chores.dump.interval: daily 'Classic', cron-like configuration is also possible. Here are the (http://deck-chores.rtfd.io/) docs , here's the (https://hub.docker.com/r/funkyfuture/deck-chores/) image repository . (/a/41186983) (Short permalink to this answer) Share Share a link to this answer Copy link (https://creativecommons.org/licenses/by-sa/3.0/) (The current license for this post: CC BY-SA 3.0) CC BY-SA 3.0 Follow (70:3:31e,16:137e8db3ed0ba589,10:1725308455,16:ea347560bb571fa1,8:41186983,1f22ed84b15f635816e3e49c161fa1d842c7b4aa82884d6e447208e4b2fa671f) Follow this answer to receive notifications answered (2016-12-16 14:52:22Z) Dec 16, 2016 at 14:52 (/users/2489914/funky-future) (funky-future's user avatar) (/users/2489914/funky-future) funky-future funky-future (reputation score) 3,917 (1 gold badge) 1 1 gold badge (33 silver badges) 33 33 silver badges (43 bronze badges) 43 43 bronze badges 2 (number of 'useful comment' votes received) 1 Thank you. This answer is most right for the Docker containers environment. No any changes in Docker images, only adding special container for executing tasks, it works like command docker exec by schedule. – (/users/5320149/prihlop) (1,599 reputation) PRIHLOP Commented (2019-10-06 10:25:46Z, License: CC BY-SA 4.0) Oct 6, 2019 at 10:25 (this comment was edited 1 time) This is gold! A very elegant and easy-to-use solution, in the paradigm of containers. Getting rid of crutches. – (/users/1056384/vatavale) (1,570 reputation) vatavale Commented (2022-06-08 15:48:52Z, License: CC BY-SA 4.0) Jun 8, 2022 at 15:48 (Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.) Add a comment | (Expand to show all comments on this post) This answer is useful (70:3:31e,16:8f692ac6d1b068b4,10:1725308455,16:4d867c1d629ee452,8:59413589,a15d512856d0751e5c7a6f9e4823cc7c21bd07360bb3c2d44c36e9d11a52c35a) 15 (This answer is not useful) (70:3:31e,16:5ff7b95fc9a68598,10:1725308455,16:272ed7ee69965224,8:59413589,5cc1aeb8eed6cfed86cf3d8015cb3336cd4ab3e58a3b6f65e3d16742919650a3) Save this answer. (Loading when this answer was accepted…) (/posts/59413589/timeline) Show activity on this post. If you're using docker for windows, remember that you have to change your line-ending format from CRLF to LF (i.e. from dos to unix) if you intend on importing your crontab file from windows to your ubuntu container. If not, your cron-job won't work. Here's a working example: FROM ubuntu:latest RUN apt-get update && apt-get -y install cron RUN apt-get update && apt-get install -y dos2unix # Add crontab file (from your windows host) to the cron directory ADD cron/hello-cron /etc/cron.d/hello-cron # Change line ending format to LF RUN dos2unix /etc/cron.d/hello-cron # Give execution rights on the cron job RUN chmod 0644 /etc/cron.d/hello-cron # Apply cron job RUN crontab /etc/cron.d/hello-cron # Create the log file to be able to run tail RUN touch /var/log/hello-cron.log # Run the command on container startup CMD cron && tail -f /var/log/hello-cron.log This actually took me hours to figure out, as debugging cron jobs in docker containers is a tedious task. Hope it helps anyone else out there that can't get their code to work! But: if cron dies, the container (https://gist.github.com/x-yuri/f17b2cefc6e673e979850d79b265f345) keeps running . (/a/59413589) (Short permalink to this answer) Share Share a link to this answer Copy link (https://creativecommons.org/licenses/by-sa/4.0/) (The current license for this post: CC BY-SA 4.0) CC BY-SA 4.0 Follow (70:3:31e,16:fe9cf7b950ca78dd,10:1725308455,16:b1014cb6d5af8d1b,8:59413589,54997209aacad8b229b73438276a7de350423f68e9891eca8df2a9b6cb3be94d) Follow this answer to receive notifications (/posts/59413589/revisions) (show all edits to this post) edited (2023-02-05 15:19:58Z) Feb 5, 2023 at 15:19 (/users/52499/x-yuri) (x-yuri's user avatar) (/users/52499/x-yuri) x-yuri (reputation score 18,275) 18.3k (15 gold badges) 15 15 gold badges (121 silver badges) 121 121 silver badges (172 bronze badges) 172 172 bronze badges answered (2019-12-19 16:28:38Z) Dec 19, 2019 at 16:28 (/users/8353218/andreas-forsl%c3%b6w) (Andreas Forslöw's user avatar) (/users/8353218/andreas-forsl%c3%b6w) Andreas Forslöw Andreas Forslöw (reputation score) 2,588 (1 gold badge) 1 1 gold badge (27 silver badges) 27 27 silver badges (37 bronze badges) 37 37 bronze badges 1 (number of 'useful comment' votes received) 1 This helped solve my issue when trying to get output redirection to work. A command like cat /proc/1/status > /proc/1/fd/1 would return an error from crond stating crond: USER root pid 6 cmd root cat /proc/1/status > /proc/1/fd/1: nonexistent directory/proc/1/fd/1 . Changing the line endings to Unix enabled me to run the command successfully. Thanks, this took me more than a few hours to figure out! – (/users/3141986/daryl-wright) (81 reputation) Daryl Wright Commented (2020-10-28 13:06:31Z, License: CC BY-SA 4.0) Oct 28, 2020 at 13:06 (Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.) Add a comment | (Expand to show all comments on this post) This answer is useful (70:3:31e,16:8914ef9dca789fc8,10:1725308455,16:2c3d7a8987faf685,8:44642583,e2e9d691ac37b2cb1f1b8216160c64cbf59fc8265272b5f4106b0e1ef23f72a5) 13 (This answer is not useful) (70:3:31e,16:3518f30ba887ccb6,10:1725308455,16:575301a04f67520c,8:44642583,8c5321ffe7976c90bf6c9cce561c9af91022005d503d472ef465f34129b8afba) Save this answer. (Loading when this answer was accepted…) (/posts/44642583/timeline) Show activity on this post. (https://stackoverflow.com/users/6309/vonc) (VonC's) VonC's answer is pretty thorough. In addition I'd like to add one thing that helped me. If you just want to run a cron job without tailing a file, you'd be tempted to just remove the && tail -f /var/log/cron.log from the cron command. However this will cause the Docker container to exit shortly after running because when the cron command completes, Docker thinks the last command has exited and hence kills the container. This can be avoided by running cron in the foreground via cron -f . (/a/44642583) (Short permalink to this answer) Share Share a link to this answer Copy link (https://creativecommons.org/licenses/by-sa/4.0/) (The current license for this post: CC BY-SA 4.0) CC BY-SA 4.0 Follow (70:3:31e,16:5920f5993ccec01c,10:1725308455,16:52d7d969a67c4751,8:44642583,4247c15632926c2573d3f0f37989b2f69d73ce92304b9aa2c3be6f218d1e6eb8) Follow this answer to receive notifications (/posts/44642583/revisions) (show all edits to this post) edited (2019-05-24 11:24:52Z) May 24, 2019 at 11:24 (/users/472495/halfer) (halfer's user avatar) (/users/472495/halfer) halfer (reputation score 20,267) 20.3k (19 gold badges) 19 19 gold badges (106 silver badges) 106 106 silver badges (197 bronze badges) 197 197 bronze badges answered (2017-06-20 01:54:43Z) Jun 20, 2017 at 1:54 (/users/8185841/vanugrah) (vanugrah's user avatar) (/users/8185841/vanugrah) vanugrah vanugrah (reputation score) 131 (1 silver badge) 1 1 silver badge (4 bronze badges) 4 4 bronze badges (Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.) Add a comment | (Expand to show all comments on this post) This answer is useful (70:3:31e,16:05969e46b357c48b,10:1725308455,16:02304442d203aac5,8:66338297,4b1427f605c5668f1c11e588c15ba2363b0be7db827b4a45623cdca0e81f6468) 13 (This answer is not useful) (70:3:31e,16:dddb0a967399c1db,10:1725308455,16:14ffe280ab3989bb,8:66338297,4d47f500f72abf175196d5a9e9bb42bdafa24fade745f120d70cae094c16768f) Save this answer. (Loading when this answer was accepted…) (/posts/66338297/timeline) Show activity on this post. Unfortunately, none of the above answers worked for me, although all answers lead to the solution and eventually to my solution, here is the snippet if it helps someone. Thanks This can be solved with the bash file, due to the layered architecture of the Docker, cron service doesn't get initiated with RUN/CMD/ENTRYPOINT commands. Simply add a bash file which will initiate the cron and other services (if required) DockerFile FROM gradle:6.5.1-jdk11 AS build # apt RUN apt-get update RUN apt-get -y install cron # Setup cron to run every minute to print (you can add/update your cron here) RUN touch /var/log/cron-1.log RUN (crontab -l ; echo "* * * * * echo testing cron.... >> /var/log/cron-1.log 2>&1" ) | crontab # entrypoint.sh RUN chmod +x entrypoint.sh CMD ["bash" ,"entrypoint.sh" ] entrypoint.sh #!/bin/sh service cron start & tail -f /var/log/cron-2.log If any other service is also required to run along with cron then add that service with & in the same command, for example: /opt/wildfly/bin/standalone.sh & service cron start & tail -f /var/log/cron-2.log Once you will get into the docker container there you can see that testing cron.... will be getting printed every minute in file: /var/log/cron-1.log But, if cron dies, the container (https://gist.github.com/x-yuri/f17b2cefc6e673e979850d79b265f345) keeps running . (/a/66338297) (Short permalink to this answer) Share Share a link to this answer Copy link (https://creativecommons.org/licenses/by-sa/4.0/) (The current license for this post: CC BY-SA 4.0) CC BY-SA 4.0 Follow (70:3:31e,16:90b9dab6347f7824,10:1725308455,16:eda5d50c82899ed8,8:66338297,a16f6683dc80567d29cdaf0f17e8eceaffa0189daf3c0254de09690a11b520cb) Follow this answer to receive notifications (/posts/66338297/revisions) (show all edits to this post) edited (2023-02-05 15:21:49Z) Feb 5, 2023 at 15:21 (/users/52499/x-yuri) (x-yuri's user avatar) (/users/52499/x-yuri) x-yuri (reputation score 18,275) 18.3k (15 gold badges) 15 15 gold badges (121 silver badges) 121 121 silver badges (172 bronze badges) 172 172 bronze badges answered (2021-02-23 17:41:07Z) Feb 23, 2021 at 17:41 (/users/2947397/gaurav-tyagi) (Gaurav Tyagi's user avatar) (/users/2947397/gaurav-tyagi) Gaurav Tyagi Gaurav Tyagi (reputation score) 679 (7 silver badges) 7 7 silver badges (8 bronze badges) 8 8 bronze badges 2 (number of 'useful comment' votes received) 2 Shouldn't it be doing tail -f /var/log/cron-1.log instead of /var/log/cron-2.log , since cron-1.log is where the STDOUT/STDERR is being directed? (Unless I'm missing something) – (/users/8792/peter) (6,352 reputation) Peter Commented (2021-07-04 05:05:34Z, License: CC BY-SA 4.0) Jul 4, 2021 at 5:05 Yes, correct, that was a typo, /var/log/cron-1.log should be at every place – (/users/2947397/gaurav-tyagi) (679 reputation) Gaurav Tyagi Commented (2021-07-05 08:06:42Z, License: CC BY-SA 4.0) Jul 5, 2021 at 8:06 (Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.) Add a comment | (Expand to show all comments on this post) This answer is useful (70:3:31e,16:81356d233a8a75b5,10:1725308455,16:555906a1a5021246,8:40131842,4c793c385b75b8e2ef07701c4b98e34cf521930d0142f6bbaabffb5048a3f245) 10 (This answer is not useful) (70:3:31e,16:0d7614b6c1698e39,10:1725308455,16:10a7e8cb6ee0665c,8:40131842,6b932ec2f37d944403bb99f7cd40b240105effa9bbc84e4bcadedcafc5aa4579) Save this answer. (Loading when this answer was accepted…) (/posts/40131842/timeline) Show activity on this post. I created a Docker image based on the other answers, which can be used like docker run -v "/path/to/cron:/etc/cron.d/crontab" gaafar/cron where /path/to/cron : absolute path to crontab file, or you can use it as a base in a Dockerfile: FROM gaafar/cron # COPY crontab file in the cron directory COPY crontab /etc/cron.d/crontab # Add your commands here For reference, the image (https://hub.docker.com/r/gaafar/cron/) is here . (/a/40131842) (Short permalink to this answer) Share Share a link to this answer Copy link (https://creativecommons.org/licenses/by-sa/4.0/) (The current license for this post: CC BY-SA 4.0) CC BY-SA 4.0 Follow (70:3:31e,16:2afccfbb12d1f900,10:1725308455,16:ba54312a5a055970,8:40131842,a9ad1c294a6656d1d38af72928aa9d12525f7e084e38b10788ffdbe88178883d) Follow this answer to receive notifications (/posts/40131842/revisions) (show all edits to this post) edited (2022-02-10 15:30:21Z) Feb 10, 2022 at 15:30 (/users/5446749/vvvvv) (vvvvv's user avatar) (/users/5446749/vvvvv) vvvvv (reputation score 30,033) 30k (19 gold badges) 19 19 gold badges (57 silver badges) 57 57 silver badges (97 bronze badges) 97 97 bronze badges answered (2016-10-19 12:48:30Z) Oct 19, 2016 at 12:48 (/users/3716153/gafi) (gafi's user avatar) (/users/3716153/gafi) gafi gafi (reputation score 12,579) 12.6k (2 gold badges) 2 2 gold badges (31 silver badges) 31 31 silver badges (33 bronze badges) 33 33 bronze badges 0 (Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.) Add a comment | (Expand to show all comments on this post) This answer is useful (70:3:31e,16:bae5ac25ce3cb45e,10:1725308455,16:8392a378c16e9b58,8:75353647,73232bfbfeeadf1515ccbae97bc6326e2ef5cb1f5c33d6ac976c6d0796d42877) 9 (This answer is not useful) (70:3:31e,16:1cfab949a384e55b,10:1725308455,16:64a53ec16c040b4c,8:75353647,ed13cb1df5bdd9eb22cd841d2fe741f2ad4f5057396e7560c5571f9ae54069a4) Save this answer. (Loading when this answer was accepted…) (/posts/75353647/timeline) Show activity on this post. I occasionally tried to find a docker -friendly cron implementation. And this last time I tried, I've found a couple. By docker -friendly I mean, "output of the tasks can be seen in docker logs w/o resorting to tricks." The most promising I see at the moment is (https://github.com/aptible/supercronic) supercronic . It can be fed a crontab file, all while being docker -friendly. To make use of it: docker-compose.yml : services: supercronic: build: . command: supercronic crontab Dockerfile : FROM alpine:3.17 RUN set -x \ && apk add --no-cache supercronic shadow \ && useradd -m app USER app COPY crontab . crontab : * * * * * date A (https://gist.github.com/x-yuri/427172d62edc9ac6596d5382365e78b5) gist with a bit more info. Another good one is (https://gist.github.com/x-yuri/66fc314d2e38395ff5778398c233e017) yacron , but it uses YAML. (https://gist.github.com/x-yuri/d135bfabbe1b4798ad2fcfbca11a907c) ofelia can be used, but they seem to focus on running tasks in separate containers. Which is probably not a downside, but I'm not sure why I'd want to do that. And there's also a number of traditional cron implementations: (https://gist.github.com/x-yuri/b21d457de5025379c7657fb7b139267b) dcron , (https://gist.github.com/x-yuri/ddbf1cd7226d54da7c7e868c3ba5ba12) fcron , (https://gist.github.com/x-yuri/4d6391931c0b6802f93e4c3c1f418066) cronie . But they come with "no easy way to see output of the tasks." (/a/75353647) (Short permalink to this answer) Share Share a link to this answer Copy link (https://creativecommons.org/licenses/by-sa/4.0/) (The current license for this post: CC BY-SA 4.0) CC BY-SA 4.0 Follow (70:3:31e,16:34d069df74fe32e9,10:1725308455,16:ae2e9c866680423e,8:75353647,2b7bd7f52be48eaf502534724ab533283fa16a1a3faa6939241b2848f475e36d) Follow this answer to receive notifications answered (2023-02-05 16:05:32Z) Feb 5, 2023 at 16:05 (/users/52499/x-yuri) (x-yuri's user avatar) (/users/52499/x-yuri) x-yuri x-yuri (reputation score 18,275) 18.3k (15 gold badges) 15 15 gold badges (121 silver badges) 121 121 silver badges (172 bronze badges) 172 172 bronze badges 2 A good alternative to my (https://stackoverflow.com/a/37458519/6309) answer indeed . – (/users/6309/vonc) (1,296,737 reputation) VonC Commented (2023-02-05 16:55:23Z, License: CC BY-SA 4.0) Feb 5, 2023 at 16:55 i just tested supercronic and it works as expected. i will now test yacron as the additional configuration options are a very nice addon in my usecase! – (/users/574981/stefan-kr%c3%bcger-s-light) (1,004 reputation) Stefan Krüger s-light Commented (2024-06-25 08:01:31Z, License: CC BY-SA 4.0) Jun 25 at 8:01 (Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.) Add a comment | (Expand to show all comments on this post) This answer is useful (70:3:31e,16:86589866973ba235,10:1725308455,16:a33897ad66b6e45f,8:68037383,876baa12538d3235b4285b76a5d01b668314259d798e91ee88787d33ff850b99) 7 (This answer is not useful) (70:3:31e,16:522d8e842e4c38ad,10:1725308455,16:7530a34d64e84850,8:68037383,83c8b13a22545125506b28531bdbcc4f279e820ba32d71d0248bcdb176363df0) Save this answer. (Loading when this answer was accepted…) (/posts/68037383/timeline) Show activity on this post. I decided to use busybox, as it is one of the smallest images. crond is executed in foreground (-f), logging is send to stderr (-d), I didn't choose to change the loglevel. crontab file is copied to the default path: /var/spool/cron/crontabs FROM busybox:1.33.1 # Usage: crond [-fbS] [-l N] [-d N] [-L LOGFILE] [-c DIR] # # -f Foreground # -b Background (default) # -S Log to syslog (default) # -l N Set log level. Most verbose 0, default 8 # -d N Set log level, log to stderr # -L FILE Log to FILE # -c DIR Cron dir. Default:/var/spool/cron/crontabs COPY crontab /var/spool/cron/crontabs/root CMD [ "crond" , "-f" , "-d" ] But output of the tasks apparently can't be seen in docker logs . (/a/68037383) (Short permalink to this answer) Share Share a link to this answer Copy link (https://creativecommons.org/licenses/by-sa/4.0/) (The current license for this post: CC BY-SA 4.0) CC BY-SA 4.0 Follow (70:3:31e,16:66c094027579ae07,10:1725308455,16:17f63186b792fecb,8:68037383,a6f42a1429908bcd98fa32bdd4dbd85603525f19fa9624e5bc0c42b653900107) Follow this answer to receive notifications (/posts/68037383/revisions) (show all edits to this post) edited (2023-02-05 15:23:01Z) Feb 5, 2023 at 15:23 (/users/52499/x-yuri) (x-yuri's user avatar) (/users/52499/x-yuri) x-yuri (reputation score 18,275) 18.3k (15 gold badges) 15 15 gold badges (121 silver badges) 121 121 silver badges (172 bronze badges) 172 172 bronze badges answered (2021-06-18 14:57:28Z) Jun 18, 2021 at 14:57 (/users/3811821/iljanne) (Iljanne's user avatar) (/users/3811821/iljanne) Iljanne Iljanne (reputation score) 71 (1 silver badge) 1 1 silver badge (2 bronze badges) 2 2 bronze badges 1 The -d parameter requires the log level as argument. You should change your CMD line to: CMD [ "crond", "-f", "-d", "8" ] – (/users/6334421/daniel) (431 reputation) Daniel Commented (2023-01-14 12:45:56Z, License: CC BY-SA 4.0) Jan 14, 2023 at 12:45 (Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.) Add a comment | (Expand to show all comments on this post) This answer is useful (70:3:31e,16:70ee0da74f7881f4,10:1725308455,16:f5e946524fff4e0c,8:48611098,29029067330cff4b90055772a797fdb8a900d51a3b0d11eb763a6488b81640e0) 6 (This answer is not useful) (70:3:31e,16:3618346f38e316be,10:1725308455,16:5557d8b079cbd0d8,8:48611098,978d508573fbbc063e73a4c8233f091a654e5301d7a22ae198e63272e2749b96) Save this answer. (Loading when this answer was accepted…) (/posts/48611098/timeline) Show activity on this post. Define the cronjob in a dedicated container which runs the command via docker exec to your service. This is higher cohesion and the running script will have access to the environment variables you have defined for your service. #docker-compose.yml version: "3.3" services: myservice: environment: MSG: i'm being cronjobbed, every minute! image: alpine container_name: myservice command: tail -f /dev/null cronjobber: image: docker:edge volumes: - /var/run/docker.sock:/var/run/docker.sock container_name: cronjobber command: > sh -c " echo ' * * * * * docker exec myservice printenv | grep MSG' > /etc/crontabs/root && crond -f" (/a/48611098) (Short permalink to this answer) Share Share a link to this answer Copy link (https://creativecommons.org/licenses/by-sa/3.0/) (The current license for this post: CC BY-SA 3.0) CC BY-SA 3.0 Follow (70:3:31e,16:ae2a50db2739a3e8,10:1725308455,16:07a9d56f6e01ca2e,8:48611098,5957ca01b190655a410b6199daf9303ca4d67ae6328c35f0d86891870441a04e) Follow this answer to receive notifications answered (2018-02-04 17:40:00Z) Feb 4, 2018 at 17:40 (/users/956415/jakob-eriksson) (Jakob Eriksson's user avatar) (/users/956415/jakob-eriksson) Jakob Eriksson Jakob Eriksson (reputation score 18,697) 18.7k (1 gold badge) 1 1 gold badge (27 silver badges) 27 27 silver badges (34 bronze badges) 34 34 bronze badges 2 I was unable to get this to work using docker swarm. Getting myservice unknown errors. – (/users/13233/mark-grimes) (567 reputation) Mark Grimes Commented (2018-07-18 17:13:25Z, License: CC BY-SA 4.0) Jul 18, 2018 at 17:13 (number of 'useful comment' votes received) 2 There should be a warning about the high security impact mounting a docker socket has: (https://www.lvh.io/posts/dont-expose-the-docker-socket-not-even-to-a-container.html) lvh.io/posts/… – (/users/5335632/casual) (129 reputation) casual Commented (2019-10-22 14:28:36Z, License: CC BY-SA 4.0) Oct 22, 2019 at 14:28 (Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.) Add a comment | (Expand to show all comments on this post) This answer is useful (70:3:31e,16:f4c11c28b9ae4a7f,10:1725308455,16:87fd4307d334d810,8:41177998,f3fe2cfa785ee63c102c095c37a1890ef57252b84500ad367ec42837db850248) 5 (This answer is not useful) (70:3:31e,16:d2873477ae164f2f,10:1725308455,16:b1af08d16f1ae9cc,8:41177998,68b1b702a6a956ec2866f8814d3bcb8d0bb51c58f9262af073a69677e92d4f9e) Save this answer. (Loading when this answer was accepted…) (/posts/41177998/timeline) Show activity on this post. When you deploy your container on another host, just note that it won't start any processes automatically. You need to make sure that 'cron' service is running inside your container. In our case, I am using Supervisord with other services to start cron service. [program:misc] command =/etc/init.d/cron restart user=root autostart=true autorestart=true stderr_logfile=/var/log/misc-cron.err.log stdout_logfile=/var/log/misc-cron.out.log priority=998 (/a/41177998) (Short permalink to this answer) Share Share a link to this answer Copy link (https://creativecommons.org/licenses/by-sa/3.0/) (The current license for this post: CC BY-SA 3.0) CC BY-SA 3.0 Follow (70:3:31e,16:325e87b0c2747882,10:1725308455,16:27afdd35b7df5bb5,8:41177998,e19fc5a4cbe8fb27cb116441cd7e626d9e92daad86acb5111ec019bbe4228feb) Follow this answer to receive notifications (/posts/41177998/revisions) (show all edits to this post) edited (2016-12-16 07:08:49Z) Dec 16, 2016 at 7:08 (/users/5771917/priya) (Priya's user avatar) (/users/5771917/priya) Priya (reputation score) 1,357 (6 gold badges) 6 6 gold badges (21 silver badges) 21 21 silver badges (41 bronze badges) 41 41 bronze badges answered (2016-12-16 05:48:09Z) Dec 16, 2016 at 5:48 (/users/5726427/sagar-ghuge) (Sagar Ghuge's user avatar) (/users/5726427/sagar-ghuge) Sagar Ghuge Sagar Ghuge (reputation score) 231 (3 silver badges) 3 3 silver badges (9 bronze badges) 9 9 bronze badges 2 I get an error in supervisor.log that the cron service stopped multiple times and entered a FATAL state. However cron seems to be running in top and executing cronjobs normally. Thanks for this! – (/users/1846363/lephleg) (1,764 reputation) lephleg Commented (2017-01-14 23:13:50Z, License: CC BY-SA 3.0) Jan 14, 2017 at 23:13 Yes, same thing happened to me as well, but it works as normal, so don't need to bother. – (/users/5726427/sagar-ghuge) (231 reputation) Sagar Ghuge Commented (2017-02-06 10:42:41Z, License: CC BY-SA 3.0) Feb 6, 2017 at 10:42 (Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.) Add a comment | (Expand to show all comments on this post) This answer is useful (70:3:31e,16:aedb3d3b789cc4fa,10:1725308455,16:1a787994d3e0676c,8:61356024,4319786129b23d116b195235072caf1b2ae9128db7bf29de9db31a0b62e54f24) 5 (This answer is not useful) (70:3:31e,16:7d6cef001026300c,10:1725308455,16:d3446c2341a36cb4,8:61356024,eb331faea096b3336d7ead548b21178361a94d1399fa8403395c4e365a823a10) Save this answer. (Loading when this answer was accepted…) (/posts/61356024/timeline) Show activity on this post. From above examples I created this combination: Alpine Image & Edit Using Crontab in Nano (I hate vi) FROM alpine RUN apk update RUN apk add curl nano ENV EDITOR=/usr/bin/nano # start crond with log level 8 in foreground, output to stderr CMD ["crond" , "-f" , "-d" , "8" ] # Shell Access # docker exec -it /bin/sh # Example Cron Entry # crontab -e # * * * * * echo hello > /proc/1/fd/1 2>/proc/1/fd/2 # DATE/TIME WILL BE IN UTC (/a/61356024) (Short permalink to this answer) Share Share a link to this answer Copy link (https://creativecommons.org/licenses/by-sa/4.0/) (The current license for this post: CC BY-SA 4.0) CC BY-SA 4.0 Follow (70:3:31e,16:53c2c59f8809a553,10:1725308455,16:1b74db3f74d23f20,8:61356024,d33316bd84ad3382689afa7e7439913b74699744e8059e8b9fc3207482911ded) Follow this answer to receive notifications answered (2020-04-22 01:54:09Z) Apr 22, 2020 at 1:54 (/users/8600209/dan-watts) (Dan Watts's user avatar) (/users/8600209/dan-watts) Dan Watts Dan Watts (reputation score) 109 (1 silver badge) 1 1 silver badge (3 bronze badges) 3 3 bronze badges (Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.) Add a comment | (Expand to show all comments on this post) This answer is useful (70:3:31e,16:ca88e4b5e9d0372a,10:1725308455,16:fa7e2495603f6117,8:61835762,dc3dfa6ece178d09061cb91711a6461d6f63455267e99e02f52336b8e613b3e0) 5 (This answer is not useful) (70:3:31e,16:566453a3ac6dd616,10:1725308455,16:63741e1e201883c8,8:61835762,9430789f21e5bcece3d370a17dba48fc778903461e1ee894af48ad17fce65f3d) Save this answer. (Loading when this answer was accepted…) (/posts/61835762/timeline) Show activity on this post. Setup a cron in parallel to a one-time job Create a script file, say run.sh, with the job that is supposed to run periodically. #!/bin/bash timestamp=`date +%Y/%m/%d-%H:%M:%S` echo "System path is $PATH at $timestamp " Save and exit. Use Entrypoint instead of CMD f you have multiple jobs to kick in during docker containerization, use the entrypoint file to run them all. Entrypoint file is a script file that comes into action when a docker run command is issued. So, all the steps that we want to run can be put in this script file. For instance, we have 2 jobs to run: Run once job : echo “Docker container has been started” Run periodic job : run.sh Create entrypoint.sh #!/bin/bash # Start the run once job. echo "Docker container has been started" # Setup a cron schedule echo "* * * * * /run.sh >> /var/log/cron.log 2>&1 # This extra line makes it a valid cron" > scheduler.txt crontab scheduler.txt cron -f Let’s understand the crontab that has been set up in the file * * * * * : Cron schedule; the job must run every minute. You can update the schedule based on your requirement. /run.sh : The path to the script file which is to be run periodically /var/log/cron.log : The filename to save the output of the scheduled cron job. 2>&1 : The error logs(if any) also will be redirected to the same output file used above. Note : Do not forget to add an extra new line, as it makes it a valid cron. Scheduler.txt : the complete cron setup will be redirected to a file. Using System/User specific environment variables in cron My actual cron job was expecting most of the arguments as the environment variables passed to the docker run command. But, with bash, I was not able to use any of the environment variables that belongs to the system or the docker container. Then, this came up as a walkaround to this problem: Add the following line in the entrypoint.sh declare -p | grep -Ev 'BASHOPTS|BASH_VERSINFO|EUID|PPID|SHELLOPTS|UID' > /container.env Update the cron setup and specify- SHELL=/bin/bash BASH_ENV=/container.env At last, your entrypoint.sh should look like #!/bin/bash # Start the run once job. echo "Docker container has been started" declare -p | grep -Ev 'BASHOPTS|BASH_VERSINFO|EUID|PPID|SHELLOPTS|UID' > /container.env # Setup a cron schedule echo "SHELL=/bin/bash BASH_ENV=/container.env * * * * * /run.sh >> /var/log/cron.log 2>&1 # This extra line makes it a valid cron" > scheduler.txt crontab scheduler.txt cron -f Last but not the least: Create a Dockerfile FROM ubuntu:16.04 MAINTAINER Himanshu Gupta # Install cron RUN apt-get update && apt-get install -y cron # Add files ADD run.sh /run.sh ADD entrypoint.sh /entrypoint.sh RUN chmod +x /run.sh /entrypoint.sh ENTRYPOINT /entrypoint.sh That’s it. Build and run the Docker image! (/a/61835762) (Short permalink to this answer) Share Share a link to this answer Copy link (https://creativecommons.org/licenses/by-sa/4.0/) (The current license for this post: CC BY-SA 4.0) CC BY-SA 4.0 Follow (70:3:31e,16:15c30c0c7b8f81ca,10:1725308455,16:819fe720a7b6c92e,8:61835762,c98ed8c9c387a30adc2ad5499012952a736653e2c0d32654f78fc5fafb4f89a5) Follow this answer to receive notifications (/posts/61835762/revisions) (show all edits to this post) edited (2023-01-16 10:27:06Z) Jan 16, 2023 at 10:27 (/users/874188/tripleee) (tripleee's user avatar) (/users/874188/tripleee) tripleee (reputation score 186,417) 186k (36 gold badges) 36 36 gold badges (296 silver badges) 296 296 silver badges (344 bronze badges) 344 344 bronze badges answered (2020-05-16 11:05:42Z) May 16, 2020 at 11:05 (/users/3620633/himanshuiiitian) (himanshuIIITian's user avatar) (/users/3620633/himanshuiiitian) himanshuIIITian himanshuIIITian (reputation score) 6,075 (6 gold badges) 6 6 gold badges (52 silver badges) 52 52 silver badges (70 bronze badges) 70 70 bronze badges 3 (number of 'useful comment' votes received) 1 @himanshuIIITian I tried this, the issue is that the script of the "run once job" is never returning and also the corn -f is not returning so... this is not working for me, any ideas? thanks – (/users/334181/doron-levi) (468 reputation) Doron Levi Commented (2020-05-19 08:54:17Z, License: CC BY-SA 4.0) May 19, 2020 at 8:54 @DoronLevi - can you please share some logs to look into the issue? Or you can check the whole code from here - (https://github.com/nehabhardwaj01/docker-cron) github.com/nehabhardwaj01/docker-cron – (/users/3620633/himanshuiiitian) (6,075 reputation) himanshuIIITian Commented (2020-05-19 10:28:53Z, License: CC BY-SA 4.0) May 19, 2020 at 10:28 (this comment was edited 1 time) (number of 'useful comment' votes received) 1 You don't need ENTRYPOINT to run multiple commands. And generally the description/solution seems too complex. – (/users/52499/x-yuri) (18,275 reputation) x-yuri Commented (2023-02-05 15:32:17Z, License: CC BY-SA 4.0) Feb 5, 2023 at 15:32 (Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.) Add a comment | (Expand to show all comments on this post) This answer is useful (70:3:31e,16:ca8ace5b97f55a4b,10:1725308455,16:d15b7d55b7123c42,8:62877733,618051bcb56848b421bd2f814c106f9461043997671520ec3c94235fcff17eb3) 5 (This answer is not useful) (70:3:31e,16:86a4787b17ba13bc,10:1725308455,16:b855f626bbbd8dd0,8:62877733,c94bb9d5e969e80f0bad0f58193f730ddc4e12a0806391a84643f2a33d112d18) Save this answer. (Loading when this answer was accepted…) (/posts/62877733/timeline) Show activity on this post. Here's my docker-compose based solution: cron: image: alpine:3.10 command : crond -f -d 8 depends_on: - servicename volumes: - './conf/cron:/etc/crontabs/root:z' restart: unless-stopped the lines with cron entries are on the ./conf/cron file. Note: this won't run commands that aren't in the alpine image. Also, output of the tasks apparently won't appear in docker logs . (/a/62877733) (Short permalink to this answer) Share Share a link to this answer Copy link (https://creativecommons.org/licenses/by-sa/4.0/) (The current license for this post: CC BY-SA 4.0) CC BY-SA 4.0 Follow (70:3:31e,16:e64f1fad99c291bb,10:1725308455,16:f7b0a137988c42b5,8:62877733,5e7c76e09fe4eff5d157a1f7f52f8dbefbc01b61963d5fbc5df9096796082bc5) Follow this answer to receive notifications (/posts/62877733/revisions) (show all edits to this post) edited (2023-02-05 15:28:36Z) Feb 5, 2023 at 15:28 (/users/52499/x-yuri) (x-yuri's user avatar) (/users/52499/x-yuri) x-yuri (reputation score 18,275) 18.3k (15 gold badges) 15 15 gold badges (121 silver badges) 121 121 silver badges (172 bronze badges) 172 172 bronze badges answered (2020-07-13 14:12:47Z) Jul 13, 2020 at 14:12 (/users/798677/that-brazilian-guy) (That Brazilian Guy's user avatar) (/users/798677/that-brazilian-guy) That Brazilian Guy That Brazilian Guy (reputation score) 3,491 (6 gold badges) 6 6 gold badges (32 silver badges) 32 32 silver badges (51 bronze badges) 51 51 bronze badges (Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.) Add a comment | (Expand to show all comments on this post) This answer is useful (70:3:31e,16:9f0b94fa20d0c1ab,10:1725308455,16:6adca5f9f5d6ba20,8:69255890,8054218d74cede8f85c30365acfee60d35b6030f5b94929688354936065d2253) 4 (This answer is not useful) (70:3:31e,16:4008ad00f25ed99c,10:1725308455,16:389ad7130694a034,8:69255890,28c27b28ccfd2b69d5bbbd5235302ae363e0be70667caad6fb473abc4dbecafe) Save this answer. (Loading when this answer was accepted…) (/posts/69255890/timeline) Show activity on this post. This question have a lot of answers, but some are complicated and another has some drawbacks. I try to explain the problems and try to deliver a solution. cron-entrypoint.sh : #!/bin/bash # copy machine environment variables to cron environment printenv | cat - /etc/crontab > temp && mv temp /etc/crontab ## validate cron file crontab /etc/crontab # cron service with SIGTERM support service cron start trap "service cron stop; exit" SIGINT SIGTERM # just dump your logs to std output tail -f \ /app/storage/logs/laravel.log \ /var/log/cron.log \ & wait $! Problems solved environment variables are not available on cron environment (like env vars or kubernetes secrets) stop when crontab file is not valid stop gracefully cron jobs when machine receive an SIGTERM signal For context, I use previous script on Kubernetes with Laravel app. (/a/69255890) (Short permalink to this answer) Share Share a link to this answer Copy link (https://creativecommons.org/licenses/by-sa/4.0/) (The current license for this post: CC BY-SA 4.0) CC BY-SA 4.0 Follow (70:3:31e,16:1c90f807e46e19ca,10:1725308455,16:2c950b089abd2456,8:69255890,2248572934754436086681349e743cca885e99a00ed24f5249a92e23a766ec33) Follow this answer to receive notifications answered (2021-09-20 14:10:31Z) Sep 20, 2021 at 14:10 (/users/860878/pablorsk) (pablorsk's user avatar) (/users/860878/pablorsk) pablorsk pablorsk (reputation score) 4,198 (1 gold badge) 1 1 gold badge (33 silver badges) 33 33 silver badges (38 bronze badges) 38 38 bronze badges 2 If I run docker stop with this setup, nothing happens, i.e. service cron stop doesn't get executed. If I run the latter manually from within the container, the cron process stops immediately instead of waiting for the cronjobs. cronjobs will still complete their run, so that may be fine. When they are done, the container does not stop either though. What am I missing? – (/users/1665966/johnson-145) (2,024 reputation) Johnson_145 Commented (2022-06-17 14:19:51Z, License: CC BY-SA 4.0) Jun 17, 2022 at 14:19 Got it working now. I think the trap handler wasn't triggered, because I defined my entryscript as CMD "/bin/sh" ENTRYPOINT /entrypoint.sh instead of ENTRYPOINT ["/entrypoint.sh"] . That way, it got wrapped in another shell which didn't pass the signals through. I had to do some further steps to actually wait for running cronjobs to finish. Elaborating on your answer (https://stackoverflow.com/a/72735422/1665966) over here . – (/users/1665966/johnson-145) (2,024 reputation) Johnson_145 Commented (2022-06-23 19:14:48Z, License: CC BY-SA 4.0) Jun 23, 2022 at 19:14 (Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.) Add a comment | (Expand to show all comments on this post) This answer is useful (70:3:31e,16:594baa8bf20dda20,10:1725308455,16:ca89734ecdbad00b,8:77027390,e80018021075aa23dc894b8f389664e7138f2dea564c5eb7d12e824ccab10fc4) 3 (This answer is not useful) (70:3:31e,16:cbc22c0890f568e4,10:1725308455,16:9a66f8620d2bcef6,8:77027390,4c18f1197c8a08b72a5b89d3a30b6e2df507ac004c644f6ab912bdfbb8b4e2b2) Save this answer. (Loading when this answer was accepted…) (/posts/77027390/timeline) Show activity on this post. This code has successfully worked for me. I placed the script.sh file inside the project folder, and both the script.sh and main.py Docker files are located at the same directory level. I made the following modifications to the Docker file in order to execute the Cron-job within the Docker container. Script.sh file #!/bin/bash # script.sh sleep 0 wget http://127.0.0.1:8081/v1.0/recommend/cronjob Docker File # Choose our version of Python FROM python:3.11 # Set up a working directory WORKDIR /opt/myapp # Copy the code into the working directory COPY . /opt/myapp # Upgrade PIP RUN pip install --upgrade pip # Install requirements RUN pip install -r requirements.txt EXPOSE 8081 # Start both FastAPI and cron jobs ADD script.sh /root/script.sh RUN chmod 0644 /root/script.sh RUN apt-get update RUN apt-get -y install cron RUN crontab -l | { cat ; echo "30 2 * * * bash /root/script.sh" ; } | crontab - # Tell uvicorn to start spin up our code, which will be running inside the container now CMD ["bash" , "-c" , "cron && uvicorn main:app --host 0.0.0.0 --port 8081" ] (/a/77027390) (Short permalink to this answer) Share Share a link to this answer Copy link (https://creativecommons.org/licenses/by-sa/4.0/) (The current license for this post: CC BY-SA 4.0) CC BY-SA 4.0 Follow (70:3:31e,16:9ff7650ba73fb192,10:1725308455,16:8ac16c63efdf86ea,8:77027390,0fc95a9179557e0a3f97945b1db6b225caeac3245856854159c410f0881d25e7) Follow this answer to receive notifications answered (2023-09-02 07:39:14Z) Sep 2, 2023 at 7:39 (/users/2756408/buddhika-lakshan) (Buddhika Lakshan's user avatar) (/users/2756408/buddhika-lakshan) Buddhika Lakshan Buddhika Lakshan (reputation score) 320 (4 silver badges) 4 4 silver badges (14 bronze badges) 14 14 bronze badges (Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.) Add a comment | (Expand to show all comments on this post) This answer is useful (70:3:31e,16:f4f359a5aaeccf22,10:1725308455,16:ff98e9af3c3bebee,8:74818352,a15a84d233832e232610fbb39dcb9a3b5f7aef4b5f3feb44a652376135ae5066) 2 (This answer is not useful) (70:3:31e,16:e6f644c2f0d942ce,10:1725308455,16:22768ed36da84771,8:74818352,4a608f3cb57877194e602a43d80851b796fd1d18d97a84a36e6626a64834e901) Save this answer. (Loading when this answer was accepted…) (/posts/74818352/timeline) Show activity on this post. With multiple jobs and various dependencies like zsh and curl , this is a good approach while also combining the best practices from other answers. Bonus: This does NOT require you to set +x execution permissions on myScript.sh , which can be easy to miss in a new environment. cron.dockerfile FROM ubuntu:latest # Install dependencies RUN apt-get update && apt-get -y install \ cron \ zsh \ curl; # Setup multiple jobs with zsh and redirect outputs to docker logs RUN (echo "\ * * * * * zsh -c 'echo " Hello World"' 1> /proc/1/fd/1 2>/proc/1/fd/2 \n\ * * * * * zsh /myScript.sh 1> /proc/1/fd/1 2>/proc/1/fd/2 \n\ " ) | crontab # Run cron in forground, so docker knows the task is running CMD ["cron" , "-f" ] Integrate this with docker compose like so: docker-compose.yml services: cron: build: context: . dockerfile: ./cron.dockerfile volumes: - ./myScript.sh:/myScript.sh Keep in mind that you need to docker compose build cron when you change contents of the cron.dockerfile , but changes to myScript.sh will be reflected right away as it's mounted in compose. (/a/74818352) (Short permalink to this answer) Share Share a link to this answer Copy link (https://creativecommons.org/licenses/by-sa/4.0/) (The current license for this post: CC BY-SA 4.0) CC BY-SA 4.0 Follow (70:3:31e,16:0c4f6857d6ad70f2,10:1725308455,16:99b1e9655e089fd5,8:74818352,7744ee3600700153eb8e1f93a503282995e7f459304562a5fe773afec82ecf40) Follow this answer to receive notifications answered (2022-12-15 22:42:26Z) Dec 15, 2022 at 22:42 (/users/1175285/cwista) (Cwista's user avatar) (/users/1175285/cwista) Cwista Cwista (reputation score) 359 (1 silver badge) 1 1 silver badge (11 bronze badges) 11 11 bronze badges (Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.) Add a comment | (Expand to show all comments on this post) This answer is useful (70:3:31e,16:00be9b3ac75a0902,10:1725308455,16:4ff46826a6b47a93,8:63000862,681d2f3355b5c093602eb0a4c58ca560257224718d99b23b5012a97139acf551) 2 (This answer is not useful) (70:3:31e,16:5bc3785cbcb0f791,10:1725308455,16:6fbf69c710784234,8:63000862,10981a66854214ad55afc5ab8b62298c7720a91c7ac39142deea281cf8df9e9c) Save this answer. (Loading when this answer was accepted…) (/posts/63000862/timeline) Show activity on this post. this line was the one that helped me run my pre-scheduled task. ADD mycron/root /etc/cron.d/root RUN chmod 0644 /etc/cron.d/root RUN crontab /etc/cron.d/root RUN touch /var/log/cron.log CMD ( cron -f -l 8 & ) && apache2-foreground # <-- run cron --> My project run inside: FROM php:7.2-apache But: if cron dies, the container (https://gist.github.com/x-yuri/f17b2cefc6e673e979850d79b265f345) keeps running . (/a/63000862) (Short permalink to this answer) Share Share a link to this answer Copy link (https://creativecommons.org/licenses/by-sa/4.0/) (The current license for this post: CC BY-SA 4.0) CC BY-SA 4.0 Follow (70:3:31e,16:a4f856128faf5f16,10:1725308455,16:bce7ad4cbd58a1ac,8:63000862,e1780e7a2cb22e42b2d3c5f8970ce1e6bdb6a54fd1e49736cdd2824032b02e59) Follow this answer to receive notifications (/posts/63000862/revisions) (show all edits to this post) edited (2023-02-05 15:34:50Z) Feb 5, 2023 at 15:34 (/users/52499/x-yuri) (x-yuri's user avatar) (/users/52499/x-yuri) x-yuri (reputation score 18,275) 18.3k (15 gold badges) 15 15 gold badges (121 silver badges) 121 121 silver badges (172 bronze badges) 172 172 bronze badges answered (2020-07-20 17:30:43Z) Jul 20, 2020 at 17:30 (/users/7535525/santiago-vasquez) (Santiago Vasquez's user avatar) (/users/7535525/santiago-vasquez) Santiago Vasquez Santiago Vasquez (reputation score) 147 (1 silver badge) 1 1 silver badge (10 bronze badges) 10 10 bronze badges (Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.) Add a comment | (Expand to show all comments on this post) This answer is useful (70:3:31e,16:caf10e8c664e2b5e,10:1725308455,16:9c3eeaf4ca975c93,8:54794026,0d07da51b42c2cd67fa39917ed418f42566b1e6df373d33edd7f17812516d86d) 1 (This answer is not useful) (70:3:31e,16:c866bcb427f7b225,10:1725308455,16:5679b55f6e1ff5c0,8:54794026,cc82b5160bb23cc83cc490032f0846146eacdadec36c5512ca41d264657c7005) Save this answer. (Loading when this answer was accepted…) (/posts/54794026/timeline) Show activity on this post. So, my problem was the same. The fix was to change the command section in the docker-compose.yml . From command: crontab /etc/crontab && tail -f /etc/crontab To command: crontab /etc/crontab command: tail -f /etc/crontab The problem was the '&&' between the commands. After deleting this, it was all fine. (/a/54794026) (Short permalink to this answer) Share Share a link to this answer Copy link (https://creativecommons.org/licenses/by-sa/4.0/) (The current license for this post: CC BY-SA 4.0) CC BY-SA 4.0 Follow (70:3:31e,16:c751042f9a1cc115,10:1725308455,16:f7c95e57d17508db,8:54794026,f3675d7f792987c2c5bced55ad681af1d5496bedca4060632facef0029d4fe76) Follow this answer to receive notifications (/posts/54794026/revisions) (show all edits to this post) edited (2019-02-21 11:28:37Z) Feb 21, 2019 at 11:28 answered (2019-02-20 19:36:15Z) Feb 20, 2019 at 19:36 (/users/8340062/random2137) (random2137's user avatar) (/users/8340062/random2137) random2137 random2137 (reputation score) 138 (1 gold badge) 1 1 gold badge (2 silver badges) 2 2 silver badges (15 bronze badges) 15 15 bronze badges 1 The second command overrides the first one. That is, having 2 command keys equals to having only the last one (the last one wins). – (/users/52499/x-yuri) (18,275 reputation) x-yuri Commented (2023-02-05 15:40:04Z, License: CC BY-SA 4.0) Feb 5, 2023 at 15:40 (Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.) Add a comment | (Expand to show all comments on this post) This answer is useful (70:3:31e,16:118be346270a5044,10:1725308455,16:bfb4e9932c437da1,8:72735422,228212b7be0887403390f3c3d88661e20f9859d34196aa45ecce1a9792917287) 1 (This answer is not useful) (70:3:31e,16:67cde25fba2ad561,10:1725308455,16:2ec03571d220f97f,8:72735422,bf2e771c4b0bd3dde2f1dcacfa29d9b84f4c590ab5b7bd075092746066b97993) Save this answer. (Loading when this answer was accepted…) (/posts/72735422/timeline) Show activity on this post. Focusing on gracefully stopping the cronjobs when receiving SIGTERM or SIGQUIT signals (e.g. when running docker stop ). That's not too easy. By default, the cron process just got killed without paying attention to running cronjobs. I'm elaborating on (https://stackoverflow.com/a/69255890/1665966) pablorsk's answer : Dockerfile : FROM ubuntu:latest RUN apt-get update \ && apt-get -y install cron procps \ && rm -rf /var/lib/apt/lists/* # Copy cronjobs file to the cron.d directory COPY cronjobs /etc/cron.d/cronjobs # Give execution rights on the cron job RUN chmod 0644 /etc/cron.d/cronjobs # similarly prepare the default cronjob scripts COPY run_cronjob.sh /root/run_cronjob.sh RUN chmod +x /root/run_cronjob.sh COPY run_cronjob_without_log.sh /root/run_cronjob_without_log.sh RUN chmod +x /root/run_cronjob_without_log.sh # Apply cron job RUN crontab /etc/cron.d/cronjobs # to gain access to environment variables, we need this additional entrypoint script COPY entrypoint.sh /entrypoint.sh RUN chmod +x /entrypoint.sh # optionally, change received signal from SIGTERM TO SIGQUIT #STOPSIGNAL SIGQUIT # Run the command on container startup ENTRYPOINT ["/entrypoint.sh" ] entrypoint.sh : #!/bin/bash # make global environment variables available within crond, too printenv | grep -v "no_proxy" >> /etc/environment # SIGQUIT/SIGTERM-handler term_handler () { echo 'stopping cron' service cron stop echo 'stopped' echo 'waiting' x=$(($(ps u -C run_cronjob.sh | wc -l)-1 )) xold=0 while [ "$x " -gt 0 ] do if [ "$x " != "$xold " ]; then echo "Waiting for $x running cronjob(s):" ps u -C run_cronjob.sh xold=$x sleep 1 fi x=$(($(ps u -C run_cronjob.sh | wc -l)-1 )) done echo 'done waiting' exit 143; # 128 + 15 -- SIGTERM } # cron service with SIGTERM and SIGQUIT support service cron start trap "term_handler" QUIT TERM # endless loop while true do tail -f /dev/null & wait ${!} done cronjobs * * * * * ./run_cronjob.sh cron1 */2 * * * * ./run_cronjob.sh cron2 */3 * * * * ./run_cronjob.sh cron3 Assuming you wrap all your cronjobs in a run_cronjob.sh script. That way, you can execute arbitrary code for which shutdown will wait gracefully. run_cronjobs.sh (optional helper script to keep cronjob definitions clean) #!/bin/bash DIR_INCL="${BASH_SOURCE%/*} " if [[ ! -d "$DIR_INCL " ]]; then DIR_INCL="$PWD " ; fi cd "$DIR_INCL " # redirect all cronjob output to docker ./run_cronjob_without_log.sh "$@ " > /proc/1/fd/1 2>/proc/1/fd/2 run_cronjob_without_log.sh your_actual_cronjob_src() Btw, when receiving a SIGKILL the container still shut downs immediately. That way you can use a command like docker-compose stop -t 60 cron-container to wait 60s for cronjobs to finish gracefully, but still terminate them for sure after the timeout. (/a/72735422) (Short permalink to this answer) Share Share a link to this answer Copy link (https://creativecommons.org/licenses/by-sa/4.0/) (The current license for this post: CC BY-SA 4.0) CC BY-SA 4.0 Follow (70:3:31e,16:f430332568ac062f,10:1725308455,16:31118c9983f4514f,8:72735422,2c5317d4a033eeb5a03408e312ebca9cd03e8f636deb63fead6871faa8cd81ee) Follow this answer to receive notifications (/posts/72735422/revisions) (show all edits to this post) edited (2022-06-23 19:54:23Z) Jun 23, 2022 at 19:54 answered (2022-06-23 19:10:16Z) Jun 23, 2022 at 19:10 (/users/1665966/johnson-145) (Johnson_145's user avatar) (/users/1665966/johnson-145) Johnson_145 Johnson_145 (reputation score) 2,024 (1 gold badge) 1 1 gold badge (20 silver badges) 20 20 silver badges (28 bronze badges) 28 28 bronze badges (Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.) Add a comment | (Expand to show all comments on this post) This answer is useful (70:3:31e,16:e5518192f280345b,10:1725308455,16:53bd5ac3fb0db59b,8:74854037,29f6b9959be16514a101a76caebaa1a8444d65ff3981c6d17408d5c8626ed860) 1 (This answer is not useful) (70:3:31e,16:db64f9c91afafbd7,10:1725308455,16:d7cf2c60b58c91e8,8:74854037,198670ccc95e0c4b3d35cb52136f98e16a88249113f76f2b931ff14f4b6026a5) Save this answer. (Loading when this answer was accepted…) (/posts/74854037/timeline) Show activity on this post. All the answers require root access inside the container because 'cron' itself requests for UID 0. To request root acces (e.g. via sudo) is against docker best practices. I used (https://github.com/gjcarneiro/yacron) https://github.com/gjcarneiro/yacron to manage scheduled tasks. (/a/74854037) (Short permalink to this answer) Share Share a link to this answer Copy link (https://creativecommons.org/licenses/by-sa/4.0/) (The current license for this post: CC BY-SA 4.0) CC BY-SA 4.0 Follow (70:3:31e,16:89c537849baa1f9f,10:1725308455,16:4bf3acaa6b3d7ab7,8:74854037,675bddbe1d4c891d1d89cc40486414c3f87df2a4e39bf6af7b5c7f5f20fad7f5) Follow this answer to receive notifications answered (2022-12-19 17:37:43Z) Dec 19, 2022 at 17:37 (/users/8530841/andreas-wittig) (Andreas Wittig's user avatar) (/users/8530841/andreas-wittig) Andreas Wittig Andreas Wittig (reputation score) 176 (8 bronze badges) 8 8 bronze badges (Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.) Add a comment | (Expand to show all comments on this post) This answer is useful (70:3:31e,16:f9f097c18d241e30,10:1725308455,16:9d48ab9afb8395a3,8:47534731,588695a3b8018552eed91a4c3b73ea9defd7cc8447f89af349c60f664bb054a9) 1 (This answer is not useful) (70:3:31e,16:a4bdb408be98811d,10:1725308455,16:dba7110af71d8c86,8:47534731,564114dc83d6fea25fcdd690ca02bfb9b7ad6d05ff4f809968b614b639b7af84) Save this answer. (Loading when this answer was accepted…) (/posts/47534731/timeline) Show activity on this post. When running on some trimmed down images that restrict root access, I had to add my user to the sudoers and run as sudo cron FROM node:8.6.0 RUN apt-get update && apt-get install -y cron sudo COPY crontab /etc/cron.d/my-cron RUN chmod 0644 /etc/cron.d/my-cron RUN touch /var/log/cron.log # Allow node user to start cron daemon with sudo RUN echo 'node ALL=NOPASSWD: /usr/sbin/cron' >>/etc/sudoers ENTRYPOINT sudo cron && tail -f /var/log/cron.log Maybe that helps someone But: if cron dies, the container (https://gist.github.com/x-yuri/f17b2cefc6e673e979850d79b265f345) keeps running . (/a/47534731) (Short permalink to this answer) Share Share a link to this answer Copy link (https://creativecommons.org/licenses/by-sa/4.0/) (The current license for this post: CC BY-SA 4.0) CC BY-SA 4.0 Follow (70:3:31e,16:5e14beb1be70ca65,10:1725308455,16:7f711e0a63af8910,8:47534731,d8136afbff40a248327345628ebd74690c1472fd73cbce2e190495171c42e73a) Follow this answer to receive notifications (/posts/47534731/revisions) (show all edits to this post) edited (2023-02-05 15:35:44Z) Feb 5, 2023 at 15:35 (/users/52499/x-yuri) (x-yuri's user avatar) (/users/52499/x-yuri) x-yuri (reputation score 18,275) 18.3k (15 gold badges) 15 15 gold badges (121 silver badges) 121 121 silver badges (172 bronze badges) 172 172 bronze badges answered (2017-11-28 15:05:57Z) Nov 28, 2017 at 15:05 (/users/266763/senica-gonzalez) (Senica Gonzalez's user avatar) (/users/266763/senica-gonzalez) Senica Gonzalez Senica Gonzalez (reputation score) 8,154 (16 gold badges) 16 16 gold badges (68 silver badges) 68 68 silver badges (110 bronze badges) 110 110 bronze badges 1 I believe the node image uses the node user; so maybe you needed to add permissions for that user – (/users/488784/bozdoz) (12,800 reputation) bozdoz Commented (2020-05-08 00:44:39Z, License: CC BY-SA 4.0) May 8, 2020 at 0:44 (Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.) Add a comment | (Expand to show all comments on this post) This answer is useful (70:3:31e,16:06630d91623dd03f,10:1725308455,16:b9a729286f5275bb,8:78705655,608e8fcd95e600b0b8a69f1b8e96b3afd32233a2e270f248d67cd5f11bc8f54a) 1 (This answer is not useful) (70:3:31e,16:1d483b892326e32d,10:1725308455,16:e5c9223b76c42b85,8:78705655,6052ee2de533fa35102edf0b98a5e13f1aa6f4094a75898994a52f75d64b2615) Save this answer. (Loading when this answer was accepted…) (/posts/78705655/timeline) Show activity on this post. As a quick workaround for tasks that simply need to be executed in regular intervals you could also use the HEALTHCHECK instruction. The health status then shows the most recent result. HEALTHCHECK --interval=60m --timeout =5s command || exit 1 (https://docs.docker.com/reference/dockerfile/#healthcheck) Documentation (https://docs.docker.com/compose/compose-file/05-services/#healthcheck) syntax for docker-compose.yml (/a/78705655) (Short permalink to this answer) Share Share a link to this answer Copy link (https://creativecommons.org/licenses/by-sa/4.0/) (The current license for this post: CC BY-SA 4.0) CC BY-SA 4.0 Follow (70:3:31e,16:3db4acb2e6cc25db,10:1725308455,16:1fa2d0f7b9a2de01,8:78705655,1cd8343ffdd7689fe83f87cb5c1554fcb4e5587006beb4efad35d16873076ef7) Follow this answer to receive notifications answered (2024-07-04 07:33:23Z) Jul 4 at 7:33 (/users/2377238/chris-cm) (chris_cm's user avatar) (/users/2377238/chris-cm) chris_cm chris_cm (reputation score) 144 (13 bronze badges) 13 13 bronze badges (Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.) Add a comment | (Expand to show all comments on this post) This answer is useful (70:3:31e,16:0524bc2ed54ad19e,10:1725308455,16:fa8db2e5f2e57d8a,8:73625026,7b9d902590ad4563d1ebc88a57c82268fe55fc6cdbce1fc4b088fddcec6b398e) 0 (This answer is not useful) (70:3:31e,16:393bbc0017ee57b1,10:1725308455,16:08952d89e7fed3c0,8:73625026,546a7f3537cc0bdea78ff806d1100aaa381727eb8a6b8b0e26683130a933462b) Save this answer. (Loading when this answer was accepted…) (/posts/73625026/timeline) Show activity on this post. Evidently, it is possible to run cron as a process inside the container (under root user) alongside other processes , using ENTRYPOINT statement in Dockerfile with start.sh script what includes line process cron start . More info (https://docs.docker.com/config/containers/multi-service_container/) here #!/bin/bash # copy environment variables for local use env >> etc/environment # start cron service service cron start # start other service service other start #... (/a/73625026) (Short permalink to this answer) Share Share a link to this answer Copy link (https://creativecommons.org/licenses/by-sa/4.0/) (The current license for this post: CC BY-SA 4.0) CC BY-SA 4.0 Follow (70:3:31e,16:f85851917137b3ed,10:1725308455,16:63946ee6da06c86f,8:73625026,c7cf63128e74d5386633850417660f098932e8735d5ccb1a80cb5b2c793fdb6c) Follow this answer to receive notifications answered (2022-09-06 16:16:36Z) Sep 6, 2022 at 16:16 (/users/10267027/vitali-khvatkov) (Vitali Khvatkov's user avatar) (/users/10267027/vitali-khvatkov) Vitali Khvatkov Vitali Khvatkov (reputation score) 1 (Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.) Add a comment | (Expand to show all comments on this post) This answer is useful (70:3:31e,16:ef883a92d1a0d33a,10:1725308455,16:37202f00c4010c35,8:73700588,a63d256dd34e2d949c2cb7ce028df51850d876a9d6eb2b8a8da812d5e681b2e5) 0 (This answer is not useful) (70:3:31e,16:e7b3d9538a82ac35,10:1725308455,16:d771369cd8c37fd2,8:73700588,ef87e9de576c5d2ae918495abff0dcc915ab1126315b8da4407b5798b8d8ef8f) Save this answer. (Loading when this answer was accepted…) (/posts/73700588/timeline) Show activity on this post. If your image doesn't contain any daemon (so it's only the short-running script or process), you may also consider starting your cron from outside , by simply defining a LABEL with the cron information, plus the scheduler itself. This way, your default container state is "exited". If you have multiple scripts, this may result in a lower footprint on your system than having multiple parallel-running cron instances. See: (https://github.com/JaciBrunning/docker-cron-label) https://github.com/JaciBrunning/docker-cron-label Example docker-compose.yaml: version: '3.8' # Example application of the cron image services: cron: image: jaci/cron-label:latest volumes: - "/var/run/docker.sock:/var/run/docker.sock" - "/etc/localtime:/etc/localtime:ro" hello: image: hello-world restart: "no" labels: - "cron.schedule=* * * * * " (/a/73700588) (Short permalink to this answer) Share Share a link to this answer Copy link (https://creativecommons.org/licenses/by-sa/4.0/) (The current license for this post: CC BY-SA 4.0) CC BY-SA 4.0 Follow (70:3:31e,16:96d29b655cccd827,10:1725308455,16:f972443551e9cc2c,8:73700588,8c854e5ebbcd72ac64bae88ab72156b869ebad595b6d2da890a99fbeff20db33) Follow this answer to receive notifications (/posts/73700588/revisions) (show all edits to this post) edited (2022-09-13 09:51:17Z) Sep 13, 2022 at 9:51 answered (2022-09-13 09:29:59Z) Sep 13, 2022 at 9:29 (/users/1353930/daniel-alder) (Daniel Alder's user avatar) (/users/1353930/daniel-alder) Daniel Alder Daniel Alder (reputation score) 5,265 (2 gold badges) 2 2 gold badges (49 silver badges) 49 49 silver badges (56 bronze badges) 56 56 bronze badges (Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.) Add a comment | (Expand to show all comments on this post) This answer is useful (70:3:31e,16:e8e20b631d75cb71,10:1725308455,16:c1676bb19f623714,8:74298715,4a059f68de08445440c8dbf8435934b9f3a3090b7c293a1580d16ed4e902e42a) 0 (This answer is not useful) (70:3:31e,16:14d98fb53ce97db9,10:1725308455,16:99c6236ab9e4e0ef,8:74298715,52142259f2da0c3780c0145a02ad238ed0fe0a7b866be20f079fee4fab59ad70) Save this answer. (Loading when this answer was accepted…) (/posts/74298715/timeline) Show activity on this post. I wanted to share a modification to the typical off of some of these other suggestions that I found more flexible. I wanted to enable changing the cron time with an environment variable and ended up adding an additional script that runs within my entrypoint.sh, but before the call to cron -f *updatecron.sh* #!/bin/sh #remove old cron files rm -rf /etc/cron.*/* #create a new formatted cron definition echo "$crondef [appname] >/proc/1/fd/1 2>/proc/1/fd/2" >> /etc/cron.d/restart-cron echo \ >> /etc/cron.d/restart-cron chmod 0644 /etc/cron.d/restart-cron crontab /etc/cron.d/restart-cron This removes any existing cron files, creates a new cronfile using an ENV variable of crondef, and then loads it. (/a/74298715) (Short permalink to this answer) Share Share a link to this answer Copy link (https://creativecommons.org/licenses/by-sa/4.0/) (The current license for this post: CC BY-SA 4.0) CC BY-SA 4.0 Follow (70:3:31e,16:b88e6c576ff28679,10:1725308455,16:61f4e483290a35ee,8:74298715,2c2eaa644040abb43c3c7a7b82c54c06c19efb80f2804cc11fb363596bbfae16) Follow this answer to receive notifications answered (2022-11-03 06:09:10Z) Nov 3, 2022 at 6:09 (/users/1626313/christopher-richmond) (Christopher Richmond's user avatar) (/users/1626313/christopher-richmond) Christopher Richmond Christopher Richmond (reputation score) 646 (5 silver badges) 5 5 silver badges (12 bronze badges) 12 12 bronze badges (Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.) Add a comment | (Expand to show all comments on this post) This answer is useful (70:3:31e,16:5606d09bdf378b01,10:1725308455,16:5d6c460c38cc8c07,8:74329776,824cb582efc00e2fae337d2a9595279b6adb9ff7f6f3588e7a6399428238d5b2) 0 (This answer is not useful) (70:3:31e,16:57826ba77b549cf7,10:1725308455,16:e87fee181dbcc19d,8:74329776,a7f4e0dda94682e78710c58c2f461445e34e8aaac0066c1bd706e442385cba92) Save this answer. (Loading when this answer was accepted…) (/posts/74329776/timeline) Show activity on this post. Our's was a nodejs application to be run as cron job and it was also dependent on environment variables. The below solution worked for us. Docker file: # syntax=docker/dockerfile:1 FROM node:12.18.1 ENV NODE_ENV=production COPY ["startup.sh" , "./" ] # Removed steps to build the node js application #--------------- Setup cron ------------------ # Install Cron RUN apt-get update RUN apt-get -y install cron # Run every day at 1AM #/proc/1/fd/1 2>/proc/1/fd/2 is used to redirect cron logs to standard output and standard error RUN (crontab -l ; echo "0 1 * * * /usr/local/bin/node /app/dist/index.js > /proc/1/fd/1 2>/proc/1/fd/2" ) | crontab #--------------- Start Cron ------------------ # Grant execution rights RUN chmod 755 startup.sh CMD ["./startup.sh" ] startup.sh: !/bin/bash echo "Copying env variables to /etc/environment so that it is available for cron jobs" printenv >> /etc/environment echo "Starting cron" cron -f (/a/74329776) (Short permalink to this answer) Share Share a link to this answer Copy link (https://creativecommons.org/licenses/by-sa/4.0/) (The current license for this post: CC BY-SA 4.0) CC BY-SA 4.0 Follow (70:3:31e,16:55efb02f7e87e8e4,10:1725308455,16:c511c2424bc90031,8:74329776,016b4b2c55bb101c313e27eba2fb61d40af576c78a80c72cbc1ceb641290f916) Follow this answer to receive notifications answered (2022-11-05 17:03:00Z) Nov 5, 2022 at 17:03 (/users/2742356/baga) (Baga's user avatar) (/users/2742356/baga) Baga Baga (reputation score) 1,422 (15 silver badges) 15 15 silver badges (24 bronze badges) 24 24 bronze badges (Use comments to ask for more information or suggest improvements. Avoid comments like “+1” or “thanks”.) Add a comment | (Expand to show all comments on this post) 1 (/questions/37458287/how-to-run-a-cron-job-inside-a-docker-container?page=2&tab=scoredesc#tab-top) (Go to page 2) 2 (/questions/37458287/how-to-run-a-cron-job-inside-a-docker-container?page=2&tab=scoredesc#tab-top) (Go to page 2) Next (/help/privileges/protect-questions) Highly active question . Earn 10 reputation (not counting the (https://meta.stackexchange.com/questions/141648/what-is-the-association-bonus-and-how-does-it-work) association bonus ) in order to answer this question. The reputation requirement helps protect this question from spam and non-answer activity. Not the answer you're looking for? Browse other questions tagged (/questions/tagged/docker) (show questions tagged 'docker') docker (/questions/tagged/cron) (show questions tagged 'cron') cron (/questions/tagged/containers) (show questions tagged 'containers') containers (/questions/tagged/sh) (show questions tagged 'sh') sh or (/questions/ask) ask your own question . The Overflow Blog (https://stackoverflow.blog/2024/08/30/mobile-observability-open-telemetry-embrace-ios-android-austin-emmons/?cb=1) (Mobile Observability: monitoring performance through cracked screens, old batteries, and crappy Wi-Fi) Mobile Observability: monitoring performance through cracked screens, old... Featured on Meta (Meta Stack Exchange) (https://meta.stackexchange.com/questions/401324/announcing-a-change-to-the-data-dump-process?cb=1) Announcing a change to the data-dump process (Meta Stack Exchange) (https://meta.stackexchange.com/questions/402121/bringing-clarity-to-status-tag-usage-on-meta-sites?cb=1) Bringing clarity to status tag usage on meta sites (Meta Stack Overflow) (https://meta.stackoverflow.com/questions/431331/what-does-a-new-user-need-in-a-homepage-experience-on-stack-overflow?cb=1) What does a new user need in a homepage experience on Stack Overflow? (Meta Stack Overflow) (https://meta.stackoverflow.com/questions/431273/feedback-requested-how-do-you-use-tag-hover-descriptions-for-curating-and-do-yo?cb=1) (Feedback requested: How do you use tag hover descriptions for curating and do you use the RSS feed feature?) Feedback requested: How do you use tag hover descriptions for curating and do... (Meta Stack Overflow) (https://meta.stackoverflow.com/questions/431399/staging-ground-reviewer-motivation?cb=1) Staging Ground Reviewer Motivation Linked (https://stackoverflow.com/q/75109640?lq=1) (Question score (upvotes - downvotes)) 3 (https://stackoverflow.com/questions/75109640/how-do-i-automatically-start-cron-on-docker-alpine?noredirect=1&lq=1) How do I automatically start cron on docker alpine? (https://stackoverflow.com/q/60456516?lq=1) (Question score (upvotes - downvotes)) 4 (https://stackoverflow.com/questions/60456516/how-can-i-automatically-start-cron-in-a-docker-container-that-is-running-php?noredirect=1&lq=1) How can I automatically start cron in a Docker container that is running PHP? (https://stackoverflow.com/q/51431604?lq=1) (Question score (upvotes - downvotes)) 4 (https://stackoverflow.com/questions/51431604/best-way-to-run-cron-in-docker-container?noredirect=1&lq=1) Best way to run cron in docker container (https://stackoverflow.com/q/58583067?lq=1) (Question score (upvotes - downvotes)) -1 (https://stackoverflow.com/questions/58583067/docker-container-crontab-not-running?noredirect=1&lq=1) Docker container crontab not running (https://stackoverflow.com/q/75595530?lq=1) (Question score (upvotes - downvotes)) 1 (https://stackoverflow.com/questions/75595530/cron-job-does-not-work-in-ubuntu-docker-container?noredirect=1&lq=1) Cron job does not work in ubuntu Docker container (https://stackoverflow.com/q/56725021?lq=1) (Question score (upvotes - downvotes)) 0 (https://stackoverflow.com/questions/56725021/the-crontab-not-running-inside-container?noredirect=1&lq=1) The crontab not running inside container (https://stackoverflow.com/q/56731029?lq=1) (Question score (upvotes - downvotes)) 0 (https://stackoverflow.com/questions/56731029/how-to-run-a-python-cron-application-with-docker?noredirect=1&lq=1) How to run a python cron application with docker? (https://stackoverflow.com/q/68457231?lq=1) (Question score (upvotes - downvotes)) 0 (https://stackoverflow.com/questions/68457231/crond-isnt-running-unless-i-ssh-to-the-container?noredirect=1&lq=1) crond isn't running unless I ssh to the container (https://stackoverflow.com/q/71688172?lq=1) (Question score (upvotes - downvotes)) 0 (https://stackoverflow.com/questions/71688172/the-docker-container-exited-after-running-with-cmd-start-cron?noredirect=1&lq=1) The Docker Container Exited After Running With CMD Start Cron (https://stackoverflow.com/q/71962774?lq=1) (Question score (upvotes - downvotes)) 0 (https://stackoverflow.com/questions/71962774/how-can-i-set-the-crond-for-docker?noredirect=1&lq=1) How can I set the crond for docker? (https://stackoverflow.com/questions/linked/37458287?lq=1) See more linked questions Related (https://stackoverflow.com/q/24319662?rq=1) (Question score (upvotes - downvotes)) 3457 (https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach?rq=1) From inside of a Docker container, how do I connect to the localhost of the machine? (https://stackoverflow.com/q/22907231?rq=1) (Question score (upvotes - downvotes)) 2513 (https://stackoverflow.com/questions/22907231/how-to-copy-files-from-host-to-docker-container?rq=1) How to copy files from host to Docker container? (https://stackoverflow.com/q/22049212?rq=1) (Question score (upvotes - downvotes)) 2929 (https://stackoverflow.com/questions/22049212/copying-files-from-docker-container-to-host?rq=1) Copying files from Docker container to host (https://stackoverflow.com/q/17157721?rq=1) (Question score (upvotes - downvotes)) 2216 (https://stackoverflow.com/questions/17157721/how-to-get-a-docker-containers-ip-address-from-the-host?rq=1) How to get a Docker container's IP address from the host (https://stackoverflow.com/q/30172605?rq=1) (Question score (upvotes - downvotes)) 2300 (https://stackoverflow.com/questions/30172605/how-do-i-get-into-a-docker-containers-shell?rq=1) How do I get into a Docker container's shell? (https://stackoverflow.com/q/16047306?rq=1) (Question score (upvotes - downvotes)) 4464 (https://stackoverflow.com/questions/16047306/how-is-docker-different-from-a-virtual-machine?rq=1) How is Docker different from a virtual machine? (https://stackoverflow.com/q/35594987?rq=1) (Question score (upvotes - downvotes)) 1648 (https://stackoverflow.com/questions/35594987/how-to-force-docker-for-a-clean-build-of-an-image?rq=1) How to force Docker for a clean build of an image (https://stackoverflow.com/q/21553353?rq=1) (Question score (upvotes - downvotes)) 2790 (https://stackoverflow.com/questions/21553353/what-is-the-difference-between-cmd-and-entrypoint-in-a-dockerfile?rq=1) What is the difference between CMD and ENTRYPOINT in a Dockerfile? (https://stackoverflow.com/q/23935141?rq=1) (Question score (upvotes - downvotes)) 2333 (https://stackoverflow.com/questions/23935141/how-to-copy-docker-images-from-one-host-to-another-without-using-a-repository?rq=1) How to copy Docker images from one host to another without using a repository (https://stackoverflow.com/q/23439126?rq=1) (Question score (upvotes - downvotes)) 923 (https://stackoverflow.com/questions/23439126/how-to-mount-a-host-directory-in-a-docker-container?rq=1) How to mount a host directory in a Docker container (https://stackexchange.com/questions?tab=hot) Hot Network Questions (Science Fiction & Fantasy Stack Exchange) (https://scifi.stackexchange.com/questions/291290/did-gandalf-know-he-was-a-maia) Did Gandalf know he was a Maia? (Chess Stack Exchange) (https://chess.stackexchange.com/questions/46136/are-old-games-from-2023-considered-in-new-fide-rating-calculations) Are old games from 2023 considered in new fide rating calculations? (Home Improvement Stack Exchange) (https://diy.stackexchange.com/questions/306167/what-is-this-device-in-my-ceiling-making-out-of-battery-chirps) What is this device in my ceiling making out of battery chirps? (English Language & Usage Stack Exchange) (https://english.stackexchange.com/questions/625583/word-for-a-collection-of-awards-such-as-an-olympic-athlete-s-earned-medals) Word for a collection of awards, such as an Olympic athlete’s earned medals (Politics Stack Exchange) (https://politics.stackexchange.com/questions/88907/why-is-namibia-killing-game-animals-for-food-rather-than-allowing-more-big-game) Why is Namibia killing game animals for food rather than allowing more big game hunting? (Writing Stack Exchange) (https://writing.stackexchange.com/questions/70046/why-is-simpler-prose-more-popular-these-days) Why is simpler prose more popular these days? (Electrical Engineering Stack Exchange) (https://electronics.stackexchange.com/questions/724139/driving-relay-without-a-control-fet-back-emf-consideration-when-control-side-is) Driving relay without a control Fet: Back EMF consideration when control side is Grounded (Travel Stack Exchange) (https://travel.stackexchange.com/questions/191132/is-it-possible-to-travel-to-usa-with-legal-cannabis) Is it possible to travel to USA with legal cannabis? (Code Golf Stack Exchange) (https://codegolf.stackexchange.com/questions/275291/how-many-ways-can-you-make-change) How many ways can you make change? (Mathematics Stack Exchange) (https://math.stackexchange.com/questions/4965936/identifications-in-differential-geometry) Identifications in differential geometry (Academia Stack Exchange) (https://academia.stackexchange.com/questions/213315/what-should-be-the-affiliation-of-phd-student-who-submitted-thesis-but-yet-to-de) What should be the affiliation of PhD student who submitted thesis but yet to defend, in a conference talk slides? (Movies & TV Stack Exchange) (https://movies.stackexchange.com/questions/123342/why-does-jenny-hug-cindy-at-the-end-of-commando-1985) Why does Jenny hug Cindy at the end of "Commando" (1985)? (Signal Processing Stack Exchange) (https://dsp.stackexchange.com/questions/94978/pros-and-cons-of-esprit-versus-music) Pros and cons of ESPRIT versus MUSIC (Physics Stack Exchange) (https://physics.stackexchange.com/questions/826517/what-does-the-equation-of-the-law-of-conservation-of-total-mechanical-energy-wit) What does the equation of the law of conservation of total mechanical energy with deformation energy look like? (Physics Stack Exchange) (https://physics.stackexchange.com/questions/826484/does-measuring-a-quantum-object-collapse-the-wave-function-even-if-the-particle) Does measuring a quantum object collapse the wave function even if the particle is not found in the position where it was measured? (Role-playing Games Stack Exchange) (https://rpg.stackexchange.com/questions/213217/would-it-be-balanced-to-give-everyone-warlock-slots-for-casting-racial-spells) Would it be Balanced to Give Everyone Warlock Slots for Casting Racial Spells? (Software Engineering Stack Exchange) (https://softwareengineering.stackexchange.com/questions/454835/expensive-constructors-should-they-exist-should-they-be-replaced) Expensive constructors. Should they exist? Should they be replaced? (Travel Stack Exchange) (https://travel.stackexchange.com/questions/191130/flight-delayed-risk-of-missing-connection-can-i-cancel-and-get-refund) Flight delayed, risk of missing connection, can I cancel and get refund? (Unix & Linux Stack Exchange) (https://unix.stackexchange.com/questions/782873/bash-script-that-takes-multiple-path-arguments-and-checks-if-files-can-be-succes) Bash script that takes multiple path arguments and checks if files can be successfully created there (Law Stack Exchange) (https://law.stackexchange.com/questions/104747/is-it-illegal-to-use-a-fake-state-id-to-enter-a-private-establishment-even-when) Is it illegal to use a fake state ID to enter a private establishment even when a legitimate ID would've been fine? (Academia Stack Exchange) (https://academia.stackexchange.com/questions/213261/journal-keeps-messing-with-my-proof) Journal keeps messing with my proof (Philosophy Stack Exchange) (https://philosophy.stackexchange.com/questions/116741/are-there-probabilistic-facts-of-the-matter-about-the-universe) Are there probabilistic facts of the matter about the universe? (Cryptography Stack Exchange) (https://crypto.stackexchange.com/questions/112845/does-the-tls-ecdhe-ecdsa-with-aes-256-ccm-ciphersuite-support-the-sha-384-hash-f) Does the TLS_ECDHE_ECDSA_WITH_AES_256_CCM ciphersuite support the sha 384 hash function? (Puzzling Stack Exchange) (https://puzzling.stackexchange.com/questions/128153/decode-the-pipe) decode the pipe (/feeds/question/37458287) (Feed of this question and its answers) Question feed Subscribe to RSS Question feed To subscribe to this RSS feed, copy and paste this URL into your RSS reader. (https://stackoverflow.com/feeds/question/37458287) () lang-bash (https://stackoverflow.com) (https://stackoverflow.com) Stack Overflow (/questions) Questions (/help) Help (https://chat.stackoverflow.com/?tab=site&host=stackoverflow.com) Chat (https://stackoverflow.co/) Products (https://stackoverflow.co/teams/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=footer&utm_content=teams) Teams (https://stackoverflow.co/advertising/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=footer&utm_content=advertising) Advertising (https://stackoverflow.co/advertising/employer-branding/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=footer&utm_content=talent) Talent (https://stackoverflow.co/) Company (https://stackoverflow.co/) About (https://stackoverflow.co/company/press/) Press (https://stackoverflow.co/company/work-here/) Work Here (https://stackoverflow.com/legal) Legal (https://stackoverflow.com/legal/privacy-policy) Privacy Policy (https://stackoverflow.com/legal/terms-of-service/public) Terms of Service (/contact) Contact Us Cookie Settings (https://stackoverflow.com/legal/cookie-policy) Cookie Policy (https://stackexchange.com) Stack Exchange Network (https://stackexchange.com/sites#technology) Technology (https://stackexchange.com/sites#culturerecreation) Culture & recreation (https://stackexchange.com/sites#lifearts) Life & arts (https://stackexchange.com/sites#science) Science (https://stackexchange.com/sites#professional) Professional (https://stackexchange.com/sites#business) Business (https://api.stackexchange.com/) API (https://data.stackexchange.com/) Data (https://stackoverflow.blog?blb=1) Blog (https://www.facebook.com/officialstackoverflow/) Facebook (https://twitter.com/stackoverflow) Twitter (https://linkedin.com/company/stack-overflow) LinkedIn (https://www.instagram.com/thestackoverflow) Instagram Site design / logo © 2024 Stack Exchange Inc; user contributions licensed under (https://stackoverflow.com/help/licensing) CC BY-SA . rev 2024.9.2.14718 By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our (https://stackoverflow.com/legal/cookie-policy) Cookie Policy . Accept all cookies Necessary cookies only Customize settings