Reputation: 514
How can I have an entrypoint in a docker run
which executes multiple commands?
Something like:
docker run --entrypoint "echo 'hello' && echo 'world'" ... <image>
The image I'm trying to run, has already an entrypoint set in the Dockerfile, so solution like the following seems not to work, because it looks my commands are ignored, and only the original entrypoint is executed
docker run ... <image> bash -c "echo 'hello' && echo 'world'"
In my use-case I must use the docker run
command. Solution which change the Dockerfile
are not acceptable, since it is not in my hands
Upvotes: 7
Views: 19419
Reputation: 159830
As a style point, this gets vastly easier if your image has a CMD
that can be overridden. If you only need to run one command with no initial setup, make it be the CMD
and not the ENTRYPOINT
:
CMD ./some_command # not ENTRYPOINT
If you need to do some initial setup and then launch the main command, make the ENTRYPOINT
be a shell script that ends with the special instruction exec "$@"
. The CMD
will be passed into it as parameters, and this line replaces the shell script with that command.
#!/bin/sh
# entrypoint.sh
... do first time setup, run database migrations, set variables ...
exec "$@"
# Dockerfile
...
ENTRYPOINT ["./entrypoint.sh"] # MUST be JSON-array syntax
CMD ./some_command # as before
If you do these things, then you can use your initial docker run
form. This will replace the CMD
but leave the ENTRYPOINT
intact. In the wrapper-script case, your alternate command will be run as the exec "$@"
command, so all of the first-time setup will be done first.
# Assuming the image correctly honors the CMD
docker run ... \
image-name \
sh -c 'echo "foo is $FOO" && echo "bar is $BAR"'
If you really can't do this, you can override the docker run --entrypoint
. This runs instead of the image's entrypoint (if you want the image's entrypoint you have to run it yourself), and the syntax is awkward:
# Run a shell command instead of the entrypoint
docker run ... \
--entrypoint /bin/sh \
image-name \
-c 'echo "foo is $FOO" && echo "bar is $BAR"'
Note that the --entrypoint
option comes before the image name, and its arguments come after the image name.
Upvotes: 21