Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to add crontab scheduled tasks to the docker container of debian

Shulou Source: shulou.com Published: 2022-06-01 13:24:12 09月16日 Update

This article will explain in detail how to add crontab timing tasks to debian's docker container. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Now most docke images are based on debian

# cat / etc/issueDebian GNU/Linux 9\ n\ l

The Docker container does not support background services, and services such as systemctl service crontab that run in the background cannot pass through the

RUN systemctl start nginx

Similar to this scheme, you have to write your own entrypoint script to start. This article records how the Debian-based docker container sets up scheduled tasks.

Case background

I deployed a front-end project to use the nginx image. Since the official image is based on debian and does not feel much different in size compared to alpine, I use debian as the container system.

Dockerfile looks like this.

FROM nginx:1.15.10MAINTAINER Ryan MiaoCOPY sources.list / etc/apt/sources.listRUN apt-get update & & apt-get install-y net-tools procps curl wget vim telnet cron, & & apt-get autoremove & & apt-get clean & & rm-rf / var/lib/apt/lists/*RUN mkdir-p / data/log/nginx & & mkdir-p / data/web & & rm / etc/nginx/conf.d/default.confADD default.conf / etc/nginx/conf.d/ADD index.html / data/web/ADD clean_log.sh / data/COPY clean-cron / etc/cron.d/clean-cronRUN chmod 755 / data/clean_log.sh & & crontab / Etc/cron.d/clean-cronENTRYPOINT nginx & cron & & / bin/bash

Probably just install cron, then replace nginx config, then copy our static file, and finally start nginx and start cron.

Tell me why there are scheduled tasks. You can see that we have a regular cleaning script, because nginx itself does not provide a log processing module, so there is a cleaning script. The cleanup script needs to be executed regularly, so there are scheduled tasks, and it is found that the docker container does not support service.

In general, there are the following steps:

Install

Apt-get install cron

Add to crontab

Crontab / etc/cron.d/your-crontab

Start cron when docker starts

ENTRYPOINT cron & & xxxxx

This is the end of this article on "how to add crontab scheduled tasks to debian's docker container". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

Tags: Containers tasks scripts articles background more support services good practical volume content front end most official that is generally speaking files articles methods Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft vpn Xiaomi Linux OPPO Reno