300-910 · Question #78
A developer is containerizing an application. The container "devnetprod" has been run in detached mode to output the container ID with the command: docker run -d devnetprod…
The correct answer is B. docker exec -it devnetprod /bin/bash. To interact with a running Docker container, the docker exec command is used with the container's name or ID and the -it flags for an interactive TTY session.
Question
docker run -d devnetprod 1b487caec32a75ce2a74447060455ad838269e8747f4314769fa287
Which command must be used to start an interactive Bash shell running the container?Exhibit
Options
- Adocker exec -it 1b487caec32a /bin/bash
- Bdocker exec -it devnetprod /bin/bash
- Cdocker run -it devnetprod /bin/bash
- Ddocker run -it 1b48caec32a /bin/bash
How the community answered
(30 responses)- A7% (2)
- B90% (27)
- C3% (1)
Why each option
To interact with a running Docker container, the `docker exec` command is used with the container's name or ID and the `-it` flags for an interactive TTY session.
Using the container ID `1b487caec32a` (truncated) with `docker exec` would technically work if the ID was complete and correct, but generally, referring to containers by name (`devnetprod`) is preferred for readability and consistency.
The `docker exec -it devnetprod /bin/bash` command is correct because `docker exec` is used to run a command in a *running* container. The `-it` flags allocate a pseudo-TTY and keep STDIN open for an interactive session, and `devnetprod` is the container's name. This allows an interactive Bash shell to be started inside the already running detached container.
`docker run` is used to *start a new* container from an image, not to execute a command within an *already running* container. This would attempt to create a new container named `devnetprod` (if it was an image name) and run bash in it, not interact with the existing one.
`docker run` is incorrect for executing a command in a running container, and using a truncated container ID with `docker run` is also incorrect syntax for starting a container by ID.
Concept tested: Docker container interaction (exec)
Source: https://docs.docker.com/engine/reference/commandline/exec/
Topics
Community Discussion
No community discussion yet for this question.
