Skip to main content
The Docker package is Linux-only and requires access to cgroup filesystem and Docker daemon.

Introduction

The docker package provides functionality to monitor Docker containers on Linux systems through cgroup interfaces. It allows you to retrieve container information, CPU statistics, and memory usage directly from the cgroup filesystem.

Key Features

Container Information

List all Docker containers with their status, image, and runtime information

CPU Statistics

Monitor CPU usage, user time, and system time for containers via cgroups

Memory Statistics

Track detailed memory metrics including RSS, cache, and page faults

Cgroup Support

Works with both Docker and systemd.slice cgroup hierarchies

Installation

go get github.com/shirou/gopsutil/v4/docker

Basic Usage

import "github.com/shirou/gopsutil/v4/docker"

// Get list of all Docker containers
containers, err := docker.GetDockerStat()
if err != nil {
    log.Fatal(err)
}

for _, container := range containers {
    fmt.Printf("Container: %s (%s)\n", container.Name, container.ContainerID)
    fmt.Printf("Image: %s\n", container.Image)
    fmt.Printf("Status: %s\n", container.Status)
    fmt.Printf("Running: %v\n", container.Running)
}

Error Types

The package defines specific error types for common failure scenarios:

ErrDockerNotAvailable

Returned when the Docker daemon is not installed or not accessible.
containers, err := docker.GetDockerStat()
if errors.Is(err, docker.ErrDockerNotAvailable) {
    log.Println("Docker is not available on this system")
}

ErrCgroupNotAvailable

Returned when the cgroup filesystem is not available or accessible.

Requirements

Platform: Linux only (uses //go:build linux build tag)Permissions: Requires access to:
  • Docker daemon (for container listing)
  • /sys/fs/cgroup/ filesystem (for statistics)

Architecture

The package reads data from two primary sources:
  1. Docker CLI - Uses docker ps command to retrieve container metadata
  2. Cgroup Filesystem - Reads /sys/fs/cgroup/ for performance metrics
This hybrid approach allows monitoring without requiring the Docker API, making it lightweight and efficient.

Next Steps

Container Operations

Learn how to list and query Docker containers

Statistics

Monitor CPU and memory usage via cgroups

Build docs developers (and LLMs) love