docker exec: How to Run Commands and Open a Shell Inside a Container

Key Takeaways
- ✓docker exec -it CONTAINER bash opens a shell; use sh on Alpine and slim images.
- ✓You do not need SSH to get into a container; docker exec replaces it.
- ✓exec runs a new process; attach connects to the main one and can stop the container.
- ✓Fixes for "executable file not found", "is not running" and "not a TTY".
Table of Contents
- •docker exec: How to Run Commands and Open a Shell Inside a Container
- •The Basic Syntax
- •Open a Shell: docker exec -it
- •Useful Options
- •docker compose exec
- •"SSH Into a Docker Container": You Do Not Need SSH
- •docker exec vs docker attach vs docker run
- •Common Errors and Fixes
- •OCI runtime exec failed: exec: "bash": executable file not found in $PATH
- •Error response from daemon: container ... is not running
- •the input device is not a TTY
- •Container keeps restarting
- •Frequently Asked Questions
- •Spend Less Time on Servers
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 -laOpen a Shell: docker exec -it
docker exec -it web bash-ikeeps standard input open so you can type.-tallocates 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 shType 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 postgresCompose 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 shOr 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 shdocker 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_NAMEthe input device is not a TTY
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.
Unsure Which Cloud Hosting Plan Fits Your Site?
Answer 4 quick diagnostic questions to estimate the server CPU, RAM and storage you need with transparent pricing.
Cloud Infrastructure Engineer.
Related Posts
VPS HostingVultr Alternatives in 2026: VPS Hosting for Developers Compared
Vultr's Regular Performance prices compared with DigitalOcean, Linode, Hetzner and a fixed-plan VPS at the 8 GB size, plus when container hosting fits better.
VPS HostingWhat Is a VPS? Virtual Private Servers Explained
A VPS (virtual private server) is a virtual machine with its own operating system, resources and root access. How it works, uses, and when you need one.
VPS HostingLinode (Akamai) Alternatives in 2026: VPS Hosting Compared
Linode's shared CPU prices from Akamai's own API, compared with DigitalOcean, Vultr and a fixed-plan VPS, plus when container hosting makes more sense.