【问题标题】:Running cron in a docker container on a windows host在 Windows 主机上的 docker 容器中运行 cron
【发布时间】:2019-10-13 22:41:26
【问题描述】:

我在尝试创建一个运行 cronjob 的容器时遇到了一些问题。我可以看到 cron 在容器中使用 top 运行,但它不会像下面的示例尝试那样写入日志文件。该文件保持为空。

我在这里阅读了相同问题的答案:

但我无法让任何建议发挥作用。例如,我使用了这里的 dockerfile:https://github.com/Ekito/docker-cron/

FROM ubuntu:latest
MAINTAINER docker@ekito.fr

# Add crontab file in the cron directory
ADD crontab /etc/cron.d/hello-cron

# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/hello-cron

# Create the log file to be able to run tail
RUN touch /var/log/cron.log

#Install Cron
RUN apt-get update
RUN apt-get -y install cron


# Run the command on container startup
CMD cron && tail -f /var/log/cron.log

crontab:

* * * * * root echo "Hello world" >> /var/log/cron.log 2>&1
# Don't remove the empty line at the end of this file. It is required to run the cron job

它在我的机器上不起作用(Windows 10)。显然,其他人似乎也报告了一个特定于 Windows 的问题:https://github.com/Ekito/docker-cron/issues/3

为了测试是否只是我做错了什么,我尝试在运行 ubuntu 的虚拟机中做同样的事情(所以是一个 ubuntu 主机而不是我的 windows 主机)并且按预期工作。日志文件按预期扩展。

那么我可以做些什么来尝试完成这项工作?

我尝试写入已安装(绑定)的文件夹并创建要写入的卷。都没有用。

【问题讨论】:

    标签: docker cron dockerfile docker-for-windows


    【解决方案1】:

    rferalli 在 github 问题上的回答对我有用:

    “有同样的问题。通过将 crontab 文件的行尾从 CRLF 更改为 LF 来修复它。希望这会有所帮助!”

    【讨论】:

      【解决方案2】:

      我也有这个问题。 我的解决方法是使用任务计划程序来运行一个启动容器的 .bat 文件

      使用任务计划程序https://active-directory-wp.com/docs/Usage/How_to_add_a_cron_job_on_Windows.html


      hello.bat

      docker run hello-world
      

      TaskScheduler 操作

      cmd /c hello.bat >> hello.log 2>&1
      

      希望有帮助:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-10-06
        • 1970-01-01
        • 2021-06-22
        • 1970-01-01
        • 2019-08-25
        • 1970-01-01
        • 2020-07-28
        • 1970-01-01
        相关资源
        最近更新 更多