【发布时间】:2016-04-29 22:58:30
【问题描述】:
我有这个简单的 Dockerfile 用于测试,但这在我的 LEMP 堆栈中的 PHP 映像中也是相同的:cron 作业根本没有在 Docker 中执行。
这是我的测试 Dockerfile:
FROM debian:latest
MAINTAINER XY <info@domain.com>
LABEL Description="Cron" Vendor="Istvan Lantos" Version="1.0"
RUN apt-get -y update && apt-get -y dist-upgrade \
&& apt-get -y install \
cron \
rsyslog \
vim
RUN rm -rf /var/lib/apt/lists/*
#cron fixes
RUN touch /etc/crontab /etc/cron.d/* /var/spool/cron/crontabs/*
#COPY etc/cron.d /etc/cron.d
COPY etc/crontab /etc/crontab
#COPY var/spool/cron/crontabs /var/spool/cron/crontabs
RUN chmod 600 /etc/crontab /etc/cron.d/* /var/spool/cron/crontabs/*
RUN touch /etc/crontab /etc/cron.d/* /var/spool/cron/crontabs/*
RUN rm -rf /var/lib/apt/lists/*
COPY docker-entrypoint.sh /
RUN chmod +x /docker-entrypoint.sh
CMD ["/docker-entrypoint.sh"]
docker-entrypoint.sh:
#!/bin/bash
set -e
echo PID1 > /dev/null
/etc/init.d/rsyslog start
#Stay in foreground mode, don’t daemonize.
/usr/sbin/cron -f
这是 Crontab 文件。我还在/etc/cron.d 或/var/spool/cron/crontabs 中放置了一个带有用户名的衬垫,但效果与我修改此基本crontab 文件一样:不会执行cron 作业:
MAILTO=""
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
SHELL=/bin/sh
#PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/php7/bin:/usr/local/php7/sbin
# m h dom mon dow user command
#17 * * * * root cd / && run-parts --report /etc/cron.hourly
#25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
#47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
#52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#
*/1 * * * * root date >> /var/log/cron-test.log 2>&1
这是/var/log/syslog 文件的输出:
Jan 23 09:38:39 1ab854e8d9a7 rsyslogd: [origin software="rsyslogd" swVersion="8.4.2" x-pid="14" x-info="http://www.rsyslog.com"] start
Jan 23 09:38:39 1ab854e8d9a7 rsyslogd: imklog: cannot open kernel log(/proc/kmsg): Operation not permitted.
Jan 23 09:38:39 1ab854e8d9a7 rsyslogd-2145: activation of module imklog failed [try http://www.rsyslog.com/e/2145 ]
Jan 23 09:38:39 1ab854e8d9a7 cron[19]: (CRON) INFO (pidfile fd = 3)
Jan 23 09:38:39 1ab854e8d9a7 cron[19]: (*system*) NUMBER OF HARD LINKS > 1 (/etc/crontab)
Jan 23 09:38:39 1ab854e8d9a7 cron[19]: (*) ORPHAN (no passwd entry)
Jan 23 09:38:39 1ab854e8d9a7 cron[19]: (CRON) INFO (Running @reboot jobs)
/var/log/cron-test.log 不会由 cron 作业创建。
对于那些将其标记为“离题”和超级用户材料的人,我有一个问题,而且这是关于通用计算硬件和软件的问题:真的吗? Docker 问题何时成为系统管理员?这样,这里的每个 Docker 相关问题都至少有一个标志。我并不反对将更多用户推广到鲜为人知的子网站,但与他们相比,我们有更多的变化可以在这里找到答案。
更新:
这是我想出的,直到 cron 作业不工作:
Dockerfile 结束:
COPY cron-jobs.sh /
RUN chmod +x /cron-jobs.sh
COPY docker-entrypoint.sh /
RUN chmod +x /docker-entrypoint.sh
CMD ["/docker-entrypoint.sh"]
docker-entrypoint.sh:
#!/bin/bash
set -e
echo PID1 > /dev/null
# Run script in the background (this is not daemonized)
/cron-jobs.sh &
/usr/local/php7/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php7/etc/php-fpm.conf
cron-jobs.sh:
#!/bin/bash
while true; do
date >> /var/log/cron-test.log 2>&1
sleep 60
done
【问题讨论】:
-
我今天早上提到的例子在这里适用吗? (stackoverflow.com/a/34960575/6309)
-
你尝试过 baseimage-docker 吗?它有 syslog 和 cron。不过是 Ubuntu。
-
@VonC 我用
* * * * * root echo "Hello world" >> /var/log/cron.log 2>&1的内容和#的空行尝试了那个例子。尽管创建了cron.log,但在前 5-7 分钟后它没有填充Hello World(例如说它应该每 2 分钟回显一次?)。syslog说(*system*hello-cron) NUMBER OF HARD LINKS > 1 (/etc/cron.d/hello-cron)。老实说,我对 bash 脚本解决方案很满意,但仍然很高兴知道为什么不工作。如果可能的话,我想坚持使用 Debian。 -
*表示每分钟,*/2表示在 0、2、4 等。并且空行表示空(不以#开头:空)
标签: docker cron crontab dockerfile docker-compose