207

I want to inspect a docker image created by someone else with both an entrypoint and cmd specified, for example:

ENTRYPOINT ["/usr/sbin/apache2ctl"]
CMD ["-D", "FOREGROUND"]

I currently do:

docker run --interactive --tty --entrypoint=/bin/bash $IMAGE --login

Is there a way to override CMD to be empty (so I don't have to use "--login") ?

warp
  • 2,289
  • 2
  • 14
  • 6

3 Answers3

219

You could just enter via docker run -it --entrypoint=/bin/bash $IMAGE -i (you 'll launch a new container from the image and get a bash shell in interactive mode), then run the entrypoint command in that container.

You can then inspect the running container in the state it should be running.

EDIT: Since Docker 1.3 you can use exec to run a process in a running container. Start your container as you 'd normally do, and then enter it by issuing:

docker exec -it $CONTAINER_ID /bin/bash

Assuming bash is installed you will be given shell access to the running container.

Dagelf
  • 589
  • 4
  • 14
Panagiotis Moustafellos
  • 2,408
  • 1
  • 12
  • 17
  • Wouldn't that execute "/usr/sbin/apache2ctl /bin/bash"? (because your example doesn't override the entrypoint it will use the entrypoint defined in the Dockerfile -- or am I missing something?) – warp May 12 '14 at 17:56
  • Is that something which is different for older/newer docker versions perhaps? It certainly runs the entrypoint for me: https://gist.github.com/warpr/26a5cc471dce7fcf9021 . This is using docker 0.10.0 on Ubuntu 13.10. – warp May 12 '14 at 21:56
  • Passing `-entrypoint` overrides the entrypoint. See the edited answer – Panagiotis Moustafellos May 13 '14 at 05:07
  • 4
    I know passing --entrypoint overrides the entrypoint. But when I use --entrypoint, how do I override CMD to be empty? (For bash I can use something like -i or --login, but not all entrypoints I might want to use will have an equivalent "dummy" argument). – warp May 13 '14 at 13:24
  • 1
    At least in Docker v1.11.1 it appears that overriding the --entrypoint also prevents the normal appending of the CMD to the entrypoint. That only seems to happen when the entrypoint is not overridden. – Jasmine Hegman Aug 23 '17 at 20:59
  • 7
    Why there is a `-i` after `$IMAGE`? – Eyal Levin Oct 21 '18 at 12:57
  • Julia 1.4.0 returns: `Warning: Pkg.installed() is deprecated`. Do you know any update on how to get the package version now? – Adam Ryczkowski Apr 06 '20 at 10:39
53

See: https://docs.docker.com/engine/reference/run/#overriding-dockerfile-image-defaults

Relevant portion:

CMD (Default Command or Options) Recall the optional COMMAND in the Docker commandline:

$ docker run [OPTIONS] IMAGE[:TAG] [COMMAND] [ARG...]

This command is optional because the person who created the IMAGE may have already provided a default COMMAND using the Dockerfile CMD. As the operator (the person running a container from the image), you can override that CMD just by specifying a new COMMAND.

If the image also specifies an ENTRYPOINT then the CMD or COMMAND get appended as arguments to the ENTRYPOINT.

So to do what you want you need only specify a cmd, and override using /bin/bash. Not quite "empty", but it get the job done 99%.

Simon Thum
  • 631
  • 4
  • 4
  • Oops, I overlooked a detail. Except for shortening `--login` to `-l` I guess your solution is already as good as it gets. – Simon Thum Jun 12 '14 at 08:02
  • Here's a fun thing I saw today: the command isn't overridden, it's appended. Here's the PS from inside the container. Note that the command I added was echo "setting up the db" and the built in command is /usr/bin/geth. Also I set entrypoint to be "". `1 ? Ssl 0:00 /usr/bin/geth echo setting up db` – Paul S Feb 20 '16 at 09:08
  • The docker reference link no longer works - can't find an obvious replacement for it. – Chris Kimpton Apr 26 '17 at 11:56
  • I just fixed it, but I also found it hard to find ;( – Simon Thum Apr 27 '17 at 08:45
40

For anyone comming here to override entrypoint AND command to pass other command, e.g. run bash instead of entrypoint script and then run some other command with parameters (was not clear to me from other answers):

 docker run [other options] --entrypoint '/bin/sh' $IMAGE -c 'npm link gulp gulp-sass gulp-sourcemaps'

-c 'npm link ...' is parameter for /bin/sh so here you may pass any command you would want to run in container. /bin/sh is for alpine images, /bin/bash most likely for other images.

  • 2
    just an addtion shell commands should be json based writen , so instead of quoting with single quotes `'...'` use double quotes `"..."` – Jimmy Obonyo Abor Sep 11 '17 at 16:44
  • 2
    Or just pass as many --entrypoint arguments as you want – Nick Roz Jul 17 '19 at 16:11
  • this does not seem to work: "exec: \"-c\": executable file not found in $PATH": unknown. – Richard Sep 19 '19 at 17:13
  • Check the path in container, maybe it really does not exist. Most of the images have `/bin/sh` but yours might not be that case. – Aurelijus Rozenas Sep 20 '19 at 12:07
  • I think this the best and most complete answer, though as @JimmyObonyoAbor points out, it should be corrected to use double-quotes (`"`). – NYCeyes Apr 13 '20 at 16:47
  • @NYCeyes why would you say the example should use double quotes? I don't mind to edit the answer but I need to know why. Both single and double quotes are valid for bash even though they do not behave the same. In this example no string interpolation is done so double quotes are not required. – Aurelijus Rozenas Apr 14 '20 at 18:02