【发布时间】: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