我构建了一个Docker映像来运行crontab文件:
RUN apt-get install -y cron
RUN touch /usr/local/learnintouch/cron.log
COPY learnintouch.cron /usr/local/learnintouch/
RUN chmod 0644 /usr/local/learnintouch/learnintouch.cron \
&& sudo crontab /usr/local/learnintouch/learnintouch.cron它有一个入口点来运行包含以下内容的start.sh文件:
# Run the crontab
sudo service cron startlearnintouch.cron文件包含:
* * * * * echo "Hello cron" >> /usr/local/learnintouch/logs/cron.log 2>&1但日志上什么都没有。
只有当我在容器中连接并手动运行start.sh文件时,即作为root用户,日志才会显示Hello cron消息。
当登录到容器中时,这些文件有apache用户:
root@72f59adb5324:/usr/local/learnintouch# ll
total 2852
drwxr-xr-x 1 apache apache 4096 May 11 19:42 ./
drwxr-xr-x 1 root root 4096 May 3 20:10 ../
-rwxr-xr-x 1 apache apache 0 May 11 18:56 cron.log*
-rwxr-xr-x 1 apache apache 1057 May 11 19:34 start.sh*
root@72f59adb5324:/usr/local/learnintouch# whoami
root我认为这是一个用户权限问题。
更新:我尝试用Dockerfile复制这个问题,如下所示:
FROM ubuntu:20.10
RUN apt-get update \
&& apt-get install -y sudo \
&& apt-get autoremove -y && apt-get clean
RUN mkdir -p /usr/local/learnintouch/
RUN apt-get install -y cron
COPY learnintouch.cron /usr/local/learnintouch/
RUN chmod 0644 /usr/local/learnintouch/learnintouch.cron \
&& crontab /usr/local/learnintouch/learnintouch.cron
ENTRYPOINT ["/usr/sbin/cron", "tail", "-f", "/dev/null"]在构建图像之后:
docker build -t stephaneeybert/cronissue .并运行容器:
docker run --name cronissue -v ~/dev/docker/projects/common/volumes/logs:/usr/local/learnintouch/logs stephaneeybert/cronissue cron开始运转良好,但问题并未出现。
因此,我估计这个问题可能在我使用的docker-compose.yml文件中。
因此,我尝试使用docker-compose.yml文件运行:
version: "3.7"
services:
cronissue:
image: stephaneeybert/cronissue
volumes:
- "~/dev/docker/projects/common/volumes/logs:/usr/local/learnintouch/logs"“码头工人群”命令:
docker stack deploy --compose-file docker-compose.yml cronissue再一次,这位官员开始工作得很好,这个问题也没有出现。
最后,我在项目中添加了user: "${CURRENT_UID}:${CURRENT_GID}"属性,如下所示:
version: "3.7"
services:
cronissue:
image: stephaneeybert/cronissue
volumes:
- "~/dev/docker/projects/common/volumes/logs:/usr/local/learnintouch/logs"
user: "${CURRENT_UID}:${CURRENT_GID}"而这一次,cron没有工作,问题出现了。
只有当我与主机用户一起运行容器时,才会出现此问题。
另外,我也尝试打开文件权限,但它没有更改任何内容:
&& chmod a+x /usr/bin/crontab \
&& chmod a+x /usr/sbin/cron \更新:我最终使用了supercronic而不是cron,因为它在容器中工作得很好。
# Using supercronic as a cron scheduler
# See https://github.com/aptible/supercronic/
COPY supercronic-linux-amd64 /usr/local/learnintouch
COPY learnintouch.cron /usr/local/learnintouch/
RUN chmod 0644 /usr/local/learnintouch/learnintouch.cron发布于 2021-05-13 11:36:32
首先,ENTRYPOINT中的命令没有意义。你有:
ENTRYPOINT ["/usr/sbin/cron", "tail", "-f", "/dev/null"]看起来您正在尝试将两个命令(cron和tail -f /dev/null)组合在一起,但是不能仅仅将这样的命令混合在一起。看起来,您使用tail -f /dev/null试图让容器在cron背景本身之后运行,但这是不必要的--看看手册页,我们可以使用-f标志来将它保存在前台,这样就变成了:
ENTRYPOINT ['/usr/sbin/cron', '-f']幸运的是,按照配置的方式,cron只是忽略了未知的参数(您可以运行cron this is a test -f并工作),所以您不小心做了正确的事情。
只有当我与主机用户一起运行容器时,才会出现此问题。
cron守护进程被设计为以root的形式运行。如果我尝试以非root用户的身份启动您的映像,例如使用设置user的docker-compose.yml,我得到:
cronissue_1 | cron: can't open or create /var/run/crond.pid: Permission denied即使通过更改目录所有权来解决这个问题,cron仍然会失败:
$ cron -f
seteuid: Operation not permitted您需要将cron作为root运行,或者如果您真的希望以非root用户的身份运行调度程序,则需要找到其他工具。
另外,我也尝试打开文件权限,但它没有更改任何内容: && chmod a+x /usr/bin/crontab \& chmod a+x /usr/sbin/cron \
请注意,上面的命令是非操作的;这两个命令都是每个人都可以执行的,所以您没有做任何更改。
https://stackoverflow.com/questions/67493776
复制相似问题