Issue
How can I include my own shell script CMD
on container start/restart/attach, without removing the CMD
used by an inherited image?
I am using this, which does execute my script fine, but appears to overwrite the PHP CMD
:
FROM php
COPY start.sh /usr/local/bin
CMD ["/usr/local/bin/start.sh"]
What should I do differently? I am avoiding the prospect of copy/pasting the ENTRYPOINT or CMD of the parent image, and maybe that's not a good approach.
Solution
As mentioned in the comments, there's no built-in solution to this. From the Dockerfile, you can't see the value of the current CMD
or ENTRYPOINT
. Having a run-parts
solution is nice if you control the upstream base image and include this code there, allowing downstream components to make their changes. But docker there's one inherent issue that will cause problems with this, containers should only run a single command that needs to run in the foreground. So if the upstream image kicks off, it would stay running without giving your later steps a chance to run, so you're left with complexities to determine the order to run commands to ensure that a single command does eventually run without exiting.
My personal preference is a much simpler and hardcoded option, to add my own command or entrypoint, and make the last step of my command to exec
the upstream command. You will still need to manually identify the script name to call from the upstream Dockerfile. But now in your start.sh
, you would have:
#!/bin/sh
# run various pieces of initialization code here
# ...
# kick off the upstream command:
exec /upstream-entrypoint.sh "$@"
By using an exec
call, you transfer pid 1 to the upstream entrypoint so that signals get handled correctly. And the trailing "$@"
passes through any command line arguments. You can use set
to adjust the value of $@
if there are some args you want to process and extract in your own start.sh
script.
Answered By - BMitch Answer Checked By - Marie Seifert (WPSolving Admin)