DevDocs v1.0

Container Monitoring and Logs

Learn how to monitor Docker containers, inspect logs, track resource usage, and troubleshoot common issues.

Container Monitoring and Logs

Running containers is only part of operating applications successfully.

To maintain healthy services, you need visibility into:

  • Application logs
  • Container status
  • CPU usage
  • Memory usage
  • Network activity
  • Storage consumption

Docker provides several built-in tools that make troubleshooting and monitoring easier.

Why Monitoring Matters

Consider a scenario where an application suddenly becomes unavailable.

Possible causes include:

  • Application crashes
  • Memory exhaustion
  • High CPU usage
  • Database connectivity issues
  • Configuration problems

Without logs and monitoring, diagnosing these issues becomes significantly more difficult.

A good monitoring strategy helps you identify and resolve problems before they impact users.

Viewing Container Logs

The most common troubleshooting tool is:

docker logs

Example:

docker logs my-app

This displays all output generated by the container.

Typical logs may include:

Server started on port 3000
Database connected
Listening for requests

Following Logs in Real Time

To continuously stream logs:

docker logs -f my-app

The -f flag stands for:

follow

This is useful when debugging active applications.

Example:

Incoming request: GET /api/users
Incoming request: POST /api/login

Press:

CTRL + C

to stop following the log stream.

Viewing Recent Log Entries

Display only the most recent entries:

docker logs --tail 50 my-app

Example:

docker logs --tail 100 my-app

This is helpful when dealing with large log files.

Adding Timestamps

Display timestamps alongside log messages:

docker logs -t my-app

Example:

2025-01-10T14:30:21Z Server started
2025-01-10T14:30:25Z Database connected

Timestamps make debugging much easier.

Monitoring Running Containers

View active containers:

docker ps

Example output:

CONTAINER ID   NAME      STATUS
abc123         api       Up 2 hours
def456         postgres  Up 2 hours

This provides a quick overview of running services.

Viewing Resource Usage

Docker includes a built-in monitoring command:

docker stats

Example:

NAME       CPU %     MEM USAGE
api        2.3%      150MB
postgres   5.1%      320MB

This updates continuously in real time.

Press:

CTRL + C

to exit.

Monitoring a Specific Container

Inspect a single container:

docker stats my-app

Useful when investigating performance issues.

Inspecting Containers

Detailed container information:

docker inspect my-app

Docker returns JSON containing:

  • Environment variables
  • Network settings
  • Mounted volumes
  • Container configuration
  • Image details

Example:

docker inspect postgres-db

This is one of Docker's most useful troubleshooting commands.

Checking Container Exit Status

Sometimes containers stop unexpectedly.

View all containers:

docker ps -a

Example:

STATUS
Exited (1)
Exited (137)
Exited (255)

Exit codes can provide clues about failures.

Common examples:

CodeMeaning
0Successful exit
1Application error
137Killed due to memory limits
255Unexpected failure

Viewing Container Processes

Inspect running processes inside a container:

docker top my-app

Example output:

PID     COMMAND
1       node server.js

This helps verify what is actually running.

Accessing a Running Container

Open a shell inside a container:

docker exec -it my-app sh

Or:

docker exec -it my-app bash

depending on the image.

Once inside, you can:

  • Inspect files
  • Check configurations
  • Run commands
  • Debug applications

Monitoring Disk Usage

Docker resources consume storage over time.

View storage usage:

docker system df

Example:

TYPE            SIZE
Images          2.5GB
Containers      400MB
Volumes         1.2GB

This helps identify cleanup opportunities.

Cleaning Up Unused Resources

Remove stopped containers:

docker container prune

Remove unused images:

docker image prune

Remove unused volumes:

docker volume prune

Remove everything unused:

docker system prune

Use caution when removing resources.

Monitoring Docker Compose Applications

View service status:

docker compose ps

Example:

NAME         STATUS
frontend     running
backend      running
database     running

This provides a quick overview of your application stack.

Viewing Compose Logs

Display logs for all services:

docker compose logs

Follow logs:

docker compose logs -f

View logs for a specific service:

docker compose logs backend

This is often easier than inspecting containers individually.

Common Troubleshooting Workflow

When a container fails:

Step 1

Check status:

docker ps -a

Step 2

Review logs:

docker logs my-app

Step 3

Inspect configuration:

docker inspect my-app

Step 4

Open a shell:

docker exec -it my-app sh

Step 5

Review resource usage:

docker stats

This process solves most common container issues.

Common Problems

Container Exits Immediately

Possible causes:

  • Incorrect startup command
  • Missing environment variables
  • Application crashes

Check:

docker logs my-app

High Memory Usage

Inspect:

docker stats

Consider:

  • Memory leaks
  • Large workloads
  • Resource limits

Port Conflicts

Error:

Port already in use

Check active containers:

docker ps

Or inspect host processes.

Missing Files

Verify mounted volumes:

docker inspect my-app

Look under:

Mounts

Best Practices

Monitor Regularly

Do not wait until applications fail.

Regular monitoring helps identify issues early.

Use Structured Logging

Prefer:

{
  "level": "info",
  "message": "User logged in"
}

over unstructured text logs.

Set Resource Limits

Avoid allowing containers unlimited access to host resources.

Review Logs Frequently

Logs often reveal problems before users notice them.

Remove Unused Resources

Regular cleanup helps prevent storage-related issues.

Summary

Monitoring and logging are essential parts of operating Docker applications.

Key commands include:

docker logs
docker stats
docker inspect
docker top
docker exec
docker system df

By combining logs, resource monitoring, and container inspection, you can diagnose most Docker issues quickly and keep applications running reliably.