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?
CodePudding user response:
Once you pass your .sh
file, run it with CMD
. This is a snippet:
ADD ./configure.and.run.myapp.sh /tmp/
RUN chmod x /tmp/configure.and.run.myapp.sh
...
CMD ["sh", "-c", "/tmp/configure.and.run.myapp.sh"]
And here is my full dockerfile, have a look.
CodePudding user response:
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 multiple CMD
directives then only the last one has an effect. If you want to change directories, use the WORKDIR
directive instead.
Nothing is COPY
d into the image. Unless you explicitly COPY
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:
FROM ubuntu:18.04
# Instead of `CMD cd`; a short path like /app is very common
WORKDIR /var/www/html/components
# Make sure the application is part of the image
COPY ./ ./
EXPOSE 8081
# If the script is executable and begins with #!/bin/sh then
# you don't need to explicitly say "bash"; you probably do need
# the path if it's not in /usr/local/bin or similar
CMD ./myscript start -D FOREGROUND
(I tend to avoid ENTRYPOINT
here, for two main reasons. It's easier to docker run --rm -it your-image bash
to get a debugging shell or run other one-off commands without an ENTRYPOINT
, especially if the command requires arguments. There's also a useful pattern of using ENTRYPOINT
to do first-time setup before running the CMD
and this is a little easier to set up if CMD
is already the main container command.)