Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start a stopped Docker container with a different command?

Tags:

docker

I would like to start a stopped Docker container with a different command, as the default command crashes - meaning I can't start the container and then use 'docker exec'.

Basically I would like to start a shell so I can inspect the contents of the container.

Luckily I created the container with the -it option!

like image 854
aaa90210 Avatar asked Sep 02 '15 12:09

aaa90210


2 Answers

Find your stopped container id

docker ps -a 

Commit the stopped container:

This command saves modified container state into a new image user/test_image

docker commit $CONTAINER_ID user/test_image 

Start/run with a different entry point:

docker run -ti --entrypoint=sh user/test_image 

Entrypoint argument description: https://docs.docker.com/engine/reference/run/#/entrypoint-default-command-to-execute-at-runtime

Note:

Steps above just start a stopped container with the same filesystem state. That is great for a quick investigation. But environment variables, network configuration, attached volumes and other staff is not inherited, you should specify all these arguments explicitly.

Steps to start a stopped container have been borrowed from here: (last comment) https://github.com/docker/docker/issues/18078

like image 102
Dmitriusan Avatar answered Oct 12 '22 21:10

Dmitriusan


Edit this file (corresponding to your stopped container):

vi /var/lib/docker/containers/923...4f6/config.json 

Change the "Path" parameter to point at your new command, e.g. /bin/bash. You may also set the "Args" parameter to pass arguments to the command.

Restart the docker service (note this will stop all running containers):

service docker restart 

List your containers and make sure the command has changed:

docker ps -a 

Start the container and attach to it, you should now be in your shell!

docker start -ai mad_brattain 

Worked on Fedora 22 using Docker 1.7.1.

NOTE: If your shell is not interactive (e.g. you did not create the original container with -it option), you can instead change the command to "/bin/sleep 600" or "/bin/tail -f /dev/null" to give you enough time to do "docker exec -it CONTID /bin/bash" as another way of getting a shell.

NOTE2: Newer versions of docker have config.v2.json, where you will need to change either Entrypoint or Cmd (thanks user60561).

like image 24
aaa90210 Avatar answered Oct 12 '22 21:10

aaa90210