Docker
devopsdockerGuideBeginner
Docker Exec & Container Debugging
Complete guide to docker exec, spawning interactive shells inside running containers, running diagnostic commands, and debugging production containers.
Verified: 2026-09-30
Tested on:docker 27.x
Platforms:
linuxmacoswindows
#docker#exec#debugging#devops
The docker exec command runs a new process inside the execution environment of an already running container.
Basic Syntax
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]Common Use Cases
1. Interactive Shell into a Container
# For bash-equipped images (Ubuntu, Debian, Node Full)
docker exec -it my-container bash
# For minimal Alpine Linux images (sh only)
docker exec -it my-container sh2. Running Diagnostic Commands
# Check container disk usage
docker exec my-container df -h
# Check running processes inside container
docker exec my-container ps aux
# Verify network connectivity from container to another host
docker exec my-container ping -c 3 postgres-db3. Running Database CLI Tools
# Connect directly to Postgres CLI inside running container
docker exec -it postgres-db psql -U postgres -d my_database4. Executing as Specific User
# Run command as root to inspect permissions
docker exec -u 0 -it my-container whoamidocker exec vs docker run
| Feature | docker run | docker exec |
|---|---|---|
| State | Creates a new container from an image | Runs a command in an existing running container |
| Lifecycle | New container PID 1, new writable layer | Sub-process in existing container namespace |
| Use Case | Starting services | Live debugging, inspections, CLI client access |
Related Topics
Docker Container Logs & Logging Drivers
Complete guide to docker logs, streaming logs, log rotation, timestamps, and configuring JSON-file, Syslog, and journald logging drivers.
Docker Container Lifecycle & States
Complete guide to container lifecycle states (created, running, paused, stopped, dead), transitions, and lifecycle commands.