docker-compose&gt> entrypoint.sh

发布于 2025-02-08 17:58:51 字数 2331 浏览 2 评论 0原文

在我的开发环境中,我有一个docker-compose.yml,该从dockerfile构建图像。

然后,我将其推到Dockerhub,然后我的产品服务器使用该图像(使用Docker-Compose-prod.yml)。在产品中,我正在使用docker-swarm(目前三个节点)。

构建图像时,在dockerfile中,它使用entrypoint.sh,因此我可以在同一容器中同时运行Apache和Cron。

这是我正在使用的Web应用程序的要求(尽管理想情况下,我将Cron分为一个单独的容器,但这是不切实际的)。

结果是,无论环境如何即,每当我docker-compots在我的开发环境中时,crontab都会运行。由于多种原因,这是不希望的。

我已经有不同的docker-compose.yml dev vs vs prod(* - prod.yml)上的文件,但是启动crontab的任务在entrypoint.sh中嵌入在图像中。

因此,无论如何,我是否可以在entrypoint.sh中传递某种类型的服务器标签/环境变量,以便在其他任何产品上,它不会启动/添加crontab?

例如: new-entrypoint.sh

#!/bin/bash

if NODE IS LIVE
{
  # Setup a cron schedule
  echo "* * * * * /usr/local/bin/php /var/www/html/cron.php >> /var/log/cron.log 2>&1
  # This extra line makes it a valid cron" > /var/www/scheduler.txt

  crontab /var/www/scheduler.txt
  #cron
  service cron start
}

/usr/sbin/apache2ctl -D FOREGROUND

如果我使用的是.env文件和变量,则首先不确定我如何在entrypoint.sh中引用该文件我如何管理开发和产品之间的不同的.ENV文件(请记住我作为Docker-warm正在运行的产品)。

或者,我可以使用一个节点标签(就像我可以为Docker-Compose≫ deployement> gt;约束》中使用?

理想情况下,默认情况下,它不会运行crontab,只有在满足特定的Env或TAG时才运行。

谢谢!

文件

docker-compose.yml

version: "3.5"
services:
  example:
    build:
      context: .
      dockerfile: ./example.Dockerfile
    image: username/example
...

docker-compose-prod.yml

version: '3.5'
services:
  example:
    image: username/example
...

dockerfile

FROM php:7.4-apache

RUN ....

# MULTIPLE ENTRYPOINTS to enable cron and apache
ADD entrypoint.sh /entrypoint.sh
 
RUN chmod +x /entrypoint.sh

ENTRYPOINT /entrypoint.sh

entrypoint.sh

#!/bin/bash

# Setup a cron schedule
echo "* * * * * /usr/local/bin/php /var/www/html/cron.php >> /var/log/cron.log 2>&1
# This extra line makes it a valid cron" > /var/www/scheduler.txt

crontab /var/www/scheduler.txt
#cron
service cron start

/usr/sbin/apache2ctl -D FOREGROUND

In my development environment, I have a docker-compose.yml which builds an image from a Dockerfile.

I then push this to dockerhub, and my PROD servers use that image (using docker-compose-prod.yml). In PROD I'm using docker-swarm (currently three nodes).

When building the image, in the Dockerfile, it uses entrypoint.sh so I can run both apache and cron within the same container.

This is a requirement of the web app I'm working with (whilst I'd ideally split the cron out into a separate container, it's not practical).

The result is that the crontab runs no matter the environment; i.e. whenever I docker-compose up in my DEV environment, the crontab runs. For a variety of reasons, this isn't desired.

I already have different docker-compose.yml files on DEV vs PROD (*-prod.yml), but the task to start crontab is within entrypoint.sh which is embedded within the image.

So, is there anyway I can pass some type of server tag / environment variable within entrypoint.sh so on anything other than PROD, it doesn't start/add the crontab?

For example:
new-entrypoint.sh

#!/bin/bash

if NODE IS LIVE
{
  # Setup a cron schedule
  echo "* * * * * /usr/local/bin/php /var/www/html/cron.php >> /var/log/cron.log 2>&1
  # This extra line makes it a valid cron" > /var/www/scheduler.txt

  crontab /var/www/scheduler.txt
  #cron
  service cron start
}

/usr/sbin/apache2ctl -D FOREGROUND

If I was the use an .ENV file and variable, I'm firstly not sure how I'd reference that in the entrypoint.sh but also how do I manage different .ENV files between DEV and PROD (bear in mind I'm running PROD as a docker-swarm).

Or, can I use a node tag (like I can for docker-compose > deploy > placement > constraints)?

Ideally it'll, by default NOT run the crontab, and only run from if a specific ENV or tag is met.

Thanks!

FILES

docker-compose.yml

version: "3.5"
services:
  example:
    build:
      context: .
      dockerfile: ./example.Dockerfile
    image: username/example
...

docker-compose-prod.yml

version: '3.5'
services:
  example:
    image: username/example
...

Dockerfile

FROM php:7.4-apache

RUN ....

# MULTIPLE ENTRYPOINTS to enable cron and apache
ADD entrypoint.sh /entrypoint.sh
 
RUN chmod +x /entrypoint.sh

ENTRYPOINT /entrypoint.sh

entrypoint.sh

#!/bin/bash

# Setup a cron schedule
echo "* * * * * /usr/local/bin/php /var/www/html/cron.php >> /var/log/cron.log 2>&1
# This extra line makes it a valid cron" > /var/www/scheduler.txt

crontab /var/www/scheduler.txt
#cron
service cron start

/usr/sbin/apache2ctl -D FOREGROUND

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

溺ぐ爱和你が 2025-02-15 17:58:51

您可以以这种方式使用env vars:

docker-compose-prod.yml 中设置一个env var

    version: '3.5'
    services:
      example:
        image: username/example
        environment:
          APP_ENV: prd
        ...

,并在 entrypoint.sh 中使用它在


    #!/bin/bash
    if [ "$APP_ENV" == "prd" ]
    then
      # Setup a cron schedule
      echo "* * * * * /usr/local/bin/php /var/www/html/cron.php >> /var/log/cron.log 2>&1
      # This extra line makes it a valid cron" > /var/www/scheduler.txt
      crontab /var/www/scheduler.txt
      #cron
      service cron start
    fi
    /usr/sbin/apache2ctl -D FOREGROUND

其他环境中(例如Dev)未设置var,并且entrypoint.sh不会在内部执行代码。希望它对您有帮助。

You can use ENV vars that way:

Set a ENV var in docker-compose-prod.yml

    version: '3.5'
    services:
      example:
        image: username/example
        environment:
          APP_ENV: prd
        ...

And use it in entrypoint.sh


    #!/bin/bash
    if [ "$APP_ENV" == "prd" ]
    then
      # Setup a cron schedule
      echo "* * * * * /usr/local/bin/php /var/www/html/cron.php >> /var/log/cron.log 2>&1
      # This extra line makes it a valid cron" > /var/www/scheduler.txt
      crontab /var/www/scheduler.txt
      #cron
      service cron start
    fi
    /usr/sbin/apache2ctl -D FOREGROUND

In others environments (DEV for example) the var is not set, and the entrypoint.sh doesn't execute code inside if. Hope it helps you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文