Ask Steves · Daily notes · VPS, 20% off · AI Profit Lab · SEO · Money Hub

3 November 2026 · One command a day

How do I get a shell inside a running Docker container?

The docker exec -it command opens an interactive shell inside a container that is already running. You reach for it when you need to see what the app sees, such as which files are there, what the environment holds, or whether the container can reach another service.

I learn my automation tips in the AI Profit Boardroom. Have a look here. Affiliate link.

The command

sudo docker exec -it containername sh

docker exec runs a new process inside a container that is already running. -i keeps standard input open so you can type. -t gives you a terminal, so you get a prompt and normal line editing. containername is the name or ID shown by docker ps. If you are not sure of it, see how to see which Docker containers are running. sh is the program to start. It is a basic shell that nearly every image includes. You need sudo unless your user is in the docker group.

What you will see

$ sudo docker exec -it appname sh
/app # ls
node_modules  package.json  server.js
/app # env | grep PORT
PORT=3000
/app # wget -qO- http://127.0.0.1:3000/health
ok
/app # exit
$

The prompt changes to show you are inside the container. /app is the working directory set by the image, and # means you are root inside the container. Every command now runs in the container's own filesystem and network, so 127.0.0.1 means the container itself, not the host. Type exit or press Ctrl+D to leave. The container keeps running.

When to use it

Watch out for

Changes you make inside the container do not last. If you edit a file or install a package from this shell, it is gone the next time the container is recreated from its image, for example after docker compose up pulls a new version. Use the shell to look around and test ideas. Make the real fix in the Dockerfile, a mounted volume or the compose file. If something goes wrong, read the container's logs before you change anything inside it.

Questions people ask

Can I use bash instead of sh?

Yes, if the image includes it: sudo docker exec -it appname bash. Small images such as Alpine often do not, and Docker replies with an "executable file not found" error. In that case, use sh.

Can I run one command without opening a shell?

Yes. Leave out -it and give the command directly, for example sudo docker exec appname ls /app. It prints the output and returns you to the host.

Getting inside a running container like this is something you can only do on a server you control. You can get the same VPS with 20% off at https://asksteves.co.uk/vps. Affiliate link.

Want more automation tips? The AI Profit Boardroom on Skool is where I get mine. Have a look here. Affiliate link.

Everything on this blog runs on one Hostinger KVM 8. This link takes 20% off the same box.

GET A VPS, 20% OFF →

Affiliate link.

Related server notes: How do I open a shell as another user on Linux with sudo? · How do I see memory and CPU use per Docker container on Linux? · How do I fix file ownership on Linux after running something as root? · All daily notes