【发布时间】:2015-10-17 03:00:02
【问题描述】:
在我的 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 实际运行,例如:/tmp/logit.log 文件从未创建。
可能出了什么问题?
机器规格:
- VPS:Linode 在 KVM、Docker 1.6.2 上运行 Slackware 14.1
- 本地:VirtualBox VM 运行 Slackware 14.1、Docker 1.6.2 - 与 VPS 相同
两者都以普通用户(非 root)运行。
但是,区别在于 CentOS 映像。
- VPS:5 周龄
- 本地:4 个月大。
我已经拉取了一个新的 CentOS7 映像,所以这两个映像都是 5 周大的,但我没有先删除该映像(只是更新/拉取)。
所以我拉了 CentOS 7,然后拉了 lysender/cron-php-gearman。我的理解是它应该运行在较新的 CentOS 7 映像之上。
【问题讨论】:
-
如果你在
docker exec之后运行ps,crond是否在运行? -
是:
crond -n正在运行。 -
这里是完整的细节:本地 vs vps ps aux:gist.github.com/lysender/57c2e4c9ff6183dc813c