Lysender
Lysender

Reputation: 179

Docker CentOS 7 - cron not working in local machine

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 <[email protected]>

# 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:

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.

Upvotes: 2

Views: 6852

Answers (2)

Feras
Feras

Reputation: 2164

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

Upvotes: 3

Lysender
Lysender

Reputation: 179

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.

Upvotes: 1

Related Questions