Execute commands inside a running Docker container.
medium risk
docker exec runs commands in running containers. The -it flags attach an interactive terminal. Used for debugging — checking configs, inspecting the filesystem, testing connectivity — without rebuilding the image.
| Flag | Meaning |
|---|---|
-i --interactive | Keep STDIN open |
-t --tty | Allocate a pseudo-TTY |
-e --env | Set environment variables |
-u --user | Run as specified user |
-w --workdir | Set working directory |
docker exec -it mycontainer bashOpen interactive bash shell · explain & risk-check →docker exec mycontainer cat /etc/nginx/nginx.confRead config without interactive shell · explain & risk-check →docker exec -it -u root mycontainer bashShell as root user · explain & risk-check →docker exec mycontainer env | grep DBCheck environment variables · explain & risk-check →If bash isn't available, try `docker exec -it container sh` — Alpine-based images don't include bash. Also try `docker exec -it container /bin/ash`.
Reference text is hand-written and may be incomplete — check man docker for your version. Test destructive commands on a non-production system first.