RackUp IT - View Cloud Hosting Plans

Get started now or speak directly with our engineering team for specialized assistance.

Logo
DevelopmentWEB & APPCalculatorTOOLServicesProductBlogOur TeamCareers
View Plans
Home>Blog>docker exec: How to Run Commands and Open a Shell Inside a Container
VPS Hosting

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

Alex Vance
Alex Vance
Published Oct 12, 2026•✓ Verified Expert Reviewed•3 min read
docker exec guide cover

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 -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

OptionWhat it doesExample
-uRun as another userdocker exec -it -u root web sh
-wSet the working directorydocker exec -w /app web npm run migrate
-eSet an environment variabledocker exec -e DEBUG=1 web node script.js
-dRun in the backgrounddocker 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

the 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.

← Previous ArticleHow to Open Ports on a VPS: UFW, iptables & firewalldNext Article →Docker Logs: How to View, Tail, Find and Clear Container Logs
Interactive 60-Second Tool

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.

Free Migration 99.9% Uptime Target
Calculate My Hosting Build
Alex Vance
Written ByAlex Vance

Cloud Infrastructure Engineer.

99.9% Uptime Target

RackUp IT - View Cloud Hosting Plans

View Hosting Plans

View Hosting Plans

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

Categories

  • VPS Hosting
  • DevOps & Tutorials
  • WordPress Hosting
  • Cloud Hosting
  • Server Stack
  • Networking & Security
  • Web Hosting / Managed Cloud
  • E-Commerce
  • Hosting & SEO
  • Hosting Benchmarks
  • Cloud Backup & DevOps
  • Managed VPS

Recent Posts

  • Vultr Alternatives in 2026: VPS Hosting for Developers Compared
  • Docker Port Mapping Explained: -p, EXPOSE and Compose ports
  • Bluehost Alternatives (2026): WordPress Hosting Without the Renewal Price Jump

Tags

Dockerdocker execContainersDevOpsLinux

Related Posts

Vultr alternatives coverVPS Hosting
Nov 27, 2026

Vultr 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.

Read Article →
What is a VPS? Virtual private servers explainedVPS Hosting
Nov 20, 2026

What 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.

Read Article →
Linode alternatives coverVPS Hosting
Nov 19, 2026

Linode (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.

Read Article →
Logo

Expert Web & App Development, UI/UX Design, Branding, and Consulting to scale your business.

Company

  • About Us
  • Blog
  • Contact
  • Our Team
  • Careers

Services

  • Web Development
  • App Development
  • UI/UX Design
  • Consulting
  • Managed WordPress VPS
  • Website Speed Test

Subscribe to our newsletter

We accept
  • bKash
  • Nagad
  • Rocket
  • Upay
  • CellFin
  • Meghna Pay
  • OK Wallet
  • Tap
  • mCash
  • DBBL Nexus
  • Visa
  • Mastercard
  • American Express
  • Crypto
Pay with Visa, Mastercard, American Express, DBBL Nexus, CellFin, Rocket, bKash, Tap, Meghna Pay, Nagad, mCash, Upay and OK Wallet. Verified by EPS

© 2026 Rackup IT Solution. All rights reserved.

Privacy PolicyTerms of Service