【问题标题】:Cron service not starting with docker containerCron 服务没有从 docker 容器开始
【发布时间】:2019-07-28 12:04:20
【问题描述】:

我一直在尝试在我的 docker 容器中运行 cron 作业,但我似乎无法在容器启动时运行 cron 服务。我可以远程进入正在运行的容器并运行“cron”以使服务启动而不会出现问题。我的 DockerFile 中包含了这个,为什么没有执行命令?

我可以通过执行以下操作(如上所述)使其工作:

docker-compose build
docker-compose up -d
docker exec -it my_container /bin/bash
root@2348723ae34: /etc/init.d/cron status <--check cron service status
[FAIL] cron is not running ... failed!  <--- cron not running
root@2348723ae34: cron
[ ok ] cron is running. <-- simply running cron starts the service

crontab 文件

*/2 * * * * rm -rf /usr/src/app/assets/aligned_output/* && rm -rf /usr/src/app/assets/aligned_input/*

DockerFile

FROM node:latest

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY . /usr/src/app

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

# Add crontab file in the cron directory
ADD crontab /etc/cron.d/hello-cron

# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/hello-cron

# Apply cron job
RUN crontab /etc/cron.d/hello-cron

# Create the log file to be able to run tail
RUN touch /var/log/cron.log

# Run the command on container startup
CMD cron && tail -f /var/log/cron.log

RUN touch /etc/crontab /etc/cron.*/*

EXPOSE 80
RUN npm install
CMD ["npm", "start"]

docker-compose.yml

inventory:
   build: .
   restart: always
   command: npm start
   ports:
   - "80:80"
   environment:
      - NODE_ENV=production

【问题讨论】:

  • 我怀疑(猜想!)第二个CMD(仅)是受影响的。这将解释(我假设)您的 Node 应用程序正在运行,但 cron 没有。你能把这两个进程分开到它们自己的容器中吗?您需要在其容器的前台运行 cron,以免容器立即退出。

标签: docker cron docker-compose cron-task


【解决方案1】:

默认情况下,docker 将只运行CMD 进程。一种技巧是,添加一个 entrypoint 脚本,该脚本将在后台运行 cron,然后调用 CMD 节点应用程序。 但我更喜欢运行两个容器,一个用于 CRON,另一个用于应用程序,这将有助于分离出容器的关注点。一个容器将运行cron 作为CMD 和其他运行节点应用程序。

如果您想共享可以使用卷挂载的文件系统。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-28
    • 2018-12-15
    • 1970-01-01
    相关资源
    最近更新 更多