编程技术网

关注微信公众号,定时推送前沿、专业、深度的编程技术资料。

 找回密码
 立即注册

QQ登录

只需一步,快速开始

极客时间

Cron服务不是以Docker容器开头:Cron service not starting with docker container

namlt3820 docker 2022-5-10 15:07 7人围观

腾讯云服务器
Cron服务不是以Docker容器开头的处理方法

我一直在尝试在docker容器中运行cron作业,并且在容器启动时似乎无法使cron服务运行.我可以远程进入正在运行的容器并运行"cron"以使服务启动而不会出现问题.我已经将此文件包含在我的DockerFile中,为什么命令没有被执行?

I have been trying to run a cron job within my docker container and I can't seem to get the cron service to run when the container is started. I can remote into the running container and run "cron" to get the service to start without issues. I have this included in my DockerFile, why is the command not being executed?

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

I am able to get it to work by doing the following (as described above):

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 

问题解答

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

By default, docker will run onlt the CMD process. One hack is, add one entrypoint script that will run cron in background and then invoke CMD node application. But I will prefer running two containers, one for CRON and other for application that will help in separating out concerns fir containers. One container will run cron as CMD and other run node application.

如果要共享使用卷挂载可能的文件系统.

In case you want to share filesystem that is possible using volume mounts.

这篇关于Cron服务不是以Docker容器开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程技术网(www.editcode.net)!

腾讯云服务器 阿里云服务器
关注微信
^