docker exec: How to Run Commands and Open a Shell Inside a Container
docker exec runs a new command inside a container that is already running. It is how you open a shell to debug, check a config file, run a database migration or clear a cache, without rebuilding the image.
The Basic Syntax
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
CONTAINER can be the name or ID from docker ps. For example, list the files in the working directory of a container called web:
docker exec web ls -la
Open a Shell: docker exec -it
docker exec -it web bash
-i keeps standard input open so you can type.-t allocates a terminal so you get a prompt and line editing.
Many small images (Alpine, distroless, -slim variants) do not include bash. Use sh instead:
docker exec -it web sh
Type exit to leave. The container keeps running, because exec started a separate process.
Useful Options
| Option | What it does | Example |
|---|
-u | Run as another user | docker exec -it -u root web sh |
-w | Set the working directory | docker exec -w /app web npm run migrate |
-e | Set an environment variable | docker exec -e DEBUG=1 web node script.js |
-d | Run in the background | docker exec -d web touch /tmp/ready |
Running as root is handy for installing a debugging tool (apk add curl or apt-get install -y curl), but anything you install disappears when the container is recreated.
docker compose exec
With Docker Compose, use the service name from your compose file instead of the container name:
docker compose exec app sh
docker compose exec db psql -U postgres
Compose allocates a terminal by default, so -it is not needed. Add -T to disable it in scripts.
"SSH Into a Docker Container": You Do Not Need SSH
Containers normally do not run an SSH server, and adding one is extra attack surface. To work inside a container on a remote server, SSH to the host and use exec there:
ssh user@your-server
docker exec -it web sh
Or run it in one step from your own machine with a Docker context over SSH:
docker context create myvps --docker "host=ssh://user@your-server"
docker --context myvps exec -it web sh
docker exec vs docker attach vs docker run
- docker exec starts a new process in a running container. Exiting it does not affect the app. This is what you want for debugging.
- docker attach connects your terminal to the container's main process. Pressing Ctrl+C sends the signal to that process and can stop the container. Detach safely with Ctrl+P then Ctrl+Q (only works if the container was started with
-it). - docker run creates a brand-new container from an image.
Common Errors and Fixes
OCI runtime exec failed: exec: "bash": executable file not found in $PATH
The image has no bash. Use docker exec -it CONTAINER sh. Distroless images have no shell at all; debug them with docker debug (Docker Desktop) or a sidecar container sharing the same network.
Error response from daemon: container ... is not running
You can only exec into running containers. Check why it stopped with docker ps -a and docker logs CONTAINER. To look around inside a broken image, start a temporary container with a shell as the entrypoint:
docker run --rm -it --entrypoint sh IMAGE_NAME
You used -t in a script, cron job or CI pipeline where there is no terminal. Drop -t (keep -i only if you pipe input in).
Container keeps restarting
An exec may connect and then drop when the container restarts. Read the crash reason first with docker logs.
If your container sits behind Nginx and returns errors, see fixing 502 Bad Gateway with Docker and Nginx.
Frequently Asked Questions
How do I get into a running Docker container?
Run docker exec -it CONTAINER_NAME bash, or sh if bash is not installed. Find the name with docker ps.
How do I exec into a container as root?
Add -u root: docker exec -it -u root CONTAINER sh. This works even if the image sets a non-root USER.
Can I use docker exec on a stopped container?
No. Start it with docker start, or run a fresh container from the same image with --entrypoint sh to inspect it.
What is the Kubernetes equivalent of docker exec?
kubectl exec -it POD_NAME -- sh.
Spend Less Time on Servers
If managing Docker hosts, log rotation and disk space is taking time away from your product, RackUp IT managed hosting handles the server side for you, including monitoring, backups and SSL.