How to Exec / SSH into a Running Docker Container with Bash
Open an interactive terminal shell session inside a live running Docker container to inspect files, debug processes, or test network connectivity.
docker exec -it <container-name> /bin/bashStep-by-Step Execution Guide
docker psLists all currently running containers with their IDs, names, status, and port mappings.
docker exec -it <container-name> /bin/bash-i (interactive) and -t (pseudo-TTY) allocate a terminal shell. /bin/bash starts the Bash shell.
docker exec -it <container-name> /bin/shAlpine and minimal scratch images do not have Bash installed; use /bin/sh instead.
docker exec -u 0 -it <container-name> /bin/bash-u 0 (or --user root) forces root execution even if the container runs as a non-privileged user.
Critical Docker Pitfalls & Precautions
- β’Changes made inside a container via exec are ephemeral; they will be lost when the container is recreated unless persisted in volumes.
- β’Type "exit" or press Ctrl+D to leave the container shell without stopping the container.
Alternative Approaches
docker compose exec <service-name> shWhen running multi-container stacks via docker-compose.yml.
Exec into Container (Bash/Sh) - Frequently Asked Questions
Common questions about container isolation, signal handling, and runtime behavior.
The container base image (like Alpine or distroless) does not include Bash. Replace /bin/bash with /bin/sh.
Related Docker Command Guides
Browse All Docker RecipesReclaim gigabytes of hard drive space by removing stopped containers, unused networks, dangling images, and build caches.
Instantly stop all active containers and optionally remove them with a single command.
Copy files or directories between a Docker container and your local host file system without creating a volume mount.
Understand the fundamental differences between ENTRYPOINT and CMD directives and how to combine them for flexible container CLIs.
Inspect stdout and stderr output from containers with timestamps, tail line limits, and live streaming.
Best practices for copying files into Docker images. Why COPY is almost always preferred over ADD.