【发布时间】:2019-11-16 02:18:12
【问题描述】:
我有一个简单的 Dockerfile 和 docker compose 设置来测试容器中的 cron。
作为最后一步,启动了 cron,我收到消息 cron has started,但是当我登录到容器并检查 cron 服务的状态时,它没有运行。这怎么可能?
我相信我误解了某些东西,但无法弄清楚它到底是什么。
Dockerfile:
FROM ubuntu:bionic
# Create the log file to be able to run tail
RUN touch /var/log/cron.log
# Install Cron
RUN apt-get update && apt-get install cron
# Install the crontab
COPY crontab /etc/cron.d/crontab
# Is this needed??
RUN touch /var/log/cron.log
RUN chmod 777 /var/log/cron.log
# Run the command on container startup
CMD service cron start && tail -f /var/log/cron.log
docker-compose.yml
version: '3'
services:
cron:
# build a custom image
build:
context: .
dockerfile: Dockerfile
# a name for easier reference
image: cron
文件crontab有效,包含:
* * * * * echo "Hello world" >> /var/log/cron.log 2>&1
# Don't remove the empty line at the end of this file. It is required.
然后开始:docker-compose up --build
输出是:
Creating network "docker-cron_default" with the default driver
Building cron
Step 1/8 : FROM ubuntu:bionic
---> 4c108a37151f
Step 2/8 : MAINTAINER wouter.samaey@storefront.be
---> Using cache
---> 0d9fa7049481
Step 3/8 : RUN touch /var/log/cron.log
---> Using cache
---> 39bb838fe945
Step 4/8 : RUN apt-get update && apt-get install cron
---> Using cache
---> d3ce6cc03821
Step 5/8 : COPY crontab /etc/cron.d/crontab
---> Using cache
---> fab99f2e2e77
Step 6/8 : RUN touch /var/log/cron.log
---> Using cache
---> c7fab49def98
Step 7/8 : RUN chmod 777 /var/log/cron.log
---> Using cache
---> 7dc00a5913bd
Step 8/8 : CMD service cron start && tail -f /var/log/cron.log
---> Running in a4bdec436613
Removing intermediate container a4bdec436613
---> 97c867e25091
Successfully built 97c867e25091
Successfully tagged cron:latest
Creating docker-cron_cron_1 ... done
Attaching to docker-cron_cron_1
cron_1 | * Starting periodic command scheduler cron
cron_1 | ...done.
但是当我在这个容器上打开 bash shell 时,cron 没有运行:
docker run -it cron /bin/bash
root@d6649b402133:/# /etc/init.d/cron status
* cron is not running
这怎么可能?
【问题讨论】:
-
ps aux在容器中为您提供了什么?我想知道您发出的命令是否仅用于服务,但 Cron 并未作为服务运行。 -
ps aux仅显示 2 行。没有cron。很奇怪为什么只有2行...
标签: linux docker cron docker-compose