docker exec Explained
What docker exec does, with an example.
Published November 7, 2026
docker exec runs a command inside an already running container, most commonly to open a shell for debugging.
How it works
It starts a new process in the container's namespaces; stopping it does not stop the container.
Use -it for an interactive terminal.
Try it
docker exec -it web bash
root@a1b2c3:/# ls /usr/share/nginx/html
index.html
Practical examples
Open a shell
docker exec -it web shRun one command
docker exec web nginx -tAs another user in a directory
docker exec -u www-data -w /var/www web php artisan migratePass environment
docker exec -e DEBUG=1 web envCommon mistakes
- Using exec on a stopped container.
- Changing files inside a container and expecting them to persist.
- Using bash on minimal images that only have sh.
Key takeaways
- docker exec runs a command inside an already running container, most commonly to open a shell for debugging.
- Run docker <command> --help for all options.
- Clean up unused resources regularly.
FAQ
exec vs attach?
exec starts a new process; attach connects to the main process.
Where can I see all options?
Run the command with --help.