在我的VPS中,我创建了一个包含作为入口命令运行的cron的docker镜像。我还有一个示例cron文件,说它应该每5分钟执行一次命令。
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文件:
# 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
然后我像这样构建它:
docker build --rm -t lysender/cron-php-gearman .
然后运行:
docker run --name cron -d lysender/cron-php-gearman
5分钟后,我检查了cron是否有效:
docker exec -it cron bash
cat /tmp/logit.log
有效。所以我把它推到了我的帐户中的docker hub。
docker push lysender/cron-php-gearman
然后拉入我的本地计算机并像在VPS主机上一样运行它。
但是,没有迹象表明cron实际运行,ex:/tmp/logit.log
文件永远不会创建。
可能出现什么问题?
机器规格:
两者都以普通用户身份运行(非root用户)。
然而,区别在于CentOS图像。
我已经提取了一个新的CentOS7图像,因此两者都将是5周,但我没有先删除图像(只是更新/拉出)。
所以我拉了CentOS 7,然后拉lysender / cron-php-gearman。我的理解是它应该在较新的CentOS 7映像上运行。
答案 0 :(得分:2)
似乎是Centos上的容器特定问题:
https://github.com/CentOS/CentOS-Dockerfiles/issues/31
RUN sed -i '/session required pam_loginuid.so/d' /etc/pamd.d/crond
答案 1 :(得分:1)
I have decided to not use stock cron daemon and switch to a python based (rnrs mutable-pairs)
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.