Executing Commands Inside a Container
Once you have accessed a running Docker container using the "docker exec" command, you can execute various commands within the container's environment. This allows you to perform a wide range of tasks, such as troubleshooting, debugging, and administrative operations.
Executing Single Commands
To execute a single command inside a container, you can use the following syntax:
docker exec CONTAINER COMMAND [ARG...]
For example, to display the contents of the /etc/os-release
file inside a container named "my-container", you would run:
docker exec my-container cat /etc/os-release
This will execute the cat /etc/os-release
command within the "my-container" container and display the output in your terminal.
Executing Interactive Commands
If you need to execute an interactive command, such as a shell, you can use the -i
(interactive) and -t
(allocate a pseudo-TTY) options:
docker exec -it CONTAINER COMMAND
For instance, to open a Bash shell inside the "my-container" container, you would use:
docker exec -it my-container bash
This will give you a fully interactive shell within the container, allowing you to execute multiple commands and navigate the container's file system.
Capturing Command Output
You can also capture the output of commands executed inside a container using the standard output redirection mechanisms of your shell. For example:
docker exec my-container ls -l > container_files.txt
This will execute the ls -l
command inside the "my-container" container and save the output to a file named "container_files.txt" on the host system.
By understanding how to execute commands inside a Docker container using the "docker exec" command, you can effectively manage and troubleshoot your containerized applications.