简体   繁体   中英

Crontab doesn't seem to work within Docker container

I am trying to run Crontab within a Docker Container. The Docker container is based of Centos7. I have a python file which I need to run every 2 minutes within the Docker container.

I have installed Python3, Crontab and every dependencies within the container. When I try to run: /usr/bin/python3 /home/centos/pipeline_trigger/cron-trigger.py within the container, I am getting the expected result.

However, even if I setup crontab, its not running the python file.

Steps I followed:

  1. Ran crontab -e and entered the cron duration and action.

    在此处输入图片说明

  2. Saved the file and got output as: crontab: installing new crontab

  3. Even after 2 or 4 minutes, the cronjob is not triggering the python file.

在此处输入图片说明

Crontab Version: cronie-1.4.11-23.el7.x86_64

Does anyone know how to get the expected result?

Crontabs are run by the cron daemon. In normal systems (not Docker containers, for example, LXC, systemd-nspawn, VMs, FreeBSD Jails, real machines), the userspace begins with init , which is the service manager. The service manager starts all kinds of daemons, for example, sshd (SSH Server), getty (TTY Login Prompt), cron (Scheduled Task Daemon) and so on.

Docker containers, on the other hand, do not start that way. Containers do not have service managers, and they directly run your designated binary and arguments in the ENTRYPOINT or CMD commands while building Docker images. For example, a Java container would run /usr/bin/java as its ENTRYPOINT . Therefore, cron , which is the daemon of running crontab tasks, do not start at all.

If you rely on multiple processes in a Docker container, a common way is to write a startup script for your ENTRYPOINT. This script will start all daemons. There are also frameworks to do so.

Also, make sure your crontab is installed in Dockerfile. Any changes in files that are not mounted by volume in a running container will lose when you delete the container, since Docker containers are immutable.

I had actually made an error with my Python file where it was using the open('directory/file location') and I gave the wrong path.

I was able to find the issue after running crontab -e and adding */2 * * * * /usr/bin/python3 /home/centos/pipeline_trigger/cron-trigger.py >> /tmp/trigger.log 2>&1

It gave me the correct error message and when I gave the right path, it worked!

Now the cron job works perfectly within the container.

Added the following to the dockerfile:

RUN touch /var/spool/cron/root \
&& echo -e "30 4 * * * /usr/bin/python3 /home/centos/pipeline_trigger/cron-trigger.py >> /tmp/pipeline_trigger.log 2>&1 >> /var/spool/cron/root

This way the cron is setup while creating the Docker image itself and all I have to do is simply run the container. No need to pass arguements.

DOCKERFILE:

FROM centos:7 as base
WORKDIR /home

RUN yum update -y \
&& yum install sudo -y \
&& yum install docker -y \
&& yum install openssh-server -y \
&& yum install -y openssh-clients \
&& yum install -y epel-release \
&& yum install git -y \
&& yum install python3 -y \
&& yum install cronie -y



FROM base as configure
WORKDIR /home

RUN sudo useradd centos \
&& echo password | passwd centos --stdin \
&& echo "centos        ALL=(ALL)       NOPASSWD: ALL" >> /etc/sudoers \
&& sudo sed -i "/^[^#]*PasswordAuthentication[[:space:]]no/c\\PasswordAuthentication yes" /etc/ssh/sshd_config \



FROM configure as pipelinetrigger
WORKDIR /home/centos/pipeline_trigger/
COPY cron-trigger.py ./cron-trigger.py

RUN touch /var/spool/cron/root \
&& echo -e "30 4 * * * /usr/bin/python3 /home/centos/pipeline_trigger/cron-trigger.py >> /tmp/pipeline_trigger.log 2>&1" >> /var/spool/cron/root

ENTRYPOINT /usr/sbin/init
CMD ["systemctl","restart","sshd.service"]

Got it fixed.Thanks for all your help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM