I have a bash script that I want to dockerize but I’m having issues with both the ENTRYPOINT
and CMD
directives.
Here’s my Dockerfile:
FROM ubuntu:22.04
# NOTE: these can be overridden with `--build-arg KEY=VALUE`
ARG ...
WORKDIR /scripts
COPY . .
# Install required packages
RUN ...
# Make sure the script is executable
RUN chmod a+x my-script.sh
# Set the entrypoint to use bash
ENTRYPOINT [ "bash" ]
# Run the script
CMD [ "my-script.sh", "-u", $URL, "-k", $KEY, "-d", $DEBUG ]
I’ve done a few iterations and I kept getting different kind of errors. I’ve already deleted the images that I built and the containers so I don’t have the errors to share.
Basically, I want to build an image that would run my bash script when I start a container using that image. I already saw What is the difference between CMD and ENTRYPOINT in a Dockerfile? but I’m still left confused on how to properly use these two directives.
2
Answers
Imagine that it will be executed like:
It can have many different presentation but in your specific example, it should be
Therefore
Make sure the first line of the script has a correct "shebang" line.
Make sure the script is executable on the host system.
Put the entire command line in (preferably)
CMD
. Don’t explicitly say "bash" anywhere.Especially avoid the pattern you show of naming only the interpreter in the
ENTRYPOINT
. If you want to run an alternate command, you still need to type out the entire command, but it must be a shell script or else it won’t run (docker run --rm your-image ls -l
will fail as you’ve shown it, for example). There’s an argument to make the script itself be theENTRYPOINT
and theCMD
be only the arguments.The approach I generally take is that
CMD
is a complete command on its own. There is a useful pattern of havingENTRYPOINT
be a wrapper script that does some first-time setup and then ends inexec "$@"
to run theCMD
. There are also some interesting use cases of replacing the command at container start time (for debugging; to run a Web server and background worker off the same code base) andCMD
is a little easier to override when you need to.