Docker - 从 CRON 容器调用 Web 容器上的 PHP 脚本

Docker - Call PHP script on web container from a CRON container

我在 docker 容器(apache,php)中有一个网络应用 运行ning。 我一直在寻找安装 cron 作业的解决方案,以便定期在我的网络应用程序上执行一些操作(执行 php 文件等)。

我找到了多个答案 (),所有答案都基于创建一个单独的容器,该容器将负责 运行 cron 作业。

现在,如何使这个 cron 容器与我的 Web 容器通信?

我找到了多个解决方案:

你们有其他想法或代码示例来帮助我实现这个目标吗?

设法使用我的网络容器和我的 cron 容器之间的共享网络使其工作(感谢@David Maze)

Cron Docker文件:

FROM ubuntu:18.04

RUN apt-get update
RUN apt-get install -y systemd
RUN apt-get install -y nano
RUN apt-get install -y cron
RUN apt-get install -y curl
 
RUN systemctl enable cron
RUN (crontab -l -u root; echo "* * * * * curl web:80 -d 'action=cron.run'") | crontab

# 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

Docker撰写

version: '3.7'

services:

    # Web container
    web:
        # [...]
        ports:
            - "0001:80"
            - "0002:443"
        networks:
            - cron_network

    # Cron container
    cron:
        # [...]
        depends_on:
            - web
        networks:
          - cron_network

networks:
  cron_network: