Docker CentOS 7 - cron not working in local machin

2019-07-13 14:52发布

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:

FROM centos:centos7
MAINTAINER Lysender <foo@example.com>

# Install packages
RUN yum -y update &&  yum clean all
RUN yum -y install epel-release && yum clean all

RUN yum -y install git \
    bind-utils \
    pwgen \
    psmisc \
    net-tools \
    hostname \
    curl \
    curl-devel \
    sqlite \
    cronie \
    libevent \
    gearmand \
    libgearman \
    libgearman-devel \
    php \
    php-bcmath \
    php-common \
    php-pear \
    php-mysql \
    php-cli \
    php-devel \
    php-gd \
    php-fpm \
    php-pdo \
    php-mbstring \
    php-mcrypt \
    php-soap \
    php-xml \
    php-xmlrpc \
    php-pecl-gearman && yum clean all

# Configure servicies
ADD ./start.sh /start.sh
ADD ./my-cron.conf /etc/cron.d/my-cron

RUN chmod 755 /start.sh

CMD ["/bin/bash", "/start.sh"]

my-cron.conf file:

# Run command every 5 minutes
*/5 * * * * root echo "foo" >> /tmp/logit.log 2>&1

start.sh

#!/bin/bash

__run_cron() {
    echo "Running the run_cron function."
    crond -n
}

# Call all functions
__run_cron

Then I build it like this:

docker build --rm -t lysender/cron-php-gearman .

Then run:

docker run --name cron -d lysender/cron-php-gearman

After 5 minutes, I checked if the cron works:

docker exec -it cron bash
cat /tmp/logit.log

It works. So I pushed it to docker hub into my account.

docker push lysender/cron-php-gearman

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?

Machine specs:

  • VPS: Linode running Slackware 14.1 on KVM , Docker 1.6.2
  • Local: VirtualBox VM running Slackware 14.1, Docker 1.6.2 - same as the VPS

Both are run as regular user (non-root).

However, the difference is the CentOS image.

  • VPS: 5 weeks old
  • Local: 4 months old.

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.

2条回答
小情绪 Triste *
2楼-- · 2019-07-13 14:53

I have decided to not use stock cron daemon and switch to a python based devcron based on this Dockerfile.

https://registry.hub.docker.com/u/hamiltont/docker-cron/dockerfile/

Here is my new Dockerfile.

https://github.com/lysender/docker-cron-php-gearman/blob/master/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.

查看更多
看我几分像从前
3楼-- · 2019-07-13 15:14

Seems like a container specific issue on Centos: https://github.com/CentOS/CentOS-Dockerfiles/issues/31 RUN sed -i '/session required pam_loginuid.so/d' /etc/pamd.d/crond

查看更多
登录 后发表回答