Add crontab scheduled tasks to debian docker container

Add crontab scheduled tasks to debian docker container

Now most of the Docker images are based on Debian

# cat /etc/issue
Debian GNU/Linux 9 \n \l

Docker containers do not support background services. Background services such as systemctl service crontab cannot be accessed through

RUN systemctl start nginx

To implement a solution like this, you must write your own entrypoint script to start it. This article records how to set up scheduled tasks in a Debian-based Docker container.

Case Background

I deployed a front-end project and used the nginx image. Since the official image is based on Debian, the size is not much different compared to Alpine, so I used Debian as the container system.

The Dockerfile looks like this

FROM nginx:1.15.10
MAINTAINER Ryan Miao

COPY sources.list /etc/apt/sources.list
RUN 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.conf
ADD default.conf /etc/nginx/conf.d/
ADD index.html /data/web/

ADD clean_log.sh /data/
COPY clean-cron /etc/cron.d/clean-cron
RUN chmod 755 /data/clean_log.sh && crontab /etc/cron.d/clean-cron

ENTRYPOINT nginx && cron && /bin/bash

It's probably about installing cron, then replacing nginx config, then copying our static files, and finally starting nginx and starting cron.

Let me tell you why there are timed tasks. You can see that we have a scheduled cleanup script. Since nginx itself does not provide a log processing module, there is a cleanup script. The cleanup script needs to be executed regularly, so there is a scheduled task, and then it is found that the docker container does not support service.

In general, there are several steps:

install

apt-get install cron

add to crontab

crontab /etc/cron.d/your-crontab

Start cron when docker starts

ENTRYPOINT cron && xxxxx

ps, many people still prefer alpine as the image matrix because it is small. But this Linux command is indeed unfamiliar.

Summarize

The above is what I introduced to you about adding crontab scheduled tasks to Debian Docker containers. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!
If you find this article helpful, please feel free to reprint it and please indicate the source. Thank you!

You may also be interested in:
  • Summary of some reasons why crontab scheduled tasks are not executed
  • How to implement second-level scheduled tasks with Linux Crontab Shell script
  • How to use crontab to execute a scheduled task once a second in Linux
  • Reasons why crontab scheduled tasks are not executed in Linux
  • Example of using django-crontab to implement scheduled tasks

<<:  MySQL 8.0.11 installation and configuration method graphic tutorial

>>:  Summary of 7 pitfalls when using react

Recommend

How to construct a table index in MySQL

Table of contents Supports multiple types of filt...

Detailed tutorial on installing Docker and nvidia-docker on Ubuntu 16.04

Table of contents Docker Installation Nvidia-dock...

SSH port forwarding to achieve intranet penetration

The machines in our LAN can access the external n...

Mysql optimization Zabbix partition optimization

The biggest bottleneck of using zabbix is ​​the d...

Start nginxssl configuration based on docker

Prerequisites A cloud server (centOS of Alibaba C...

A solution to the abnormal exit of Tomcat caused by semaphore

I'm playing with big data recently. A friend ...

Summary of using MySQL online DDL gh-ost

background: As a DBA, most of the DDL changes of ...

Solution to high CPU usage of Tomcat process

Table of contents Case Context switching overhead...

How to visualize sketched charts in Vue.js using RoughViz

introduce A chart is a graphical representation o...

Analysis of permissions required to run docker

Running Docker requires root privileges. To solve...

Basic notes on html and css (must read for front-end)

When I first came into contact with HTML, I alway...

Tutorial on installing lamp-php7.0 in Centos7.4 environment

This article describes how to install lamp-php7.0...

MySQL 5.7.19 installation and configuration method graphic tutorial (win10)

Detailed tutorial on downloading and installing M...

An example of how JavaScript can prevent duplicate network requests

Preface During development, we often encounter va...