How can I override CMD when running a docker image?

The Question :

161 people think this question is useful

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”) ?

The Question Comments :

The Answer 1

175 people think this answer is useful

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.

The Answer 2

34 people think this answer is useful

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%.

The Answer 3

27 people think this answer is useful

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.

Tags:

Add a Comment