【问题标题】:how to start cron when starting a Rbase docker container启动 Rbase docker 容器时如何启动 cron
【发布时间】:2020-02-07 01:55:37
【问题描述】:

我正在尝试构建一个运行 R 和 cron 的 docker 容器。我需要的是让 cron 在我启动容器时自动运行。

我的 dockerfile 如下:

# Install R version 3.6
FROM r-base:3.6.0

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

# also tried CMD /etc/init.d/cron start
CMD cron

然后我构建了映像并在 bash 中运行容器。 我检查了 cron 的状态:

/etc/init.d/cron status

我得到了如下的 cron 状态:

[FAIL] cron is not running ... failed!

我可以通过手动启动 cron 来启动 cron:

/etc/init.d/cron start

我的问题是我应该如何修改我的dockerfile(CMD行),以便当docker容器启动时,cron自动启动?

提前非常感谢。

【问题讨论】:

  • 当您尝试CMD /etc/init.d/cron start 时会发生什么?
  • 与 CMD cron 相同。当我进入 bash 中的容器并检查状态时。 cron 没有运行。

标签: r docker cron


【解决方案1】:

CMD /etc/init.d/cron start 将在后台启动 cron,因此您的容器将在创建后立即死亡。

在第二个选项中,添加-f

# Install R version 3.6
FROM r-base:3.6.0

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

# also tried CMD /etc/init.d/cron start
CMD [ "cron", "-f" ]

所以它会让你的容器保持运行。

-f
Stay in foreground mode, don't daemonize.

但是您将无法使用/etc/init.d/cron status 查看 cron。在 dokcerfile 下面使用。

FROM r-base:3.6.0
RUN apt-get update &&  apt-get -y install cron
RUN apt-get install procps -y
CMD ["cron" ,"-f"]

然后运行

docker exec -it <your_container_id> bash -c "ps -aux"

你会看到 cron 正在运行。

【讨论】:

  • 谢谢。我刚试过。我没有看到 cron 运行。您是否尝试过您的解决方案? @Adiii
  • 是的,您是否与ps - aux 核对过?
  • 同样构建没有缓存和新图像的图像
  • 抱歉,我是 docker 新手。你是什​​么意思'构建没有缓存和新图像的图像'?你能举一个像docker build这样的例子吗......
  • 只需添加--no-cache
猜你喜欢
  • 1970-01-01
  • 2014-09-24
  • 1970-01-01
  • 2015-08-07
  • 2016-12-16
  • 2019-11-13
  • 2019-11-23
  • 2018-04-04
  • 1970-01-01
相关资源
最近更新 更多