【发布时间】:2023-01-22 11:24:45
【问题描述】:
我正在创建一个 shell 脚本应该每 1 小时运行一次的项目。我正在使用 cron 模式,以便每 15 分钟运行一次脚本。当我用 Ubuntu docker image 尝试这个时,一切都干净整洁。但是当涉及到 alpine 镜像时,运行 crond 服务存在一些问题。下面是我正在使用的 Dockerfile。
FROM alpine
RUN apk update
RUN apk add --no-cache tini openrc busybox-initscripts
RUN apk add --no-cache logrotate
COPY . .
在这里我使用busybox-initscripts安装 crond服务和openrc获得rc服务启用。
构建图像后,我将运行此容器作为与以下命令的交互
>>> docker build . -t alpine-test
*Build success*
>>> docker run -it alpine-test /bin/sh
/ # cat /etc/os-release
NAME="Alpine Linux"
ID=alpine
VERSION_ID=3.16.2
PRETTY_NAME="Alpine Linux v3.16"
HOME_URL="https://alpinelinux.org/"
BUG_REPORT_URL="https://gitlab.alpinelinux.org/alpine/aports/-/issues"
以下是我在 docker 容器 (alpine) 中运行的命令
当我尝试使用 rc-service 命令查看 crond 服务的状态时,它显示了一些警告。
/ # rc-service crond status
* You are attempting to run an openrc service on a
* system which openrc did not boot.
* You may be inside a chroot or you may have used
* another initialization system to boot this system.
* In this situation, you will get unpredictable results!
* If you really want to do this, issue the following command:
* touch /run/openrc/softlevel
在这里,我尝试使用触摸创建软级别
/ # touch /run/openrc/softlevel
touch: /run/openrc/softlevel: No such file or directory
但自从openrc路径不在那里。
/ # ls -la /run
total 12
drwxr-xr-x 1 root root 4096 Aug 10 15:35 .
drwxr-xr-x 1 root root 4096 Aug 11 00:39 ..
所以我使用mkdir手动创建了那个路径
/ # mkdir /run/openrc
然后就修好了
/ # touch /run/openrc/softlevel
之后我就可以运行 rc-service 命令
/ # rc-service crond status
* status: stopped
但是当我尝试启动该服务时,它警告我 crond 服务已经启动。
/ # rc-service crond start
* WARNING: crond is already starting
令人惊讶的是,当我检查状态时,它再次说 crond 服务已停止。
/ # rc-service crond status
* status: stopped
然后我尝试停止 crond 服务,重新启动服务。不幸的是,服务的状态只是停止了。
/ # rc-service crond stop
* ERROR: crond stopped by something else
/ # rc-service crond start
* WARNING: crond is already starting
/ # rc-service crond status
* status: stopped
/ # rc-service crond restart
* WARNING: crond is already starting
/ # rc-service crond status
* status: stopped
【问题讨论】:
-
Docker 容器运行单个进程;它不是运行具有多种服务的初始化系统的虚拟机。 this answer 到 How to run a cron job inside a docker container? 是否为您提供了有用的食谱?
标签: docker service cron alpine-linux