Cron作业不会在Docker容器中运行 [英] Cron job won't run inside Docker container

查看:77
本文介绍了Cron作业不会在Docker容器中运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图安排一个任务在我的容器中运行,但是无法让cron执行任何事情.

I am trying to schedule a task to run inside my container, but can't get cron to execute anything.

我都遵循了如何运行和 https://serverfault.com/questions/924779/docker-cron-not-working 没有成功.当我启动容器时,什么也没发生.

I've followed both How to run a cron job inside a docker container? and https://serverfault.com/questions/924779/docker-cron-not-working with no success. Simply nothing happens when I start up my container.

我做了一个简单的容器:

I have made a simple container:

test/
    Dockerfile
    hello-cron

Dockerfile 的内容:

FROM ubuntu:latest

RUN apt-get update && apt-get -y install cron

COPY hello-cron /etc/cron.d/hello-cron

RUN chmod 0644 /etc/cron.d/hello-cron

RUN crontab /etc/cron.d/hello-cron

RUN touch /var/log/cron.log

CMD cron && tail -f /var/log/cron.log

hello-cron :

* * * * * root echo "Hello World" >> /var/log/cron.log 2>&1


我构建我的容器: docker build -t cron-test.并运行它 docker run -t -i cron-test .

I build my container: docker build -t cron-test . and run it docker run -t -i cron-test.

没有控制台输出.我还尝试过在图像中输入bash来检查日志文件的内容以及 hello-cron 文件是否实际上已添加到crontab中:

There is no console output. I have also tried to enter bash in the image to check the contents of the log-file and whether or not the hello-cron file is actually added to the crontab:

docker exec -it <image-id> bash

cat/var/log/cron.log 不产生任何结果,而 hello-cron 文件位于/etc/cron.d/hello中-cron .

and cat /var/log/cron.log yields nothing, while the hello-cron file is located at /etc/cron.d/hello-cron.

我知道这似乎是重复的,但是我见过的所有公认解决方案都不能解决这个问题.

I know this seems like a duplicate, but none of the accepted solutions I have seen solves this issue.

推荐答案

我建议为了cron的目的而使用alpine而不是ubuntu.

I will suggest using alpine instead of ubuntu just for the sake of cron.

FROM alpine:latest

RUN echo "* * * * * echo hello;exit 0" | crontab - 
CMD ["crond","-f"]

您可以探索更多选择

    -c  Crontab directory
    -u  User
    -l  List crontab
    -e  Edit crontab
    -r  Delete crontab
    FILE    Replace crontab by FILE ('-': stdin)

对于阿尔卑斯山脉,您也无需使用 CMD cron&&tail -f/var/log/cron.log ,就像您在ubuntu基本映像中添加一样.

Also with alpine you do not need to make container good for no thing with CMD cron && tail -f /var/log/cron.log like you add in ubuntu base image.

当您使用 CMD cron&&tail -f/var/log/cron.log ,cron不再是容器的根进程,并且如果cron关闭,容器将重新启动或停止.

When you use CMD like CMD cron && tail -f /var/log/cron.log , cron is no more the root process of the container and the container will restart or suppose to stop if cron is down.

使用 CMD ["crond",-f"] ,如果在前台运行容器,则可以在控制台中查看日志,也可以稍后查看日志.

using CMD ["crond","-f"] you will able to see logs in the console if you run the container in the foreground or you can checks logs latter.

奖金:

您仅需 5.58MB

这篇关于Cron作业不会在Docker容器中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆