Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I override the entrypoint and pass Bash commands as a string?

I don't want to modify the Dockerfile.

I want to override the entrypoint and run arbitrary commands but this is not working for me:

docker run -it --entrypoint /bin/bash myimage bash -c "echo aaaa &"

I get /bin/bash: /bin/bash: cannot execute binary file

I want to be able to immediately start a background job before entering the container interactively- again without modifying the Dockerfile.

like image 634
red888 Avatar asked Sep 22 '20 21:09

red888


People also ask

Can ENTRYPOINT be overridden?

ENTRYPOINT is the other instruction used to configure how the container will run. Just like with CMD, you need to specify a command and parameters. However, in the case of ENTRYPOINT we cannot override the ENTRYPOINT instruction by adding command-line parameters to the `docker run` command.

Can we override ENTRYPOINT in Dockerfile?

If you are overriding the Dockerfile ENTRYPOINT or CMD , it only affects the image but not the deployment in Kubernetes. If a deployment using this image defines the command or args options, they will take precedence over the overrides you define for the image.

Does CMD override ENTRYPOINT?

CMD commands are ignored by Daemon when there are parameters stated within the docker run command. ENTRYPOINT instructions are not ignored but instead are appended as command line parameters by treating those as arguments of the command.

Does docker compose command override ENTRYPOINT?

All About Docker Compose Override Entrypoint Entrypoint helps use set the command and parameters that executes first when a container is run. In fact, the command line arguments in the following command become a part of the entrypoint command, thereby overriding all elements mentioned via CMD.


1 Answers

When Docker launches a container, it combines the "entrypoint" and "command" parts together into a single command. The docker run --entrypoint option only takes a single "word" for the entrypoint command.

So, say you need to run some command --with an-arg. You need to

  1. Break this into words
  2. Pass the first word as docker run --entrypoint, before the image name
  3. Pass the remaining words as the command part, after the image name.
# some command --with an-arg
docker run \
  --entrypoint some
  image-name \
  command --with an-arg

# ls -al /
docker run --rm \
  --entrypoint /bin/ls
  image-name \
  -al /

# bash -c "echo aaaa"
docker run --rm \
  --entrypoint /bin/bash \
  image-name \
  -c 'echo aaaa'

This construct is kind of awkward. If you control the image, I tend to recommend making the image's CMD be a complete command, and either omitting ENTRYPOINT or making it be a wrapper that takes a complete command as arguments (for example, a shell script that ends in exec "$@").

# Hard to replace with an alternate command:
# ENTRYPOINT python3 ./manage.py runserver

# Better:
CMD python3 ./manage.py runserver
# Takes a complete command as arguments
# (MUST use JSON-array form)
ENTRYPOINT ["bundle", "exec"]

# Can be overridden at runtime
# (But whatever you supply will run in a Ruby Bundler context)
CMD ["rails", "server", "-b", "0.0.0.0"]
like image 82
David Maze Avatar answered Oct 31 '22 19:10

David Maze