How to Run Command in a Docker Container

Posted on


Actually, this article is an article where the main focus is just to run a container or execute a command in a running container. In this context, the container can be an already running container or it is already stop. This article has an indirect relation with the other article in this link. The article itself has the title of ‘How to Start a Docker Container’. So, there will be a different output on running the container depends on the state of the container itself. If the container is in the state of running, running a command in the container will have a higher possibility to be success. On the other hand, if the container is not running or currently stop, running a command in it will definitely end in a failure.

Run Command in Docker Container

First of all, the following is the consequence of running the command in a running container. The following is the actual status of the running container and also the one which is stop :

C:\Users\Administrator>docker container list --all
CONTAINER ID   IMAGE      COMMAND                  CREATED        STATUS                    PORTS                    NAMES
3ca3c24f657d   0f55       "bash"                   11 hours ago   Up 6 seconds              80/tcp                   blissful_ellis
2cfce8343dcf   0f55       "/bin/bash"              4 days ago     Exited (0) 11 hours ago                            vigilant_grothendieck
8a52dcce139c   registry   "/ /etc…"   4 days ago     Up 16 hours     >5000/tcp   hub.local
8bca4865ac83   centos     "/bin/bash"              5 days ago     Up 12 hours                                        centos_app


So, after listing all the containers available, just execute the following command to run it in the running docker application. As in the above output, it is a container with the ID of ‘3ca3c24f657d’ and the name of ‘blissful_ellis ‘. In other words, executing command on a running container will ends in a success :

C:\Users\Administrator>docker exec -it 3ca3c bash
[root@3ca3c24f657d www]# exit

[root@3ca3c24f657d www]#

On the other hand, the following after is the consequence of running the command in a stop container. It is a container with the ID of ‘2cfce8343dcf’ and the name of ‘vigilant_grothendieck’. The actual execution status of the stop container :

C:\Users\Administrator>docker exec -it 2cfce bash
Error response from daemon: Container 2cfce8343dcf78fc670e5a7f203a2b54494b982130099a24d0ddb4f739f2249d is not running


The stop container does not respond to the execution command of container using ‘docker exec’. Being said, the only option is just to remove the container and start a new container which can start and up running. Either because of there is a service on it or it is running in a detached mode.

Leave a Reply