This is my Dockerfile Fallout new vegas gun build.
After the docker image is created, I login and try to setup a cron job for testing. To my surprise,
cron and crontab are not present.
But I expect
cron to be present in an ubuntu image. Did I pick a wrong image or is there anything I need to do to enable cron ?
Anthony Kong
Anthony KongAnthony Kong
99811 gold badge2121 silver badges5353 bronze badges
2 Answers
The cron command is not installed by default in the image ubuntu:16.04
Need to run Anthony KongAnthony Kong
apt-get install cron
99811 gold badge2121 silver badges5353 bronze badges
Docker images are minimal by design, and they are used for creating containers, not a full operating system. A container is isolating the running of an application, so it will not have all the other OS daemons running inside that environment like cron, syslog, mail etc, by default.
You can install cron with:
inside your Dockerfile. However to run the crontab entries, you also need to start the cron daemon as part of your container startup process. There are tools like forego and supervisord that you can use to run multiple processes in your container (cron plus your app), but doing so is often the sign of an anti-pattern.
BMitchBMitch
Not the answer you're looking for? Browse other questions tagged crondocker or ask your own question.
I would like to run a python cron job inside of a docker container in detached mode. My set-up is below:
My python script is test.py
My cron file is my-crontab
and my Dockerfile is
What are the potential problems with this approach? Are there other approaches and what are their pros and cons?
Christopher Rapcewicz
Christopher RapcewiczChristopher Rapcewicz
1,35255 gold badges2424 silver badges3737 bronze badges
8 Answers
Several issues that I faced while trying to get a cron job running in a docker container were:
There are cron-specific issues and are docker-specific issues in the list, but in any case they have to be addressed to get cron working.
To that end, my current working solution to the problem posed in the question is as follows:
Create a docker volume to which all scripts running under cron will write:
The script that will run under cron is
test.py :
In order to pass the environment variable to the script that I want to run under cron, follow Thomas' suggestion and put a crontab fragment for each script (or group of scripts) that has need of a docker environment variable in
/etc/cron.d with a placeholder XXXXXXX which must be set.
Instead of calling cron directly, wrap cron in a python script that does does things: 1. reads the environment variable from the docker environment variable and sets the environment variable in a crontab fragment.
The
Dockerfile that for the container in which the cron jobs run is as follows:
Finally, create the containers and run them:
1,35255 gold badges2424 silver badges3737 bronze badges
Here is a complement on rosksw answer.
There is no need to do some string replacement in the crontab file in order to pass environment variables to the cron jobs.
It is simpler to store the environment variables in a file when running the contrainer, then load them from this file at each cron execution. I found the tip here.
In the dockerfile:
In the crontab file:
AndreL
3,54833 gold badges2020 silver badges3434 bronze badges
Alban MoutonAlban Mouton
Adding crontab fragments in
/etc/cron.d/ instead of using root's crontab might be preferable.
This would:
Observe that the format of those files is a bit different from a crontab entry. Here's a sample from the Debian php package:
Overall, from experience, running cron in a container does work very well (besides cron logging leaving a lot to be desired).
Thomas OrozcoThomas Orozco
37.2k66 gold badges7878 silver badges9797 bronze badges
Here's an alternative solution.
in
Dockerfile
in
entrypoint.sh
evtuhovdoevtuhovdo
We are using below solution. It supports both
docker logs functionality and ability to hang the cron process in the container on PID 1 (if you use tail -f workarounds provided above - if cron crashes, docker will not follow restart policy):
cron.sh:
Dockerfile:
crontab:
And please don't forget to add the creepy new line in your crontab
dogikdogik
Single Container Method
You may run
crond within the same container that is doing something closely related using a base image that handles PID 0 well, like phusion/baseimage.
Specialized Container Method
May be cleaner would be to have another Container linked to it that just runs
crond . For example:
Dockerfile
crontab
Then run:
Note: In this case it'll run the job as WernightWernight
www-data . Cannot just mount the crontab file as volume because it needs to be owned by root with only write access for root , else crond will run nothing. Also you'll have to run crond as root .
23.3k1818 gold badges101101 silver badges121121 bronze badges
Don't mix crond and your base image. Prefer to use a native solution for your language (schedule or crython as said by Anton), or decouple it. By decoupling it I mean, keep things separated, so you don't have to maintain an image just to be the fusion between python and crond.
You can use Tasker, a task runner that has cron (a scheduler) support, to solve it, if you want keep things decoupled. Download mobile legend for pc.
Here an
docker-compose.yml file, that will run some tasks for you
Just run
docker-compose up , and see it working. Here is the Tasker repo with the full documentation:
OPSXCQOPSXCQ
Another possibility is to use Crython. Crython allows you to regularly schedule a python function from within a single python script / process. It even understands cron syntax:
Using crython avoids the various headaches of running crond inside a docker container - your job is now a single process that wakes up when it needs to, which fits better into the docker execution model. But it has the downside of putting the scheduling inside your program, which isn't always desirable. Still, it might be handy in some use cases.
Anton I. SiposAnton I. Sipos
1,73011 gold badge1919 silver badges2424 bronze badges
Not the answer you're looking for? Browse other questions tagged pythoncrondocker or ask your own question.
I have referred 10+ links of similar issue but none of one worked. I am working in ubuntu 16.04 docker container. I am trying to set cron jobs. I referred this link to install cron.
for making demo add below line in
crontab -e (as a root user)
when saving file it will notify
crontab: installing new crontab but none of file created at a specified location.
Below steps are I am trying to sort out that issues but none of one worked.
note: Actually the origin of the issues come from here. If you need any additional info please mention in comment.
can anyone assist me what I am doing wrong here?
I tried the same replication of @BMitch answer but that will also produce same problem. I am waiting more than five minutes but no cron log file generated in
/var/www/public/cronfile.log , please see my stuff
version check
Community♦
Bilal UseanBilal Usean
88511 gold badge88 silver badges2929 bronze badges
1 Answer
Docker is an application isolation tool, unlike how virtual machines are an OS isolation tool. That means you run your application inside of a Docker container, and in that container no other OS services components are running by default. So if you configure your crontab, you don't have the cron daemon running that would process that file and kick off the jobs. To have this run for you, a
cron -f needs to be run to launch the daemon in the foreground. If your container runs another application (most likely), then you'd need a tool like supervisord to launch multiple applications to also run cron.
Edit: this is my attempt to reproduce your error in my lab after starting cron. As you can see, the file is created for me:
BMitchBMitch
77.3k1111 gold badges173173 silver badges189189 bronze badges
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Not the answer you're looking for? Browse other questions tagged ubuntudockercroncrontabubuntu-16.04 or ask your own question.
In my VPS, I have created a docker image containing cron running as entry command. I also have a sample cron file that says it should execute the command every 5 minutes.
Dockerfile:
my-cron.conf file:
start.sh
Then I build it like this:
Then run:
After 5 minutes, I checked if the cron works:
It works. So I pushed it to docker hub into my account.
Then pull in my local machine and run it like how I did on my VPS host.
However, there is no sign that the cron actually runs, ex:
/tmp/logit.log file is never created.
What could have been wrong?
Docker Compose Crontab
Machine specs:
Both are run as regular user (non-root).
However, the difference is the CentOS image.
I have pulled a new CentOS7 image so that both would be 5 weeks old but I didn't delete the image first (just updated/pulled).
So I pulled CentOS 7, then pull lysender/cron-php-gearman. My understanding is that it should be running on top of the newer CentOS 7 image.
LysenderLysender
2 Answers
Seems like a container specific issue on Centos:https://github.com/CentOS/CentOS-Dockerfiles/issues/31 FerasFeras
RUN sed -i '/session required pam_loginuid.so/d' /etc/pamd.d/crond
1,26611 gold badge1212 silver badges3838 bronze badges
I have decided to not use stock cron daemon and switch to a python based
devcron based on this Dockerfile.
Here is my new Dockerfile.
Devcron: https://bitbucket.org/dbenamy/devcron
The reason is that, after wiping out the whole local docker installation and installing fresh docker images, the stock cron is still not working in my local setup.
This will be my temporary solution for now.
Thanks for the help.
LysenderLysender
Not the answer you're looking for? Browse other questions tagged crondockercentos7 or ask your own question.Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |