How to inspect a Docker container?
· Category: Docker
Short answer
Docker provides several commands to inspect containers: docker inspect for detailed metadata, docker logs for stdout/stderr, docker stats for live resource usage, and docker exec for running commands inside a running container.
Steps
- View raw metadata with
docker inspect <container>. - Follow logs with
docker logs -f <container>. - View resource usage with
docker stats. - Open a shell inside the container with
docker exec -it <container> /bin/sh.
Example
docker inspect --format='{{.NetworkSettings.IPAddress}}' mycontainer
View recent logs:
docker logs --tail 100 mycontainer
Check CPU and memory usage:
docker stats mycontainer
Tips
- Use Go templates with
docker inspect --formatto extract specific fields. - Stream logs to a centralized logging system for production monitoring.
- Use
docker top <container>to see running processes inside a container.
Common issues
docker logsmay be empty if the application logs to a file instead of stdout.docker execfails if the container is not running or lacks a shell binary.- Large
docker inspectoutput can be filtered withjqfor readability.