Cron pro docker-compose stack

Viewed 14

Mám hotový stack vytvořený pomocí docker-compose a potřebuju v něm spustit cron. Jak na to?

1 Answers

Nakonec jsem to vymyslel tak, že jsem přidal třetí container s cronem:

Well, as a matter of fact, I have solved it, but it took me some time to get it all together. Apart from "itflow" and "itflow-db" containters I have added a third one "itflow-cron". Then I created a Dockerfile.cron from which the container is build and a crontab file that is then copied to itflow-cron container. It seems to work rather nicely. Here is what worked for me:

docker-compose.yml

version: "3.3"
########################### NETWORKS
networks:
  reverseproxy-nw:
    external: true
  itflow-db:
    external: false
########################### ITFLOW
services:
  itflow:
    hostname: itflow
    container_name: itflow
    # Comment out build for docker.io image
    image: lued/itflow
    # build: .
    restart: unless-stopped
    depends_on:
      - itflow-db
    networks:
      - reverseproxy-nw
      - itflow-db
    ports:
      - "80:8080"
    environment:
      - TZ=$TZ
      - ITFLOW_NAME=ITFlow
      - ITFLOW_URL=support.$ROOT_DOMAIN
      - ITFLOW_PORT=8080
      - ITFLOW_REPO=github.com/itflow-org/itflow
      - ITFLOW_REPO_BRANCH=master
      - ITFLOW_LOG_LEVEL=info
      - ITFLOW_DB_HOST=itflow-db
      - ITFLOW_DB_PASS=$ITFLOW_DB_PASS
    volumes:
      - /services/docker/itflow/data:/var/www/html

  itflow-db:
    hostname: itflow-db
    container_name: itflow-db
    image: mariadb:10.6.11
    restart: always
    networks:
      - itflow-db
    ports:
      - "13936:3306"
    environment:
      - MARIADB_RANDOM_ROOT_PASSWORD=true
      - MARIADB_DATABASE=itflow
      - MARIADB_USER=itflow
      - MARIADB_PASSWORD=$ITFLOW_DB_PASS
      - MARIADB_HOST=%
    volumes:
      - /services/docker/itflow/db:/var/lib/mysql/
      - /services/docker/itflow/db/scripts:/docker-entrypoint-initdb.d

  itflow-cron:
    hostname: itflow-cron
    container_name: itflow-cron
    build:
      context: /services/docker/itflow/config
      dockerfile: Dockerfile.cron
    volumes:
      - /services/docker/itflow/data:/var/www/html
      - /services/docker/itflow/config:/mnt/config
    networks:
      - reverseproxy-nw
      - itflow-db
    restart: unless-stopped

Dockerfile.cron:

FROM php:8.2-cli-buster

# Set working directory
WORKDIR /var/www/html

# Install dependencies
RUN apt-get update && apt-get install -y cron libltdl7 libc-client-dev libkrb5-dev
RUN docker-php-ext-configure imap --with-kerberos --with-imap-ssl
RUN docker-php-ext-install mysqli imap
RUN docker-php-ext-enable mysqli imap
RUN pecl install mailparse && docker-php-ext-enable mailparse

# Set your timezone here
RUN ln -snf /usr/share/zoneinfo/${TZ} /etc/localtime && echo ${TZ} > /etc/timezone

# Copy crontab file to the cron.d directory as a itflow-cron file
COPY crontab /etc/cron.d/itflow-cron

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

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

crontab:

* * * * * /usr/local/bin/php /var/www/html/cron_ticket_email_parser.php [KEY] >> /var/log/cron_ticket_email_parser.log 2>&1
* * * * * /usr/local/bin/php /var/www/html/cron_mail_queue.php [KEY] >> /var/log/cron_mail_queue.log 2>&1
0 2 * * * /usr/local/bin/php /var/www/html/cron.php [KEY] >> /var/log/cron.log 2>&1
# don't remove the empty line at the end

Btw there is an error in docs here, because the email parser script has a different name now.

Then run docker-compose build, docker-compose up -d and you should be ready to go.

P.S.: Don't forget to create .env file with variables that are referenced in docker-compose.yml. And if you are using Portainer, this does not work because Portainer has problems with paths referenced during build and there currently seems to be no way around it.