Closed
Description
Steps To Reproduce
- I am running 8.3-fpm-apache with a bunch of custom entrypoint scripts
- as of 3.4.0 my later scripts are not executed because my
11-xxx.shends with aexit 0, this causes thewhileloop to terminate early
# Execute scripts from /etc/entrypoint.d/ in numeric order
find /etc/entrypoint.d/ -type f -name '*.sh' | sort -n -t- -k1 | while IFS= read -r f; do
if [ -e "$f" ]; then
if [ "$LOG_OUTPUT_LEVEL" = "debug" ]; then
echo "Executing $f"
fi
if ! . "$f"; then
echo "Error executing $f" >&2
exit 1
fi
else
echo "Warning: $f not found" >&2
fi
doneOutcome
What did you expect?
all entrypoint scripts should execute successfully
What happened instead?
only scripts until my 11-xxx.sh scripts are executed, all subsequent scripts are skipped
Affected Docker Images
serversideup/php:8.3-fpm-apache (at least)
Anything else?
This is copilots take on the problem

To be honest I have no idea why it did work with 3.3.0 and lower, the change from #410 does not really appear to make any difference in that regard (to me), but it seems to do it. It would be more robust if you can use your good old exit <code> in your scripts without breaking the startup sequence.
Reacted by Lukas Bachlechner
Activity
jaydrogers commentedon Oct 23, 2024
So this is the culprit:
docker-php/src/common/usr/local/bin/docker-php-serversideup-entrypoint
Lines 42 to 54 in 825625b
Line 47 is where it gets executed.
I forget the exact reason why we did it this way, but if there is a better way to execute sub-scripts -- I am all ears.
To get your script to exit properly, you need to use
return 0. Theexit 0will stop since it's using the same shell.rburgst commentedon Oct 24, 2024
found this https://unix.stackexchange.com/questions/730580/can-a-script-be-run-as-a-subshell which seems to match with an earlier implementation of this script.
jaydrogers commentedon Oct 24, 2024
I wonder if @szepeviktor would have any opinions on this as he is a shell wizard 🧙
szepeviktor commentedon Oct 24, 2024
.is the short version ofsource.sourcedoes not start a subshell,$fis executed in the same shell environment. - Hereexitis dangerous.sh $for./fdoes open a subshell. - A subshell isexit-tolerant,exitwill exit from the subshell but not from the current script.jaydrogers commentedon Oct 24, 2024
Thanks! Makes sense.
I guess my question is Which method should we follow? Existing shell or subshell?
In the case of allowing people to run their own scripts, should we put them in their own subshell or keep it all within our existing shell?
We would need the environment variables to be accessible from Docker Compose files, etc, so I think that might be a reason why I went with the same shell? It's been a while since I looked at this and I forgot why I went this way -- but it was my first time building something like this so that's why I opened this up for discussion
szepeviktor commentedon Oct 25, 2024
There is no solution 💣 only work-arounds.
Shell scripting is for simple tasks, there is no security in shell.
The only thought that does not make my stomach turn is: use
exportfor every variable you want to make available.jaydrogers commentedon Nov 7, 2024
Thanks @szepeviktor!
@rburgst: Are you okay with just using
return 0instead ofexit 0?rburgst commentedon Nov 7, 2024
I have changed our scripts accordingly however it’s a bit unintuitive or might be actually a footgun.
jaydrogers commentedon Nov 7, 2024
I am all ears on proposals.
I have @cappuc who submitted a PR to have it be
return 0and it seems like you're suggesting it to beexit 0.We can't support both, so let's work together as a community and figure out what is the best DX and most secure 😃
jaydrogers commentedon Nov 7, 2024
So I've been looking into this deeper and trying to use AI to help guide a direction with this, it had a helpful explanation as I asked it about this section:
docker-php/src/common/usr/local/bin/docker-php-serversideup-entrypoint
Lines 41 to 54 in f2250d8
It pointed me to an example of the official Postgres entrypoint, which follows a similar method to what we're doing.
I also found this article which was really helpful: https://www.docker.com/blog/docker-best-practices-choosing-between-run-cmd-and-entrypoint/
Conclusion
return 0is the recommended way to goIf you have any more thoughts, I would love to hear them.
cappuc commentedon Nov 7, 2024
I made that PR to change
exit 0toreturn 0because I encountered the same problem.With the current implementation
exit 0can be used to prevent subsequent entrypoint scripts to run without exiting with an error (I don't if this can be usefull in some cases).I don't know what is the best solution.
Added docs to clarify why we don't use subshells (#481 (comment))
jaydrogers commentedon Nov 7, 2024
Thanks! @cappuc! I think the
return 0without a subshell is the way to go (at least my gut is telling me that).I documented it here: c98009b
I am all ears if someone has a better solution in the meantime
Fix exit on error during migrations, CI updates, and improve notices …
jaydrogers commentedon Nov 7, 2024
I added this in the docs: https://serversideup.net/open-source/docker-php/docs/customizing-the-image/adding-your-own-start-up-scripts#dont-use-exit-0-in-your-script
I am closing for now, but if there is any further discussion on this item, please comment below 👍
szepeviktor commentedon Nov 7, 2024
@jaydrogers This is a surprise gift 🎁 for the permission-less shell script.
a.shb.shStart a.sh: b.sh can not exit!
jaydrogers commentedon Nov 7, 2024
Fun! So should I document that you shouldn't source other scripts from your script?
I'm not aware of a use case where someone would want to do this.
Entrypoint scripts are just simple start up scripts to execute something.
It seems like the issue you bring up would affect the official Postgres images too?
szepeviktor commentedon Nov 7, 2024
You should define that function in your script where you call a user script.
(it is like immunizing against
exit)szepeviktor commentedon Nov 7, 2024
👉🏻
"it won't stop the execution"
jaydrogers commentedon Nov 7, 2024
Ohhhhhhh, now I get it 😃
I like where this is going, but will it work in
/bin/sh? We need to make sure it's/bin/shfor Alpine.To my understanding this might only work in
/bin/bash.szepeviktor commentedon Nov 7, 2024
Yes I'm a Bashist with a hard hat.
Please try it in your shell!
jaydrogers commentedon Nov 7, 2024
Trust me, I wish I could do this in
/bin/bash😃export -fis not available in/bin/sh. I tried this:My error