Get Inside A Running Docker Container
Use exec to open a shell inside a running container for inspection and debugging.
Sometimes you need to look inside a running container.
Maybe you want to check files, inspect environment variables, confirm a process is running, or understand how the container filesystem looks. Docker lets you do this with docker exec.
Start a container
Run an Nginx container in detached mode:
docker run -d --name web nginxCheck that it is running:
docker psNow you can enter it.
Use docker exec
Use this command:
docker exec -it web bashThe options mean:
| Option | Meaning |
|---|---|
-i | Keep input open |
-t | Allocate a terminal |
web | Container name |
bash | Shell to open inside the container |
If the image does not include bash, try:
docker exec -it web shSmall images often include sh instead of bash.
Explore inside the container
Once inside, try:
pwd
ls
whoami
cat /etc/os-releaseFor Nginx, you can check the default web files:
cd /usr/share/nginx/html
ls
cat index.htmlThis helps you see that the container has its own filesystem.
Exit the container shell
To leave the shell:
exitThe container keeps running because you only exited the extra shell process, not the main Nginx process.
Check again:
docker psExec vs attach
Beginners sometimes confuse exec and attach.
| Command | Meaning |
|---|---|
docker exec | Runs a new command inside a running container |
docker attach | Attaches your terminal to the main container process |
For inspection and debugging, prefer docker exec.
Do not manually fix production containers
Getting inside a container is useful for learning and debugging. But in real deployments, you should not manually edit containers and treat those edits as permanent.
If a change is needed, update the Dockerfile or application code, build a new image, and run a new container.
Changes made inside a running container can disappear when the container is removed. Permanent changes belong in the image, not inside a manually edited container.
Written By: Shiva Srivastava
How is this guide?
Last updated on
