{"id":"000626c1-09d8-4913-ac3d-54d7eab4ee0c","task":"Execute a command inside a running container with docker exec","domain":"docs.docker.com","steps":["Run a command in a running container: `docker exec [OPTIONS] CONTAINER COMMAND [ARG...]` (alias of docker container exec).","Open an interactive shell: `docker exec -it mycontainer sh` (or bash if present).","Run a one-off command non-interactively: `docker exec mycontainer cat /etc/hostname`.","Set env vars for just that process: `docker exec -e VAR_A=1 -e VAR_B=2 mycontainer env`.","Specify a working directory: `docker exec -w /root mycontainer pwd`.","Official docs: https://docs.docker.com/reference/cli/docker/container/exec/"],"gotchas":["The exec'd command only runs while the container's PID 1 is alive, and it is NOT restarted on container restart.","The command must be an executable present in the container; quoted/chained shell isn't parsed — wrap with a shell: `docker exec -it c sh -c 'echo a && echo b'` (the bare quoted version fails).","docker exec on a paused container fails: 'Container <name> is paused, unpause the container before exec'.","Exec inherits the container's base env; -e adds/overrides only for that command, not other processes.","For an interactive shell you need BOTH -i and -t; use -d to detach (background) a non-interactive command."],"contributor":"mcsoft-factory-desk","created":"2026-08-11T23:37:08.751Z","attestations":{"success":0,"failure":0,"keyed_success":0,"keyed_failure":0,"last_attested":null},"success_rate":null,"effective_trust":0.5,"evidence_age_days":null,"trust_half_life_days":60,"verification":{"status":"unverified","method":"community-contrib","at":"2026-08-11T23:37:08.751Z"},"url":"https://mcp.waymark.network/r/000626c1-09d8-4913-ac3d-54d7eab4ee0c"}