ubuntu 16.04 的 docker 镜像中缺少 cron 和 crontab

ubuntu 16.04 的 docker 镜像中缺少 cron 和 crontab

这是我的 Dockerfile

FROM ubuntu:16.04
RUN apt-get update -y && apt-get install -y \
  git \
  python \
  python-pip

创建 docker 镜像后,我登录并尝试设置一个 cron 作业进行测试。令我惊讶的是,croncrontab都不存在。

# ls 
app  bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  
root  run  sbin  srv  sys  tmp  usr  var
# crontab -l
/bin/sh: 6: crontab: not found
# crontab -l
/bin/sh: 7: crontab: not found
# crontab -l
/bin/sh: 10: crontab: not found
# cron
/bin/sh: 11: cron: not found

但我希望cron它出现在 ubuntu 镜像中。我选错了镜像吗?还是我需要做什么才能启用cron

答案1

镜像 ubuntu:16.04 中默认未安装 cron 命令

需要运行apt-get install cron

答案2

Docker 镜像在设计上是极简的,它们用于创建容器,而不是完整的操作系统。容器隔离应用程序的运行,因此默认情况下,它不会在该环境中运行所有其他操作系统守护程序,如 cron、syslog、mail 等。

你可以使用以下命令安装 cron:

RUN apt-get update \
 && DEBIAN_FRONTEND=noninteractive apt-get install \
      cron \
 && apt-get clean \
 && rm -rf /var/lib/apt/lists/*

在您的 Dockerfile 中。但是,要运行 crontab 条目,您还需要在容器启动过程中启动 cron 守护程序。您可以使用 forego 和 supervisord 等工具在容器中运行多个进程(cron 加上您的应用程序),但这样做通常是反模式的标志。

相关内容