DEV Community

Abhay Singh Kathayat
Abhay Singh Kathayat

Posted on

Mastering Docker CLI: Advanced Commands for Logs, Resource Monitoring, and Container Management

Docker CLI Advanced Commands (logs, stats, top, pause)

The Docker Command Line Interface (CLI) offers a wide range of commands for managing containers, images, networks, and volumes. Beyond the basic commands like docker run or docker ps, there are several advanced commands that provide deeper insights into container operations and allow you to interact with containers in a more granular way.

In this article, we'll cover some of the advanced Docker CLI commands, including logs, stats, top, and pause, and explain how they can help in managing, troubleshooting, and monitoring containers.


1. docker logs - View Container Logs

One of the most essential tools for troubleshooting is the ability to view container logs. The docker logs command allows you to access the logs produced by a container’s stdout and stderr streams. This is crucial for debugging and understanding the behavior of applications running inside containers.

Usage:

docker logs [OPTIONS] CONTAINER
Enter fullscreen mode Exit fullscreen mode

Options:

  • -f or --follow: Continuously stream the logs (like tail -f).
  • --since: Show logs since a given timestamp (e.g., --since="2024-12-01T13:23:00").
  • --tail: Show only the last N lines of logs (e.g., --tail 100).
  • -t or --timestamps: Show timestamps with logs.
  • --details: Show additional details about logs if available.

Example:

To view the logs of a container named my-container:

docker logs my-container
Enter fullscreen mode Exit fullscreen mode

To follow the logs continuously:

docker logs -f my-container
Enter fullscreen mode Exit fullscreen mode

To see the last 50 lines of logs:

docker logs --tail 50 my-container
Enter fullscreen mode Exit fullscreen mode

2. docker stats - Display Container Resource Usage

The docker stats command provides real-time information about container resource usage, including CPU, memory, and network I/O. It’s useful for monitoring the performance of containers, especially when you need to troubleshoot performance issues or understand resource consumption.

Usage:

docker stats [OPTIONS] [CONTAINER...]
Enter fullscreen mode Exit fullscreen mode

Options:

  • --no-stream: Disable continuous updates; show only one snapshot.
  • --all: Show stats for all containers (including those that are stopped).

Example:

To view the resource usage of all running containers:

docker stats
Enter fullscreen mode Exit fullscreen mode

To view the stats of a specific container (my-container):

docker stats my-container
Enter fullscreen mode Exit fullscreen mode

To see a single snapshot (instead of continuous streaming):

docker stats --no-stream my-container
Enter fullscreen mode Exit fullscreen mode

3. docker top - Display Running Processes in a Container

The docker top command allows you to view the processes running inside a container, similar to the ps command in Linux. This is useful for debugging and monitoring the processes within a container, such as checking if an application is running or if a process is consuming too many resources.

Usage:

docker top CONTAINER [ps OPTIONS]
Enter fullscreen mode Exit fullscreen mode

Example:

To view the processes inside a container named my-container:

docker top my-container
Enter fullscreen mode Exit fullscreen mode

If you want to see a more detailed view with specific columns (e.g., PID, USER, etc.), you can use the ps options:

docker top my-container aux
Enter fullscreen mode Exit fullscreen mode

4. docker pause - Pause Running Containers

The docker pause command allows you to pause all processes in a container. This can be useful for temporarily suspending a container without stopping it, allowing you to resume its execution later. This is often used when you need to free up system resources temporarily without fully stopping a container.

Usage:

docker pause CONTAINER [CONTAINER...]
Enter fullscreen mode Exit fullscreen mode

Example:

To pause a running container:

docker pause my-container
Enter fullscreen mode Exit fullscreen mode

To pause multiple containers at once:

docker pause container1 container2
Enter fullscreen mode Exit fullscreen mode

After pausing, you can resume the container with the docker unpause command:

docker unpause my-container
Enter fullscreen mode Exit fullscreen mode

Practical Use Cases for Advanced Docker CLI Commands

  • Monitoring Resource Usage:
    Use docker stats to keep an eye on the resource usage of containers, especially for memory and CPU consumption. This can help in optimizing container performance or troubleshooting resource-related issues.

  • Debugging Containers:

    • Use docker logs to get real-time logs for debugging when a container fails or misbehaves.
    • Combine docker logs -f with docker exec to troubleshoot applications in real time.
    • Use docker top to inspect running processes within containers for any unexpected behaviors.
  • Gracefully Managing Containers:

    The docker pause and docker unpause commands are useful for temporarily halting containers in critical production environments without shutting them down completely. This is especially helpful when you need to free up system resources or perform maintenance tasks without disrupting container state.


By understanding and utilizing these advanced Docker CLI commands, you can greatly improve your ability to monitor, troubleshoot, and manage containers in production and development environments. Whether you're dealing with resource consumption issues, debugging a failing application, or simply managing the lifecycle of containers, these commands provide the necessary tools to streamline your workflow.

Top comments (0)