Skip to content

go-zoox/docker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

49 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Docker - Simply Docker SDK

PkgGoDev Build Status Go Report Card Coverage Status GitHub issues Release

A simple and easy-to-use Docker SDK that provides a Go wrapper for Docker API. Supports management operations for containers, images, networks, volumes, and system information.

Features

  • 🚀 Simple API - Uses functional options pattern with clean and intuitive API design
  • 📦 Complete Features - Supports core Docker functionality including containers, images, networks, volumes, and more
  • đź”§ Flexible Configuration - Supports both local and remote Docker hosts
  • 🛠️ CLI Tool - Built-in command-line tool for quick operations
  • 📚 Type Safe - Complete type definitions with excellent IDE support

Installation

go get -u github.com/go-zoox/docker

Quick Start

Create Client

package main

import (
    "context"
    "fmt"
    "github.com/go-zoox/docker"
)

func main() {
    // Use default configuration (reads from environment variables)
    client, err := docker.New()
    if err != nil {
        panic(err)
    }

    // Or specify Docker server address
    client, err = docker.New(func(cfg *docker.Config) {
        cfg.Server = "tcp://localhost:2375"
    })
    if err != nil {
        panic(err)
    }

    // Use the client...
}

API Documentation

Container Management

Container management provides complete container lifecycle operations.

List Containers

containers, err := client.Container().List(ctx)
if err != nil {
    return err
}

for _, container := range containers {
    fmt.Printf("ID: %s, Name: %s, Status: %s\n", 
        container.ID, container.Names, container.Status)
}

Create Container

import (
    "github.com/go-zoox/docker/container"
    dc "github.com/docker/docker/api/types/container"
)

response, err := client.Container().Create(ctx, func(opt *container.CreateOptions) {
    opt.Name = "my-container"
    opt.Container.Image = "nginx:latest"
    opt.Container.Cmd = []string{"nginx", "-g", "daemon off;"}
    opt.Container.Env = []string{"ENV=production"}
})

Start/Stop/Restart Container

// Start container
err := client.Container().Start(ctx, "container-id")

// Stop container
err := client.Container().Stop(ctx, "container-id", func(opt *container.StopOptions) {
    opt.Timeout = 30 * time.Second
})

// Restart container
err := client.Container().Restart(ctx, "container-id")

View Container Logs

logs, err := client.Container().Logs(ctx, "container-id", func(opt *container.LogsConfig) {
    opt.ShowStdout = true
    opt.ShowStderr = true
    opt.Follow = true
    opt.Tail = "100"
})
if err != nil {
    return err
}
defer logs.Close()

io.Copy(os.Stdout, logs)

Execute Container Command

term, err := client.Container().Exec(ctx, "container-id", func(opt *container.ExecOptions) {
    opt.Cmd = []string{"ls", "-la"}
    opt.Tty = true
})
if err != nil {
    return err
}
defer term.Close()

io.Copy(os.Stdout, term)

View Container Statistics

stats, err := client.Container().Stats(ctx, "container-id")
if err != nil {
    return err
}
defer stats.Close()

// Read statistics stream
io.Copy(os.Stdout, stats)

Run Container (Create and Start)

err := client.Container().Run(ctx, func(opt *container.RunOptions) {
    opt.Image = "nginx:latest"
    opt.Name = "my-nginx"
    opt.Remove = true // Auto-remove on exit
})

Image Management

Image management provides operations for building, pulling, and pushing images.

List Images

images, err := client.Image().List(ctx)
if err != nil {
    return err
}

for _, image := range images {
    fmt.Printf("ID: %s, Tags: %v, Size: %d\n", 
        image.ID, image.RepoTags, image.Size)
}

Pull Image

err := client.Image().Pull(ctx, "nginx:latest", func(cfg *image.PullConfig) {
    cfg.All = false
})

Push Image

err := client.Image().Push(ctx, "my-registry/nginx:latest", func(cfg *image.PushConfig) {
    cfg.All = false
})

Build Image

err := client.Image().Build(ctx, "./dockerfile-dir", func(cfg *image.BuildConfig) {
    cfg.Dockerfile = "Dockerfile"
    cfg.Tags = []string{"my-image:latest"}
})

Remove Image

results, err := client.Image().Remove(ctx, "image-id", func(cfg *image.RemoveConfig) {
    cfg.Force = false
    cfg.PruneChildren = true
})

Prune Unused Images

import "github.com/docker/docker/api/types/filters"

report, err := client.Image().Prune(ctx, func(cfg *image.PruneConfig) {
    *cfg = filters.NewArgs()
    cfg.Add("dangling", "true")
})
fmt.Printf("Deleted: %d, Space Reclaimed: %d\n", 
    len(report.ImagesDeleted), report.SpaceReclaimed)

Network Management

Network management provides Docker network creation, deletion, and querying functionality.

List Networks

networks, err := client.Network().List(ctx)
if err != nil {
    return err
}

for _, network := range networks {
    fmt.Printf("ID: %s, Name: %s, Driver: %s\n", 
        network.ID, network.Name, network.Driver)
}

Create Network

import (
    "github.com/go-zoox/docker/network"
    dnetwork "github.com/docker/docker/api/types/network"
)

response, err := client.Network().Create(ctx, "my-network", func(opt *network.CreateOption) {
    opt.Driver = "bridge"
    opt.IPAM = &dnetwork.IPAM{
        Config: []dnetwork.IPAMConfig{
            {Subnet: "172.20.0.0/16"},
        },
    }
})

Remove Network

err := client.Network().Remove(ctx, "network-id")

Prune Unused Networks

report, err := client.Network().Prune(ctx)
fmt.Printf("Deleted: %d\n", len(report.NetworksDeleted))

Volume Management

Volume management provides Docker volume creation, deletion, and querying functionality.

List Volumes

volumes, err := client.Volume().List(ctx)
if err != nil {
    return err
}

for _, volume := range volumes {
    fmt.Printf("Name: %s, Driver: %s, Mountpoint: %s\n", 
        volume.Name, volume.Driver, volume.Mountpoint)
}

Create Volume

import (
    "github.com/go-zoox/docker/volume"
    vo "github.com/docker/docker/api/types/volume"
)

vol, err := client.Volume().Create(ctx, func(opt *volume.CreateOption) {
    opt.Name = "my-volume"
    opt.Driver = "local"
    opt.Labels = map[string]string{
        "env": "production",
    }
})

Remove Volume

err := client.Volume().Remove(ctx, "volume-name", func(opt *volume.RemoveOption) {
    opt.Force = false
})

Prune Unused Volumes

import "github.com/docker/docker/api/types/filters"

report, err := client.Volume().Prune(ctx, func(opt *volume.PruneOption) {
    opt.Filters = filters.NewArgs()
    opt.Filters.Add("label", "env=test")
})
fmt.Printf("Deleted: %d, Space Reclaimed: %d\n", 
    len(report.VolumesDeleted), report.SpaceReclaimed)

System Information

System information provides Docker system-related querying functionality.

Get System Info

info, err := client.Info().Get(ctx)
if err != nil {
    return err
}

fmt.Printf("Containers: %d, Images: %d\n", 
    info.Containers, info.Images)
fmt.Printf("Server Version: %s\n", info.ServerVersion)

Get Version Info

version, err := client.Info().Version(ctx)
if err != nil {
    return err
}

fmt.Printf("API Version: %s\n", version.APIVersion)
fmt.Printf("Go Version: %s\n", version.GoVersion)

Get Disk Usage

diskUsage, err := client.Info().Disk(ctx)
if err != nil {
    return err
}

fmt.Printf("Images Size: %d\n", diskUsage.ImagesSize)
fmt.Printf("Containers Size: %d\n", diskUsage.ContainersSize)
fmt.Printf("Volumes Size: %d\n", diskUsage.VolumesSize)

CLI Tool

The project also provides a command-line tool for quick Docker operations.

Install CLI

go install github.com/go-zoox/docker/cmd/docker@latest

Usage Examples

# List containers
docker container list

# View container logs
docker container logs <container-id>

# Execute container command
docker container exec <container-id> ls -la

# List images
docker image list

# Pull image
docker image pull nginx:latest

# List networks
docker network list

# Create network
docker network create my-network

# List volumes
docker volume list

# Create volume
docker volume create my-volume

Complete Example

package main

import (
    "context"
    "fmt"
    "log"

    "github.com/go-zoox/docker"
    "github.com/go-zoox/docker/container"
)

func main() {
    ctx := context.Background()

    // Create client
    client, err := docker.New()
    if err != nil {
        log.Fatal(err)
    }

    // Create and run container
    err = client.Container().Run(ctx, func(opt *container.RunOptions) {
        opt.Image = "nginx:latest"
        opt.Name = "test-nginx"
        opt.Remove = true
    })
    if err != nil {
        log.Fatal(err)
    }

    // List all containers
    containers, err := client.Container().List(ctx)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("Found %d containers\n", len(containers))
    for _, c := range containers {
        fmt.Printf("- %s: %s\n", c.Names[0], c.Status)
    }

    // Get system information
    info, err := client.Info().Get(ctx)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("\nDocker Info:\n")
    fmt.Printf("  Containers: %d\n", info.Containers)
    fmt.Printf("  Images: %d\n", info.Images)
    fmt.Printf("  Server Version: %s\n", info.ServerVersion)
}

License

GoZoox is released under the MIT License.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages