docker exec

Execute commands inside a running Docker container.

medium risk

interactive breakdown — edit the command and press Explain
$ 
⚠ Risks & cautions
What it does

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.

Common flags
FlagMeaning
-i --interactiveKeep STDIN open
-t --ttyAllocate a pseudo-TTY
-e --envSet environment variables
-u --userRun as specified user
-w --workdirSet working directory
Examples
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 →
Pro tip

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`.

Related
docker logs docker ps kubectl
Open the full explainer →More in Docker & Containers

Reference text is hand-written and may be incomplete — check man docker for your version. Test destructive commands on a non-production system first.