docker-compose&gt> entrypoint.sh
在我的开发环境中,我有一个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以以这种方式使用env vars:
在 docker-compose-prod.yml 中设置一个env var
,并在 entrypoint.sh 中使用它在
其他环境中(例如Dev)未设置var,并且entrypoint.sh不会在内部执行代码。希望它对您有帮助。
You can use ENV vars that way:
Set a ENV var in docker-compose-prod.yml
And use it in entrypoint.sh
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.