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.
- 🚀 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
go get -u github.com/go-zoox/dockerpackage 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...
}Container management provides complete container lifecycle operations.
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)
}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 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")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)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)stats, err := client.Container().Stats(ctx, "container-id")
if err != nil {
return err
}
defer stats.Close()
// Read statistics stream
io.Copy(os.Stdout, stats)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 provides operations for building, pulling, and pushing 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)
}err := client.Image().Pull(ctx, "nginx:latest", func(cfg *image.PullConfig) {
cfg.All = false
})err := client.Image().Push(ctx, "my-registry/nginx:latest", func(cfg *image.PushConfig) {
cfg.All = false
})err := client.Image().Build(ctx, "./dockerfile-dir", func(cfg *image.BuildConfig) {
cfg.Dockerfile = "Dockerfile"
cfg.Tags = []string{"my-image:latest"}
})results, err := client.Image().Remove(ctx, "image-id", func(cfg *image.RemoveConfig) {
cfg.Force = false
cfg.PruneChildren = true
})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 provides Docker network creation, deletion, and querying functionality.
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)
}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"},
},
}
})err := client.Network().Remove(ctx, "network-id")report, err := client.Network().Prune(ctx)
fmt.Printf("Deleted: %d\n", len(report.NetworksDeleted))Volume management provides Docker volume creation, deletion, and querying functionality.
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)
}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",
}
})err := client.Volume().Remove(ctx, "volume-name", func(opt *volume.RemoveOption) {
opt.Force = false
})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 provides Docker system-related querying functionality.
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)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)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)The project also provides a command-line tool for quick Docker operations.
go install github.com/go-zoox/docker/cmd/docker@latest# 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-volumepackage 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)
}GoZoox is released under the MIT License.