在 MySQL Docker 容器上发布 运行 cron

Issue running cron on MySQL Docker container

我正在设置一个 cron 以将数据库转储导入 MySQL 容器。但是,我正在尝试测试 cron 以确保一切正常,但我没有得到任何输出。

我的 Dockerfile 是这样设置的

FROM mysql:8

RUN apt-get update
RUN apt-get -y install cron

COPY /crons/test-cron /etc/cron.d/test-cron

RUN chmod 0644 /etc/cron.d/test-cron

RUN crontab /etc/cron.d/test-cron

RUN touch /var/log/cron.log

MySQL 图像已成功创建,我的测试 cron 可以在 /etc/cron.d/test-cron 中找到,但是我从未将输出输出到我创建的 cron.log 文件。

test-cron

* * * * * root echo 'Written by cron' >> /var/log/cron.log 2>&1
# empty line

我的docker-compose.xml设置如下

version: '3'

services:
    data:
        build: ./data/
    db:
        build: ./db/
        volumes_from:
            - data
        volumes:
            - ./db/:/var/lib/mysql
        ports:
          - "3306:3306"
        environment:
          - MYSQL_DATABASE=myDB
          - MYSQL_USER=myUser
          - MYSQL_PASSWORD=Password
          - MYSQL_ROOT_PASSWORD=Password
          - MYSQL_ROOT_HOST=""

创建文件 crontab 并添加这样的条目

FROM mysql:8

RUN apt-get update && apt-get -y install cron

# Add crontab file in the cron directory
ADD crontab /etc/cron.d/test-cron

# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/test-cron

# Apply cron job
RUN crontab /etc/cron.d/test-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