Home/Engineer/Docker / Containers/Debugging Running Containers…
Docker / ContainersIntermediateUbuntuDebianCentOSdockerContainerslogs

Debugging Running Containers — docker exec, logs, inspect

A practical rundown of debugging commands and patterns to use when a container is not behaving as expected.

Contents
  1. Checking container status
  2. Viewing logs
  3. Accessing the container
  4. Querying details with inspect
  5. When a container exits immediately
  6. Network debugging
  7. Analyzing image layers

Checking container status

Bash
# Running containers
docker ps

# Including stopped containers
docker ps -a

# Real-time resource usage monitoring
docker stats

# A specific container only
docker stats my_app

Viewing logs

Bash
# Full logs
docker logs my_app

# Last 100 lines
docker logs --tail 100 my_app

# Follow in real time
docker logs -f my_app

# Include timestamps
docker logs -t my_app

# Logs since a specific time
docker logs --since "2024-01-15T10:00:00" my_app

Accessing the container

Bash
# Open a shell (use sh if bash is not available)
docker exec -it my_app bash
docker exec -it my_app sh

# Run a single command
docker exec my_app cat /etc/nginx/nginx.conf

# Connect as root (for permission debugging)
docker exec -u root -it my_app bash

# Check environment variables
docker exec my_app env

Querying details with inspect

Bash
# Full information (JSON)
docker inspect my_app

# Extract a specific field
docker inspect -f '{{.State.Status}}' my_app
docker inspect -f '{{.NetworkSettings.IPAddress}}' my_app
docker inspect -f '{{range .Mounts}}{{.Source}} -> {{.Destination}}{{"
"}}{{end}}' my_app

# List of environment variables
docker inspect -f '{{range .Config.Env}}{{.}}{{"
"}}{{end}}' my_app

When a container exits immediately

Bash
# Check the exit code
docker inspect my_app | grep -A5 '"State"'

# View logs of a crash-looping container
docker logs my_app 2>&1 | tail -50

# Override entrypoint to get a shell
docker run -it --entrypoint sh my_image:latest

# Copy a file from a stopped container
docker cp dead_container:/app/error.log ./error.log

Network debugging

Bash
# List container networks
docker network ls

# Network details (which containers are connected)
docker network inspect bridge

# Ping another container from a container (use the container name)
docker exec my_app ping db

# Check DNS resolution
docker exec my_app nslookup db

# Check listening ports
docker exec my_app ss -tlnp

Analyzing image layers

Bash
# Check size per layer
docker history my_image:latest

# Detailed layer information
docker history --no-trunc my_image:latest
#docker#Containers#logs#inspect
Editorial note

AI tools may assist drafting. Check the evidence in each guide for whether commands were run and which environment was used. Confirm the current docs for your version before applying instructions, and email us about errors. email us

Topic-related official docs (auto-linked · not per-claim sources)Docker documentation ↗

Questions & answers

Ask a question about this guide. We'll review and reply.