Junior — Middle
How do you connect to a running Docker container to execute commands or diagnose?
sobes.tech AI
Answer from AI
To connect to a running Docker container and execute commands or perform diagnostics, the docker exec command is used. It allows starting an interactive session inside the container.
Example command:
docker exec -it <container_id_or_name> /bin/bash
Here:
-i— interactive mode-t— allocate pseudo-TTY<container_id_or_name>— container ID or name/bin/bash— command to run (usually a shell)
If the container does not have bash, you can use /bin/sh.
After executing this command, you will be inside the container and can run any commands for diagnostics or configuration.
You can also use docker attach, but it is less convenient for running new commands as it attaches to the main process of the container.