如何在MariaDB容器中运行cron? [英] How can I run a cron in MariaDB container?

查看:79
本文介绍了如何在MariaDB容器中运行cron?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据最新的图像在MariaDB容器中包含一个cron任务,但我对此坚持不懈。

I want to include a cron task in a MariaDB container, based on the latest image mariadb, but I'm stuck with this.

由于无法同时启动MariaDB和Cron,我尝试了很多事情,但均未成功。

I tried many things without success because I can't launch both MariaDB and Cron.

这是我实际的dockerfile:

Here is my actual dockerfile:

FROM mariadb:10.3

# DB settings
ENV MYSQL_DATABASE=beurre \
    MYSQL_ROOT_PASSWORD=beurette

COPY ./data /docker-entrypoint-initdb.d
COPY ./keys/keys.enc home/mdb/
COPY ./config/encryption.cnf /etc/mysql/conf.d/encryption.cnf

# Installations
RUN apt-get update && apt-get -y install python cron

# Cron
RUN touch /etc/cron.d/bp-cron
RUN printf '* * * * * root echo "Hello world" >> /var/log/cron.log 2>&1\n#' >> /etc/cron.d/bp-cron
RUN touch /var/log/cron.log
RUN chmod 0644 /etc/cron.d/bp-cron

RUN cron

使用其设置,数据库可以正确启动,但未初始化 Cron。要使其正常工作,我必须进入容器并执行 Cron命令,然后一切正常运行。

With its settings, the database starts correctly, but "Cron" is not initialized. To make it work, I have to get into the container and execute the "Cron" command, and everything works perfectly.

所以我正在寻找一种启动方法

So I'm looking for a way to launch both the db and cron from my Dockerfile used in my docker-compose.

如果这不可能,也许还有另一种方法来计划任务?

If this is not possible, maybe there is another way to do tasks planned? The purpose being to execute a script of the db.

推荐答案

详细说明@ k0pernikus的注释,我建议使用一个单独的容器运行cron。然后,该容器中的cronjobs即可与您的mysql数据库一起使用。

Elaborating on @k0pernikus's comment, I would recommend to use a separate container that runs cron. The cronjobs in that container can then work with your mysql database.

这是我的处理方式:

您可以非常简单地设置cron容器。这是一个应该完成此工作的Dockerfile示例:

You can set up a cron container fairly simply. Here's an example Dockerfile that should do the job:

FROM alpine
COPY ./crontab /etc/crontab
RUN crontab /etc/crontab
RUN touch /var/log/cron.log
CMD crond -f

只需将您的crontab放入该Dockerfile旁边的 crontab 文件中,您就应该有一个工作的cron容器。

Just put your crontab into a crontab file next to that Dockerfile and you should have a working cron container.

示例crontab文件:

An example crontab file:

* * * * * mysql -h mysql --execute "INSERT INTO database.table VALUES 'v';"



2。将cron容器作为服务添加到docker-compose.yml中



确保将cron容器添加到docker-compose.yml中,并将其放入同一容器中网络作为您的mysql服务:

2. Add the cron container to your docker-compose.yml as a service

Make sure you add your cron container to the docker-compose.yml, and put it in the same network as your mysql service:

networks:
    my_network:
services:
    mysql:
        image: mariadb
        networks:
          - my_network
    cron:
        image: my_cron
        depends_on: 
          - mysql
        build:
            context: ./path/to/my/cron-docker-folder
        networks:
          - my_network

这篇关于如何在MariaDB容器中运行cron?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆