Rob
Rob

Reputation: 133

Issue running cron on MySQL Docker container

I am working on setting up a cron to import database dumps into a MySQL container. However, I am trying a test cron to make sure everything is working correctly but I get no output.

My Dockerfile is setup as such

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

The MySQL image is successfully created and my test-cron can be found in /etc/cron.d/test-cron however I never get an output to the cron.log file I created.

test-cron

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

My docker-compose.xml is setup as the following

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=""

Upvotes: 1

Views: 1257

Answers (1)

Ashok
Ashok

Reputation: 3611

Create file crontab and add an entry like this

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

Upvotes: 2

Related Questions