Docker
Exec & Logs
Run commands inside running containers and inspect logs.
By EZ4Code Team
execlogsdebugging
Code
# Execute a command in a running container
docker exec -it web sh
docker exec db psql -U postgres -c "SELECT 1;"
# Follow logs, last N lines, since timestamp
docker logs -f web
docker logs --tail 100 web
docker logs --since 30m web
docker logs -t web | head -20
# Stats and top
docker stats
docker top web
# Inspect state and exit codes
docker inspect web | grep -A5 State
docker wait web # blocks until container stops, prints exit codeExplanation
docker exec opens a new process inside a running container, useful for debugging or running ad-hoc commands. docker logs streams the stdout/stderr of the container's main process; --tail and --since slice the output. stats and top give a live view of resource usage and processes.
More Docker Snippets
Run a Container
Run, list, stop, and remove Docker containers.
Writing a Dockerfile
Build an image from a Dockerfile with multi-instruction layers.
Image Management
Pull, list, tag, push, and prune images.
Volumes & Bind Mounts
Persist data with named volumes, anonymous volumes, and bind mounts.
Networking
Create networks, attach containers, and expose ports.
Docker Compose
Define and run multi-container apps with compose.