cron コンテナーの 1 つを実行する方法を次に示します。
Dockerfile:
FROM alpine:3.3
ADD crontab.txt /crontab.txt
ADD script.sh /script.sh
COPY entry.sh /entry.sh
RUN chmod 755 /script.sh /entry.sh
RUN /usr/bin/crontab /crontab.txt
CMD ["/entry.sh"]
crontab.txt
*/30 * * * * /script.sh >> /var/log/script.log
entry.sh
#!/bin/sh
# start cron
/usr/sbin/crond -f -l 8
script.sh
#!/bin/sh
# code goes here.
echo "This is a script, run by cron!"
そのように構築
docker build -t mycron .
そのように実行
docker run -d mycron
独自のスクリプトを追加して crontab.txt を編集し、イメージをビルドして実行するだけです。アルパインをベースにしているので画像は超小さいです。
クロンド Alpine では tiny でうまく動作します
RUN apk add --no-cache tini
ENTRYPOINT ["/sbin/tini", "--"]
CMD ["/usr/sbin/crond", "-f"]
ただし、ゾンビリープの問題とシグナル処理の問題のため、コンテナーのメインプロセス (PID 1) として実行しないでください。詳細については、この Docker PR とこのブログ投稿を参照してください。
@ken-cochrane のソリューションがおそらく最適ですが、追加のファイルを作成せずにそれを行う方法もあります。
余分なファイルなしでそれを行うには:
行く方法は、 entrypoint.sh
内にcronを設定することです ファイル。
Dockerfile
...
# Your Dockerfile above
COPY entrypoint.sh /
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
entrypoint.sh
echo "* * * * * echo 'I love running my crons'" >> /etc/crontabs/root
crond -l 2 -f > /dev/stdout 2> /dev/stderr &
# You can put the rest of your entrypoint.sh below this line
...