How to start a stopped Docker container with another command?

I would like to start a stopped Docker container with another command, because the default command crashes - this means that I can not start the container, and then use docker exec.

Basically, I would like to run a shell so that I can check the contents of the container.

Fortunately, I created a container with the -it option!

+179
docker
02 Sep '15 at 12:23
source share
10 answers

Find the id of the stopped container

docker ps -a 

Lock a stopped container:

This command saves the changed state of the container to a new image user/test_image

 docker commit $CONTAINER_ID user/test_image 

Run / run with another entry point:

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

Entry Point Argument Description: https://docs.docker.com/engine/reference/run/#/entrypoint-default-command-to-execute-at-runtime

Note:

Steps above, just start the stopped container with the same state of the file system. This is great for a quick investigation. But environment variables, network configuration, attached volumes, and other personnel are not inherited, you must explicitly specify all these arguments.

The steps for starting a stopped container were borrowed from here: (last comment) https://github.com/docker/docker/issues/18078

+286
Sep 05 '16 at 10:58
source share

Edit this file (corresponding to your stopped container):

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

Change the "Path" parameter to point to your new command, for example / bin / bash. You can also set the Args parameter to pass arguments to the command.

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

 service docker restart 

List your containers and make sure the command has changed:

 docker ps -a 

Launch the container and attach to it, now you should 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 (for example, you did not create the original container with the -it option), you can change the command to "/ bin / sleep 600" or "/ bin / tail -f / dev /" instead. null, "to give you enough time to execute" docker exec -it CONTID / bin / bash "as another way to get the shell.

NOTE 2. In newer versions of Docker, there is a config.v2.json file in which you will need to change either Entrypoint or Cmd (thanks to user60561).

+103
02 Sep '15 at 12:26
source share

Add a check at the top of your script entry point

Docker really needs to implement this as a new feature, but there is another option for a workaround for situations in which you have an entry point that ends after success or failure, which can make debugging difficult.

If you don't have a script entry point yet, create one that executes any commands necessary for your container. Then at the top of this file add these lines to entrypoint.sh :

 # Run once, hold otherwise if [ -f "already_ran" ]; then echo "Already ran the Entrypoint once. Holding indefinitely for debugging." cat fi touch already_ran # Do your main things down here 

To ensure that cat contains a connection, you may need to provide TTY. I run the container with my script entry point as follows:

 docker run -t --entrypoint entrypoint.sh image_name 

This will cause the script to be run once, creating a file that indicates that it is already running (in the container’s virtual file system). Then you can restart the container to perform debugging:

 docker start container_name 

When the container is already_ran , the already_ran file will be found, as a result of which the script entry point will be closed with cat (which simply waits forever for input, which never arrives, but keeps the container in an active state). Then you can debug the bash session:

 docker exec -i container_name bash 

While the container is running, you can also remove already_ran and manually execute the entrypoint.sh script to restart it if you need to debug this path.

+14
Mar 03 '17 at 23:20
source share

Run the command manually inside the docker container.

I am running docker version 7.03.1-ce and I cannot find config.json.

I am trying to edit the config.v2.json file, but it does not work, and I do not need the commit method, because I do not want to create a new image. I find an alternative by manually executing the command inside the container.

Run container

 docker start <container> 

Access bash

 docker exec -ti <container> bash 

Run the command manually

 root@<container_id># service <command> start 

or

 docker exec -d <container> service <command> start #Note: run this in another terminal while docker exec is attach. 
+1
May 28 '17 at 3:48 a.m.
source share

I took @Dmitriusan's answer and made it an alias:

alias docker-run-prev-container = 'prev_container_id = "$ (docker ps -aq | head -n1)" && & && docker commit "$ prev_container_id" "prev_container / $ prev_container_id" && & & docker run -it -entrypoint = bash "prev_container / $ prev_container_id" '

Add this to your ~/.bashrc aliases ~/.bashrc and you will have a great new docker-run-prev-container alias that puts you in a wrapper in the previous container.

Invalid for debug docker build s

+1
Jun 14 '17 at 9:14
source share

My problem:

  • I started the container with docker run <IMAGE_NAME>
  • And then he added some files to this container
  • Then I closed the container and tried to start it again with the same command as above.
  • But when I checked for new files, they were missing
  • when I run docker ps -a I could see two containers.
  • This means that every time I docker run <IMAGE_NAME> , a new image is created

Decision. To work with the same container that you created first, follow these steps:

  • docker ps to get your container container
  • docker container start <CONTAINER_ID> to start an existing container
  • Then you can continue from where you left. e.g. docker exec -it <CONTAINER_ID>/bin/bash
  • Then you can decide to create a new image from it
+1
Dec 09 '18 at 7:57
source share

It is not indicated whether the container exits only if your code works, and you need to see what happens in the container. If this is not an option, here is another potential solution.

Get container id using docker ps

docker exec -it 665b4a1e17b6 /bin/sh

If the entry point is given by something problematic, it can also be redefined, as suggested in the answer of Dmitrius. It should also be noted that you can attach to any running container using docker attach . So many decisions of different decisions. I just do not see the need to fix the image. This seems unnecessary.

Docs for Docker exec - https://docs.docker.com/engine/reference/commandline/exec/

Docker Joining Docs - https://docs.docker.com/engine/reference/commandline/attach/

0
Jan 30 '17 at 22:42
source share

This is not quite what you are asking for, but you can use docker export in a stopped container if all you need is to check the files.

 mkdir $TARGET_DIR docker export $CONTAINER_ID | tar -x -C $TARGET_DIR 
0
Jul 03 '19 at 8:59
source share
 docker container start <CONTAINER_ID> 
-eleven
Jul 16 '17 at 14:46
source share

I really disagree with both of these answers. If you just want to see what is in the container, then you can run this command to get a shell. No need to change the entry point at all or any configs.

 docker run -it <image_name> bash 
-12
Oct 17 '16 at 20:14
source share



All Articles