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
CodePudding user response:
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.