I create the image "FOO" from a Dockerfile that contains an ENTRYPOINT.
Is it possible to run this image in order that the entry point is executed (no override), and then the extra commands from the command line ?
Let’s be more clear :
Here is my dockerfile
FROM ubuntu
RUN blah blah
ENTRYPOINT ["myscript.sh"]
Then, I create an image with docker build -t FOO .
Now, if I run docker run --rm -it FOO
it works well: myscript.sh is well launched.
But, if I run docker run --rm -it FOO bash
it says "Extra argument bash.".
On the other hand, if I create the image without the ENTRYPOINT in the dockerfile, then the second command (with bash) actually launches bash (but of course not myscript.sh).
Is it possible to get the best of the both ?
Execute the ENTRY POINT and then, if any, what is at the end of the docker run command line ?
Thanx!
2
Answers
Not in the way you’ve said it. A container runs exactly one command, and then it exits.
However: the
ENTRYPOINT
is passed theCMD
or an overridden value as its command-line arguments. A common pattern is to useENTRYPOINT
to do some first-time setup, and thenexec
its arguments to switch to theCMD
as the "real" main container process. You’ll often see this as a shell scriptIn the Dockerfile, name this script as the
ENTRYPOINT
; it must use JSON-array syntax. You will generally provide a defaultCMD
as well, though if you really want the container to just exit in the default case then this could be a no-op command like/bin/true
.A very typical use for this setup is to run database migrations in the entrypoint script, or to set environment variables based on runtime-only information, or to initialize a data store that’s mounted at runtime. The
CMD
will by default run the main server process but this can be overridden.You can also detect whether it was started with a tty at the top of your entrypoint
then, if you run
you get
but if you run
you get the
bash
prompt.