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

  1. View raw metadata with docker inspect <container>.
  2. Follow logs with docker logs -f <container>.
  3. View resource usage with docker stats.
  4. 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 --format to 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 logs may be empty if the application logs to a file instead of stdout.
  • docker exec fails if the container is not running or lacks a shell binary.
  • Large docker inspect output can be filtered with jq for readability.