Skip to main content
⚡ OpenDevDocs
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 sh

2. 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-db

3. Running Database CLI Tools

# Connect directly to Postgres CLI inside running container
docker exec -it postgres-db psql -U postgres -d my_database

4. Executing as Specific User

# Run command as root to inspect permissions
docker exec -u 0 -it my-container whoami

docker exec vs docker run

Featuredocker rundocker exec
StateCreates a new container from an imageRuns a command in an existing running container
LifecycleNew container PID 1, new writable layerSub-process in existing container namespace
Use CaseStarting servicesLive debugging, inspections, CLI client access

On this page