0

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?

user1729972
  • 712
  • 2
  • 9
  • 29

2 Answers2

0

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.

Francesco
  • 1,742
  • 5
  • 44
  • 78
  • So all I need do is CMD ["sh", "-c", "myscript start"] ? – user1729972 Jun 13 '22 at 09:40
  • When file is in place, `ADD ./configure.and.run.myapp.sh /yourcontainerworkdir` and you add execution permission, yes. Maybe it's better to update my answer.. – Francesco Jun 13 '22 at 09:47
  • You never need `CMD ["sh", "-c", ...]`. If it's a simple command you can just run it using the JSON-array syntax `CMD ["the-script.sh"]`, or if you need the shell wrapper, use the Docker shell syntax `CMD the-script.sh`. – David Maze Jun 13 '22 at 10:09
0

I see three problems with the Dockerfile you've shown.

There are multiple CMDs. 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 COPYd 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.)

David Maze
  • 130,717
  • 29
  • 175
  • 215
  • You’re right about not needing the bash as it worked directly from command line. Alas, these changes are not making the script execute at start up of the container. This is confusing to say the least! – user1729972 Jun 16 '22 at 16:14