Docker

How to Run Commands in a Stopped Container

There are few situations in which you might want to get into the container shell or may be run a single command in a stopped container.

Situations like,

  1. You started building an image and it stopped at some point. You are only left with an intermediate container and you want to troubleshoot it.
  2. The image you built is not starting up and you want to into the containers shell for troubleshooting.

In this guide, you will learn how to run commands against a stopped docker container.

Run Commands in a Stopped Container

Ther is no way you can run commands in a stopped container as the container needs to be in running state for getting the  shell access or for executing commands.

For both scenarios explained above, there is one solution. You need to commit the stopped container to an image and then run a container in interactive mode to gain access to the shell or terminal.

Follow the instructions given below.Commit the image of the stopped container using the command syntax shown below.

1 . Commit the image of the stopped container using the command syntax shown below.

docker commit <stopped container name/id> <user defined image name>

For example,

docker commit  8bd2a352dd5a michael/apache

2.  Now, run the image in interactive mode using the new committed image name as shown below.

docker run -it <image name> <command>

For example,

docker run -it michael/apache /bin/sh

That’s it. Now you will have a terminal session to your container and you can run commands against it.




Stay UpToDate With

Latest Tools, Tips and

Tutorials

x