在Alpine容器中启用crond

时间:2019-07-05 10:58:20

标签: docker cron alpine

上下文

我正在尝试安排一些在 Alpine 容器中的摄取工作。我花了一段时间了解为什么我的 cron作业没有启动:crond似乎没有运行

rc-service -l | grep crond 

根据Alpine's documentation,crond必须首先以openrc(即某种systemctl)开头。这是Dockerfile

FROM python:3.7-alpine

# set work directory
WORKDIR /usr/src/collector

RUN apk update \
    && apk add curl openrc

# ======>>>> HERE !!!!! 
RUN rc-service crond start && rc-update add crond

# install dependencies
RUN pip install --upgrade pip
RUN pip install pipenv
COPY ./Pipfile /usr/src/collector/Pipfile
RUN pipenv install --skip-lock --system --dev

# copy entrypoint.sh
COPY ./entrypoint.sh /usr/src/collector/entrypoint.sh

# copy project
COPY . /usr/src/collector/

# run entrypoint.sh
ENTRYPOINT ["/usr/src/collector/entrypoint.sh"]

entrypoint.sh仅将作业附加在/etc/crontabs/root

的末尾

问题

我遇到以下错误:

 * rc-service: service `crond' does not exist
ERROR: Service 'collector' failed to build: The command '/bin/sh -c rc-service crond start && rc-update add crond' returned a non-zero code: 1

事情开始变得有些循环。 rc-service如何同时无法识别服务:

  • sh似乎知道名字crond

  • 有一个/etc/crontabs/root

我想念什么?

4 个答案:

答案 0 :(得分:1)

我能够通过在实际脚本命令之前将 crond 命令添加到 docker-entrypoint.sh 来解决此问题。

例如:

#!/bin/sh
set -e

crond &

...(rest of the original script)

通过这种方式,crond 被重新加载为一个分离的进程。

所以所有需要的步骤是

  • 找到 entrypoint.sh 并将其复制到 build 文件夹。我从正在运行的容器中执行此操作:
docker cp <running container name>:<path to script>/entrypoint.sh <path to Dockerfile folder>/entrypoint.sh
  • 如上所述修改entrypoint.sh
  • 在用于构建自定义映像的 Dockerfile 中再次包含 entrypoint.sh。 Dockerfile 示例:
...
COPY docker-entrypoint.sh <path to script>/entrypoint.sh
RUN chmod +x <path to script>/entrypoint.sh
...
  • 然后构建并使用新的自定义图像:
docker build -t <registry if used>/<image name>:<tag> .

答案 1 :(得分:0)

某些Alpine Docker容器缺少 busybox-initscripts package 。只需将其附加到您的apk add命令的末尾,crond应该作为服务运行。

您可能还需要从Dockerfile中删除以下行,因为似乎 busybox-initscripts 在安装后立即将crond作为服务运行:

RUN rc-service crond start && rc-update add crond

答案 2 :(得分:0)

运行此命令:apk add openrc --no-cache

答案 3 :(得分:0)

也遇到过这个问题。我对容器的解决方案是在 screen

内执行 crond
# apk add screen --no-cache
# screen -dmS crond crond -f -l 0