This article will explain the method of executing bash commands in the Docker container.
How to Execute Bash Command in Docker Container?
To execute bash commands in a Docker container, try out the following steps:
- Select a particular running container
- Access the selected docker container using the “docker exec -it <container-name> bash” command.
- Execute bash commands in the Docker container.
Step 1: Select Running Container
First, list all Docker containers, and choose a particular running container:
The above output has displayed two running containers and we have selected the “Cont2” container.
Step 2: Access the Selected Container
To access the selected running container for executing bash commands in it, utilize the “docker exec -it <container-name> bash” command:
It can be observed that the bash shell has been started and the user can now execute bash commands in it.
Step 3: Execute Bash Command in Container
Finally, execute any bash command inside the container. For instance, run the “touch <file-name>” command to create a new file in the current container:
To list all the container’s content, use the “ls” command:
The above output has displayed all the container’s content including the newly created “demo.txt” file.
Conclusion
To execute bash commands in a Docker container, first, select a specific running container. Then, access it through the “docker exec -it <container-name> bash” command. Finally, execute bash commands in the Docker container. This article has explained the method of executing bash commands in the Docker container.