# Docker Engine (Part 6)

When running dozens or hundreds of containers in production, knowing basic commands like `docker container ls` isn't enough. When an application slows down, crashes, or consumes excessive server resources, you need quick ways to look under the hood without disrupting running workloads.

In this guide, we will walk through Docker's built-in inspection and observability commands—`inspect`, `stats`, `top`, `logs`, and `events`—and show how to use them together to pinpoint performance bottlenecks in real time.

## 1\. Deep Metadata Inspection (`docker container inspect`)

While `docker container ls` gives you a high-level summary (ID, image, status, ports), `docker container inspect` returns the complete low-level configuration and runtime state of a container in **JSON format**.

```plaintext
docker container inspect webapp
```

### What You Can Find in `inspect` Output

- `State`**:** Exact status (`running`, `exited`, `restarting`), exit code, PID on the host, and whether the container was killed due to Out-Of-Memory (`OOMKilled`).
- `Config`**:** Environment variables, exposed ports, working directory, and the exact `Cmd` / `Entrypoint` running inside.
- `Mounts`**:** All bind mounts and volumes attached to the container, along with source and destination paths.
- `NetworkSettings`**:** Assigned IP address, MAC address, gateway, and port mappings.

### Pro Tip: Filtering JSON Output with `--format`

Because `inspect` outputs hundreds of lines of JSON, you can extract specific fields directly using Go templates (`-f` / `--format`):

```plaintext
# Get the container's IP address
docker container inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' webapp

# Check if the container was killed by Out-Of-Memory (OOM)
docker container inspect -f '{{.State.OOMKilled}}' webapp
```

## 2\. Live Resource Monitoring (`docker container stats`)

When a server slows down under the load of multiple containers, manually checking each container is inefficient. `docker container stats` provides a live, real-time stream (similar to Linux `top`) of CPU, memory, network I/O, and block (disk) I/O across all running containers.

```plaintext
docker container stats
```

To inspect specific containers only, pass their names or IDs:

```plaintext
docker container stats webapp redis-db
```

### Understanding the Columns

- `CPU %` **&** `MEM USAGE / LIMIT`**:** Shows how much host CPU and RAM each container is consuming against its configured cgroup limits.
- `NET I/O`**:** Cumulative data sent and received over the container's network interfaces.
- `BLOCK I/O`**:** Data read from and written to block storage devices on the host.

*(Tip: Add* `--no-stream` *if you want a single point-in-time snapshot rather than a continuously refreshing dashboard—useful for logging or scripts.)*

## 3\. Identifying Culprit Processes (`docker container top`)

Suppose `docker container stats` reveals that one container is pegging the CPU at 50% or 100%. How do you see *which process* inside that container is responsible without opening an interactive shell (`exec`)?

Use `docker container top`:

```plaintext
docker container top webapp
```

### How Host vs. Container PIDs Work

Because containers share the host's Linux kernel (isolated via PID namespaces), every process running inside a container is also a real process on the host machine:

- Inside the container, the main process appears as **PID 1**.
- On the host kernel, that same process has a standard host PID (for example, **PID 17001** running a `stress` test).

`docker container top` displays the running processes along with their **host-level PIDs** and CPU time, allowing you to immediately spot runaway processes like `stress` or stuck worker threads from outside the container.

## 4\. Viewing & Streaming Application Logs (`docker container logs`)

Applications running inside containers write their logs to standard output (`STDOUT`) and standard error (`STDERR`). Instead of SSH-ing into containers to hunt down log files, retrieve them directly via `docker container logs`:

```plaintext
docker container logs webapp
```

### Useful Log Flags for Troubleshooting

- **Follow logs live (**`-f` **/** `--follow`**):** Streams new log entries to your terminal in real time (similar to `tail -f`):

  Bash

  ```plaintext
  docker container logs -f webapp
  ```
- **Limit output (**`--tail`**):** View only the last *N* lines instead of flooding your terminal with thousands of old logs:

  Bash

  ```plaintext
  docker container logs --tail 50 -f webapp
  ```
- **Add timestamps (**`-t` **/** `--timestamps`**):** Prepends an RFC3339 timestamp to every log line to correlate errors with traffic spikes:

  Bash

  ```plaintext
  docker container logs -t --tail 100 webapp
  ```

## 5\. Monitoring Daemon Activity (`docker system events`)

While `docker container logs` shows what happens *inside* an application, `docker events` (or `docker system events`) streams real-time lifecycle events from the **Docker Daemon** itself.

```plaintext
docker events
```

Every time a container is created, started, killed, restarted, or OOM-killed—or whenever networks, images, or volumes change—Docker logs an event with a timestamp:

```plaintext
2026-12-02T19:15:10.124Z container create 8a9f2b1c... (image=nginx, name=webapp)
2026-12-02T19:15:10.482Z network connect 4e1d9a0b... (container=8a9f2b1c..., name=bridge)
2026-12-02T19:15:10.690Z container start 8a9f2b1c... (image=nginx, name=webapp)
```

### Filtering Events

You can query historical events from the daemon's buffer using `--since` or filter by event type using `--filter`:

```plaintext
# View events from the last 30 minutes
docker events --since '30m'

# Filter only for container crash/die events
docker events --filter 'event=die'
```

## Step-by-Step Troubleshooting Workflow

When debugging a misbehaving container in production, combine these commands in order:

1. **Spot the bottleneck:** Run `docker container stats` to find which container is spiking CPU or memory.
2. **Identify the process:** Run `docker container top <container>` to see which internal process is causing the load.
3. **Check application errors:** Run `docker container logs --tail 100 -f <container>` to read application stack traces.
4. **Verify lifecycle & limits:** Run `docker container inspect <container>` or `docker events --filter 'container=<container>'` to check for restart loops, mount misconfigurations, or `OOMKilled` crashes.
