If you need to do this at all regularly, you can refactor your Dockerfile to make this easier to do.
A Docker container's main process is run by concatenating together the "entrypoint" and "command" argument lists. In a Dockerfile, these come from the ENTRYPOINT
and CMD
directives. In the docker run
command this is trickier: anything after the image name is the "command" part, but the "entrypoint" part needs to be provided by the --entrypoint
argument, it needs to be before the image name, and it can only be a single word.
If you need to routinely replace the command, the syntax becomes much cleaner if you set it using CMD
and not ENTRYPOINT
in the Dockerfile.
# Dockerfile
CMD ["some", "main", "command"] # not ENTRYPOINT
If you make this change, then you can just put your alternate command after the image name in the docker run
command, without a --entrypoint
option and without splitting the command string around the image name.
docker run my-image:latest /bin/sh -c 'my-script.sh arg1 arg2'
I will somewhat routinely recommend a pattern where ENTRYPOINT
is a wrapper script that does some first-time setup, then does something like exec "$@"
to run the command that's passed to it as arguments. That setup is compatible with this CMD
-first approach: the entrypoint wrapper will do its setup and then run the override command instead of the image's command.