I’m stuck trying to achieve the objective described in the title. Tried various options last of which is found in this article. Currently my Dockerfile is as follows:
FROM ubuntu:18.04
EXPOSE 8081
CMD cd /var/www/html/components
CMD "bash myscript start" "-D" "FOREGROUND"
#ENTRYPOINT ["bash", "myscript", "start"]
Neither the CMD…"FOREGROUND" nor the commented-out ENTRYPOINT lines work. However, when I open an interactive shell into the container, cd into /var/…/components folder and execute the exact same command to run the script, it works.
What do I need to change?
2
Answers
Once you pass your
.sh
file, run it withCMD
. This is a snippet:And here is my full dockerfile, have a look.
I see three problems with the Dockerfile you’ve shown.
There are multiple
CMD
s. A Docker container only runs one command (and then exits); if you have multipleCMD
directives then only the last one has an effect. If you want to change directories, use theWORKDIR
directive instead.Nothing is
COPY
d into the image. Unless you explicitlyCOPY
your script into the image, it won’t be there when you go to run it.The
CMD
has too many quotes. In particular, the quotes around"bash myscript start"
make it into a single shell word, and so the system looks for an executable program named exactly that, including spaces as part of the filename.You should be able to correct this to something more like:
(I tend to avoid
ENTRYPOINT
here, for two main reasons. It’s easier todocker run --rm -it your-image bash
to get a debugging shell or run other one-off commands without anENTRYPOINT
, especially if the command requires arguments. There’s also a useful pattern of usingENTRYPOINT
to do first-time setup before running theCMD
and this is a little easier to set up ifCMD
is already the main container command.)