Suppose you have already created a Docker container previously and have worked with it. Now, you have stopped the container. So, the container is in exited state. What if you want to run a command inside the container?
Typically, there can be two cases. Either the container is stopped using the Docker stop command or the container is currently running in the background. In such a case, either you can start the container back, access it’s bash, and execute whatever command that you want. This is perfect for containers that are not running. Another solution is that you use the Docker exec command to run commands in a container that is actively running.
For containers that are stopped, you can also start the container using the Docker start command and then run the Docker exec command. Let’s check out all these scenarios one by one.
Let’s create a container using the Docker run command.
$ docker run -it --name=myubuntu ubuntu:latest bash
This command will create an ubuntu container called ubuntu and will open a bash of the container which will allow you to execute commands inside the running container.
You can check the status of the container by listing the containers.
$ docker ps -a
This command will list all the existing containers in your local machine along with their current states.
Now, execute the Docker stop command mentioned below to stop the container.
$ docker stop myubuntu
If you now execute the Docker ps command, you will find that the status of the myubuntu container has now been changed to exited. Now, if you want to run commands inside this stopped container, you will have to first start the container and attach a bash using the following command.
$ docker start -ai myubuntu
This command will start the container again and you will have access to the bash of the container. You can execute any command that you want here.
Another method of executing commands inside Docker containers is by using the Docker exec command. However, you can only use the Docker exec commands on containers that are in running state.
Let’s use the Docker run command but using the detached option to run the container in background mode.
$ docker run -itd --name=myubuntu ubuntu:latest bash
This will run the container in the background mode. You can check the status by listing all the existing containers once again.
$ docker ps -a
Now, let’s use the Docker exec command to create a file inside the container. The syntax of the Docker exec command is -
$ docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
$ docker exec myubuntu touch tutorialspoint.txt
This command will create a file called tutorialspoint.txt inside the default working directory of the container. You can also use the -w (workdir) flag along with the Docker exec command to specify the path of the directory where you want to execute the command inside the container.
If you want to access the bash of a container running in background mode, you can use the -it options and provide the /bin/bash command.
$ docker exec -it ubuntu /bin/bash
This will open a bash of the container and you will be able to execute commands inside it. Please make sure that after making changes inside the container, you commit the changes using the Docker commit command if you want your changes to persist the next time you start the container.
The syntax of the Docker commit command is -
$ docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
To sum up, in this tutorial, you saw two different ways to run commands inside a container that already exists. You know that the Docker run command is used to create or start a new container and then execute commands inside it. But if you already have a container in your system and you want to execute commands inside it, you can use the methods discussed above. Based on whether your container is stopped or running in the background, you can choose any of the methods discussed above.