This guide explains how to open an interactive shell inside a running Docker container for troubleshooting and operational tasks. You will learn a safe step-by-step workflow, the difference between docker exec and docker attach, and why SSH is usually unnecessary for containers.
Originally published on 2016-02-23; fully revised and updated.

Quick workflow for container shell access
For most debugging work on a running container, use docker exec -it. It starts a new process inside the container and avoids coupling your terminal session to the container’s main process.
flowchart TD
A[Identify container] --> B{Container running}
B -- no --> C[Start container]
B -- yes --> D[Choose access method]
C --> D
D --> E[Use docker exec for new shell]
D --> F[Use docker attach for main process]
E --> G[Exit shell]
F --> G
Step 1: Find the target container and start it if needed
List running containers first. If the target container is stopped, start it before attempting shell access.
docker ps
docker ps -a
docker start <container_id_or_name>Code language: HTML, XML (xml)
Step 2: Choose the right access method
docker exec and docker attach are not interchangeable. Pick based on whether you need a new shell process or direct interaction with the container’s main process.
| Method | Creates a new process? | Best for | Important caveat |
|---|---|---|---|
| docker exec -it <container> /bin/bash | Yes | Opening a fresh interactive shell for debugging | Requires a shell binary in the image (use /bin/sh if bash is missing) |
| docker attach <container> | No | Interacting with or observing the main process stream | You are tied to PID 1 behavior; exiting can impact container lifecycle depending on process design |
| SSH into container | Usually not applicable | Legacy workflows only | Needs SSH server setup, keys, and exposed port; increases complexity and attack surface |
Step 3: Open a new shell with docker exec (recommended)
Use container ID or name. Try /bin/bash first; if unavailable, fall back to /bin/sh.
docker exec -it <container_id> /bin/bash
docker exec -it <container_name> /bin/bash
docker exec -it <container_id_or_name> /bin/shCode language: HTML, XML (xml)
If you specifically need to connect to the running main process stream instead, use attach:
docker attach <container_id_or_name>Code language: HTML, XML (xml)
Step 4: Exit safely
From an exec-created shell, leave with exit. This ends only that shell process, not the container itself.
exitCode language: PHP (php)
When using docker attach, remember you are connected to the container’s main process I/O. Detach behavior and impact depend on how that process is configured.
Linux permissions and sudo
Whether you need sudo for Docker commands depends on host setup. If your user is not in the Docker group, commands may fail with permission errors.
sudo usermod -aG docker $USER
newgrp dockerCode language: PHP (php)
Best practices and common pitfalls
- Prefer docker exec -it for day-to-day troubleshooting.
- Avoid adding SSH servers to application containers unless there is a clear operational requirement.
- Use container names for readability, but keep IDs handy for scripts.
- If /bin/bash fails, retry with /bin/sh.
- Be careful with docker attach: it connects to the main process and can affect runtime behavior.
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services — all in one place.
Explore Hospitals