Skip to content
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 code

Explanation

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