SDKs overview

Docker Hub provides a centralized cloud-based registry for storing, managing, and distributing Docker container images. While the primary interaction often occurs via the Docker CLI, developers can also programmatically interact with Docker Hub and the underlying Docker Engine using various Software Development Kits (SDKs) and libraries. These tools enable automation of tasks such as pushing and pulling images, managing repositories, configuring automated builds, and setting up webhooks for event notifications.

Interacting with Docker Hub typically involves two main interfaces: the Docker Engine API and the Docker Hub API. The Docker Engine API controls the local Docker daemon, allowing operations like building, running, and managing containers and images on a host. The Docker Hub API, on the other hand, specifically manages resources within Docker Hub itself, such as repositories, organizations, and webhooks. Official SDKs generally abstract these interactions, providing language-specific interfaces for common operations.

The availability of SDKs and libraries extends Docker Hub's utility beyond manual command-line operations, allowing for deeper integration into custom applications, continuous integration/continuous deployment (CI/CD) pipelines, and broader DevOps automation strategies. This page details the official SDKs maintained by Docker, as well as notable community-contributed libraries, providing installation instructions and quickstart examples.

Official SDKs by language

Docker maintains official SDKs primarily for Go and Python, which are widely used for infrastructure automation and cloud-native development. These SDKs are designed to interact with the Docker Engine API, facilitating local container and image management. While they can indirectly support Docker Hub operations (e.g., pulling images from Hub), direct Docker Hub API interactions for repository management might require additional libraries or direct HTTP calls.

Language Package/Module Primary Interaction Maturity
Go github.com/docker/docker/client Docker Engine API Stable
Python docker Docker Engine API Stable

The Docker SDK for Python is a high-level library that wraps the Docker Engine API, providing a Pythonic interface for managing Docker containers, images, networks, and volumes. Similarly, the Go package provides direct access to the Docker Engine API client, enabling Go applications to programmatically control Docker daemons. These official SDKs are regularly updated to support new Docker Engine features.

Installation

Installing the official Docker SDKs is typically straightforward using standard package managers for each language environment.

Go

For Go applications, the Docker client library is imported directly into your project. Ensure you have Go installed on your system.

go get github.com/docker/docker/client

After running this command, the package will be available for use in your Go source files.

Python

The Docker SDK for Python can be installed using pip, the Python package installer.

pip install docker

It is recommended to install this within a Python virtual environment to manage dependencies effectively.

Quickstart example

This section provides basic quickstart examples for interacting with the Docker Engine using the official SDKs. These examples focus on common operations like listing images, which implicitly interact with images potentially sourced from or destined for Docker Hub.

Python quickstart: List Docker images

This Python snippet demonstrates how to connect to the Docker daemon and list all available images on the local system. These images could have been pulled from Docker Hub or built locally.

import docker

try:
    # Connect to the Docker daemon
    client = docker.from_env()
    
    print("Listing Docker images:")
    # Iterate over all images and print their tags
    for image in client.images.list():
        print(f"  - {image.tags}")

except docker.errors.DockerException as e:
    print(f"Error connecting to Docker: {e}")
    print("Ensure Docker daemon is running and accessible.")

To run this example, save it as a .py file and execute it with Python. Ensure your Docker daemon is running and accessible from your environment.

Go quickstart: List Docker images

This Go example shows how to list Docker images using the Go client library for the Docker Engine API.

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/docker/docker/client"
)

func main() {
	ctx := context.Background()
	cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
	if err != nil {
		log.Fatalf("Error creating Docker client: %v", err)
	}

	images, err := cli.ImageList(ctx, types.ImageListOptions{})
	if err != nil {
		log.Fatalf("Error listing images: %v", err)
	}

	fmt.Println("Listing Docker images:")
	for _, image := range images {
		fmt.Printf("  - %v\n", image.RepoTags)
	}
}

Before running, ensure you have the necessary Go modules, including github.com/docker/docker/client and github.com/docker/go-connections/tlsconfig, properly fetched. You might also need to import github.com/docker/docker/api/types for types.ImageListOptions.

Community libraries

Beyond the official SDKs, the Docker ecosystem benefits from a variety of community-contributed libraries that extend programmatic interaction with Docker Hub and the Docker Engine to other languages and use cases. These libraries often wrap the Docker Engine API or target the specific Docker Hub API for repository and account management.

Node.js

  • docker-modem: A low-level HTTP client for the Docker Engine API. Many higher-level Node.js Docker libraries build upon this.
  • dockerode: A popular, high-level Node.js client for the Docker Engine API, providing a fluent interface for managing containers, images, networks, and volumes. It simplifies many Docker operations for Node.js applications.

Java

  • docker-java: A comprehensive Java library for the Docker Remote API. It supports most Docker Engine API operations and is widely used in Java-based applications and frameworks for Docker integration.

PHP

  • docker-php: A PHP client for the Docker Engine API, enabling PHP applications to interact with Docker. It provides methods for managing containers, images, and other Docker resources.

Ruby

  • docker-api: A Ruby client for the Docker Remote API. It allows Ruby applications to manage Docker containers and images programmatically, often used in automation scripts and web applications.

.NET (C#)

  • Docker.DotNet: A .NET client library for the Docker Remote API. It provides a C# interface for interacting with the Docker Engine, suitable for .NET applications and services needing Docker integration.

When choosing a community library, it is advisable to consider its active maintenance, community support, and compatibility with the Docker Engine API version you intend to use. Refer to the respective project documentation for installation instructions and usage details.