Docker exec: run a command in an existing container
A second process in the same container
docker exec is useful when an existing container is running and you need to inspect a file or run a small diagnostic program inside it. Unlike docker run, it does not create another container. The original main process must remain active.
Lab prerequisites
Use an approved disposable Linux-container lab only. Verify the active context (docker context show), CLI/daemon versions and that the instructor has reviewed and preloaded the BusyBox 1.37 fixture for the target platform; record its digest before use. The tag is inherited from earlier chapters, not a verified immutable identity. --pull=never avoids automatic downloads; --network none isolates normal network access for these local-only tasks. Verify each bm-v04 name is unused; if not, choose a fresh name consistently. Run host commands one at a time in Bash, stop on unexpected errors, and clean up only the container you created. If a fixture times out, inspect its state before retrying; remove only your stopped fixture. All snippets are static, not executed by ByteMind. Before any engine request, confirm the effective approved daemon endpoint, including DOCKER_HOST, DOCKER_CONTEXT and CLI overrides; do this privately in each terminal. Confirm the local tag still resolves to the approved image identity and platform. If any check fails, use the paper exercise instead. The network flag limits container networking, not the client-to-daemon connection. Use a lab with readable, local-only logging and an owner-set execution time limit; confirm your fixture identity before every cleanup.
Execute a small command
Start a fixture whose main process waits for up to 300 seconds. In that window, the first exec prints inside; the second prints /tmp because its working directory is overridden. The last exec reads two messages using the shell. These operations do not replace the waiting main process.
docker run -d --pull=never --network none --name bm-v04-exec busybox:1.37 sleep 300docker exec bm-v04-exec echo insidedocker exec -w /tmp bm-v04-exec pwddocker exec bm-v04-exec sh -c 'echo one && echo two'docker stop bm-v04-execdocker rm bm-v04-execUnderstand the arguments
The command pattern is docker exec [OPTIONS] CONTAINER COMMAND [ARG...]. Docker starts the named executable with its arguments. A shell expression such as echo one && echo two is not itself an executable name; an explicit shell can interpret it when needed. -w selects a directory for that invocation; it does not permanently update container configuration. Some minimal images do not include sh or common diagnostics. An exec process uses the container’s environment and user defaults unless overridden; it can fail if the executable is absent or the container is paused or has stopped. Inspect state instead of repeatedly retrying.
Common mistakes
Do not start a privileged shell just to inspect a file. Do not pass untrusted text to sh -c. An exec command is tied to the current running container and will not be relaunched automatically on restart. Editing a live container is also not a reproducible image update.
Manual practice
Create a fresh waiting fixture. Run pwd twice, once with -w /tmp and once without -w. Explain why the second command does not inherit the temporary override. Stop and remove your fixture.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Docker exec: run a command in an existing container — Knowledge check
Apply the concepts from this lesson.
Which command adds a process to an already running container?
How can you interpret a fixed shell expression containing && inside a BusyBox fixture?
What happens to an exec process when its container restarts?