本文整理汇总了Golang中github.com/ailispaw/talk2docker/client.NewDockerClient函数的典型用法代码示例。如果您正苦于以下问题:Golang NewDockerClient函数的具体用法?Golang NewDockerClient怎么用?Golang NewDockerClient使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewDockerClient函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: pullImageInSilence
func pullImageInSilence(ctx *cobra.Command, name string) error {
r, n, t, err := client.ParseRepositoryName(name)
if err != nil {
return err
}
name = n + ":" + t
if r != "" {
name = r + "/" + name
}
f, err := os.Open(os.DevNull)
if err != nil {
return err
}
docker, err := client.NewDockerClient(configPath, hostName, f)
if err != nil {
return err
}
if _, err := docker.PullImage(name); err != nil {
return err
}
return nil
}
开发者ID:ChengTiesheng,项目名称:talk2docker,代码行数:26,代码来源:image.go
示例2: inspectContainers
func inspectContainers(ctx *cobra.Command, args []string) {
if len(args) < 1 {
ErrorExit(ctx, "Needs an argument <NAME|ID> at least to inspect")
}
docker, err := client.NewDockerClient(configPath, hostName, ctx.Out())
if err != nil {
log.Fatal(err)
}
var containers []api.ContainerInfo
var gotError = false
for _, name := range args {
if containerInfo, err := docker.InspectContainer(name); err != nil {
log.Error(err)
gotError = true
} else {
containers = append(containers, *containerInfo)
}
}
if len(containers) > 0 {
if err := FormatPrint(ctx.Out(), containers); err != nil {
log.Fatal(err)
}
}
if gotError {
log.Fatal("Error: failed to inspect one or more containers")
}
}
开发者ID:ChengTiesheng,项目名称:talk2docker,代码行数:32,代码来源:container.go
示例3: getContainerChanges
func getContainerChanges(ctx *cobra.Command, args []string) {
if len(args) < 1 {
ErrorExit(ctx, "Needs an argument <NAME|ID> to get changes")
}
docker, err := client.NewDockerClient(configPath, hostName, ctx.Out())
if err != nil {
log.Fatal(err)
}
changes, err := docker.GetContainerChanges(args[0])
if err != nil {
log.Fatal(err)
}
for _, change := range changes {
var kind string
switch change.Kind {
case api.CHANGE_TYPE_MODIFY:
kind = "C"
case api.CHANGE_TYPE_ADD:
kind = "A"
case api.CHANGE_TYPE_DELETE:
kind = "D"
}
ctx.Printf("%s %s\n", kind, change.Path)
}
}
开发者ID:ChengTiesheng,项目名称:talk2docker,代码行数:28,代码来源:container.go
示例4: inspectImages
func inspectImages(ctx *cobra.Command, args []string) {
if len(args) < 1 {
ErrorExit(ctx, "Needs an argument <NAME[:TAG]|ID> at least to inspect")
}
docker, err := client.NewDockerClient(configPath, hostName, ctx.Out())
if err != nil {
log.Fatal(err)
}
var images []api.ImageInfo
var gotError = false
for _, name := range args {
if imageInfo, err := docker.InspectImage(name); err != nil {
log.Error(err)
gotError = true
} else {
images = append(images, *imageInfo)
}
}
if len(images) > 0 {
if err := FormatPrint(ctx.Out(), images); err != nil {
log.Fatal(err)
}
}
if gotError {
log.Fatal("Error: failed to inspect one or more images")
}
}
开发者ID:ChengTiesheng,项目名称:talk2docker,代码行数:32,代码来源:image.go
示例5: getContainerProcesses
func getContainerProcesses(ctx *cobra.Command, args []string) {
if len(args) < 1 {
ErrorExit(ctx, "Needs an argument <NAME|ID> to execute ps")
}
docker, err := client.NewDockerClient(configPath, hostName, ctx.Out())
if err != nil {
log.Fatal(err)
}
ps_args := ""
if len(args) > 1 {
ps_args = strings.Join(args[1:], " ")
}
ps, err := docker.GetContainerProcesses(args[0], ps_args)
if err != nil {
log.Fatal(err)
}
if boolYAML || boolJSON {
if err := FormatPrint(ctx.Out(), ps); err != nil {
log.Fatal(err)
}
return
}
PrintInTable(ctx.Out(), ps.Titles, ps.Processes, 100, tablewriter.ALIGN_DEFAULT)
}
开发者ID:ChengTiesheng,项目名称:talk2docker,代码行数:29,代码来源:container.go
示例6: commitContainer
func commitContainer(ctx *cobra.Command, args []string) {
if len(args) < 2 {
ErrorExit(ctx, "Needs two arguments to commit <CONTAINER-NAME|ID> to <IMAGE-NAME[:TAG]>")
}
reg, name, tag, err := client.ParseRepositoryName(args[1])
if err != nil {
log.Fatal(err)
}
if reg != "" {
name = reg + "/" + name
}
docker, err := client.NewDockerClient(configPath, hostName, ctx.Out())
if err != nil {
log.Fatal(err)
}
if _, err := docker.CommitContainer(args[0], name, tag, message, author, boolPause); err != nil {
log.Fatal(err)
}
ctx.Printf("Committed %s as %s:%s\n", args[0], name, tag)
}
开发者ID:ChengTiesheng,项目名称:talk2docker,代码行数:25,代码来源:container.go
示例7: tagImage
func tagImage(ctx *cobra.Command, args []string) {
if len(args) < 2 {
ErrorExit(ctx, "Needs two arguments <NAME[:TAG]|ID> <NEW-NAME[:TAG]>")
}
reg, name, tag, err := client.ParseRepositoryName(args[1])
if err != nil {
log.Fatal(err)
}
if reg != "" {
name = reg + "/" + name
}
docker, err := client.NewDockerClient(configPath, hostName, ctx.Out())
if err != nil {
log.Fatal(err)
}
if err := docker.TagImage(args[0], name, tag, boolForce); err != nil {
log.Fatal(err)
}
ctx.Printf("Tagged %s as %s:%s\n", args[0], name, tag)
}
开发者ID:ChengTiesheng,项目名称:talk2docker,代码行数:25,代码来源:image.go
示例8: pullImage
func pullImage(ctx *cobra.Command, args []string) {
if len(args) < 1 {
ErrorExit(ctx, "Needs an argument <NAME[:TAG]> to pull")
}
reg, name, tag, err := client.ParseRepositoryName(args[0])
if err != nil {
log.Fatal(err)
}
repository := name + ":" + tag
if boolAll {
repository = name
}
if reg != "" {
repository = reg + "/" + repository
}
docker, err := client.NewDockerClient(configPath, hostName, ctx.Out())
if err != nil {
log.Fatal(err)
}
if _, err := docker.PullImage(repository); err != nil {
log.Fatal(err)
}
}
开发者ID:ChengTiesheng,项目名称:talk2docker,代码行数:29,代码来源:image.go
示例9: searchImages
func searchImages(ctx *cobra.Command, args []string) {
if len(args) < 1 {
ErrorExit(ctx, "Needs an argument <TERM> to search")
}
docker, err := client.NewDockerClient(configPath, hostName, ctx.Out())
if err != nil {
log.Fatal(err)
}
images, err := docker.SearchImages(args[0])
if err != nil {
log.Fatal(err)
}
if boolStar {
sort.Sort(sort.Reverse(api.SortImagesByStars{ImageSearchResults: images}))
} else {
sort.Sort(api.SortImagesByName{ImageSearchResults: images})
}
if boolQuiet {
for _, image := range images {
ctx.Println(image.Name)
}
return
}
if boolYAML || boolJSON {
if err := FormatPrint(ctx.Out(), images); err != nil {
log.Fatal(err)
}
return
}
var items [][]string
for _, image := range images {
out := []string{
image.Name,
image.Description,
FormatInt(int64(image.Stars)),
FormatBool(image.Official, "*", " "),
FormatBool(image.Automated, "*", " "),
}
items = append(items, out)
}
header := []string{
"Name",
"Description",
"Stars",
"Official",
"Automated",
}
PrintInTable(ctx.Out(), header, items, 50, tablewriter.ALIGN_DEFAULT)
}
开发者ID:ChengTiesheng,项目名称:talk2docker,代码行数:58,代码来源:image.go
示例10: exportVolume
func exportVolume(ctx *cobra.Command, args []string) {
if len(args) < 1 {
ErrorExit(ctx, "Needs an argument <ID|CONTAINER-NAME:PATH> to export")
}
volumes, err := getVolumes(ctx)
if err != nil {
log.Fatal(err)
}
volume := volumes.Find(args[0])
if volume == nil {
log.Fatalf("No such volume: %s", args[0])
}
var (
config api.Config
hostConfig api.HostConfig
)
config.Image = "busybox:latest"
hostConfig.Binds = []string{volume.Path + ":/" + volume.ID}
docker, err := client.NewDockerClient(configPath, hostName, ctx.Out())
if err != nil {
log.Fatal(err)
}
var cid string
cid, err = docker.CreateContainer("", config, hostConfig)
if err != nil {
if apiErr, ok := err.(api.Error); ok && (apiErr.StatusCode == 404) {
if err := pullImageInSilence(ctx, config.Image); err != nil {
log.Fatal(err)
}
cid, err = docker.CreateContainer("", config, hostConfig)
if err != nil {
log.Fatal(err)
}
} else {
log.Fatal(err)
}
}
defer docker.RemoveContainer(cid, true)
if err := docker.CopyContainer(cid, "/"+volume.ID); err != nil {
log.Fatal(err)
}
}
开发者ID:ChengTiesheng,项目名称:talk2docker,代码行数:51,代码来源:volume.go
示例11: removeVolume
func removeVolume(ctx *cobra.Command, volume *Volume) error {
var (
config api.Config
hostConfig api.HostConfig
)
config.Cmd = []string{"/bin/sh", "-c", "rm -rf /.docker_volume_config/" + volume.ID}
config.Image = "busybox:latest"
hostConfig.Binds = []string{filepath.Dir(volume.configPath) + ":/.docker_volume_config"}
if !volume.IsBindMount {
config.Cmd[2] = config.Cmd[2] + (" && rm -rf /.docker_volume/" + volume.ID)
hostConfig.Binds = append(hostConfig.Binds, filepath.Dir(volume.Path)+":/.docker_volume")
}
docker, err := client.NewDockerClient(configPath, hostName, ctx.Out())
if err != nil {
return err
}
var cid string
cid, err = docker.CreateContainer("", config, hostConfig)
if err != nil {
if apiErr, ok := err.(api.Error); ok && (apiErr.StatusCode == 404) {
if err := pullImageInSilence(ctx, config.Image); err != nil {
return err
}
cid, err = docker.CreateContainer("", config, hostConfig)
if err != nil {
return err
}
} else {
return err
}
}
defer docker.RemoveContainer(cid, true)
if err := docker.StartContainer(cid); err != nil {
return err
}
if _, err := docker.WaitContainer(cid); err != nil {
return err
}
return nil
}
开发者ID:ChengTiesheng,项目名称:talk2docker,代码行数:50,代码来源:volume.go
示例12: buildImage
func buildImage(ctx *cobra.Command, args []string) {
path := "."
if len(args) > 0 {
path = args[0]
}
docker, err := client.NewDockerClient(configPath, hostName, ctx.Out())
if err != nil {
log.Fatal(err)
}
if _, err := docker.BuildImage(path, imageTag, boolQuiet); err != nil {
log.Fatal(err)
}
}
开发者ID:ChengTiesheng,项目名称:talk2docker,代码行数:15,代码来源:image.go
示例13: exportContainer
func exportContainer(ctx *cobra.Command, args []string) {
if len(args) < 1 {
ErrorExit(ctx, "Needs an argument <NAME|ID> to export")
}
docker, err := client.NewDockerClient(configPath, hostName, ctx.Out())
if err != nil {
log.Fatal(err)
}
if len(args) > 1 {
if err := docker.CopyContainer(args[0], args[1]); err != nil {
log.Fatal(err)
}
} else {
if err := docker.ExportContainer(args[0]); err != nil {
log.Fatal(err)
}
}
}
开发者ID:ChengTiesheng,项目名称:talk2docker,代码行数:20,代码来源:container.go
示例14: pushImage
func pushImage(ctx *cobra.Command, args []string) {
if len(args) < 1 {
ErrorExit(ctx, "Needs an argument <NAME[:TAG]> to push")
}
reg, name, tag, err := client.ParseRepositoryName(args[0])
if err != nil {
log.Fatal(err)
}
if len(strings.SplitN(name, "/", 2)) == 1 {
log.Fatalf("You cannot push a \"root\" repository. Please rename your repository in <yourname>/%s", name)
}
if reg != "" {
name = reg + "/" + name
}
config, err := client.LoadConfig(configPath)
if err != nil {
log.Fatal(err)
}
if reg == "" {
reg = client.INDEX_SERVER
}
registry, err := config.GetRegistry(reg)
if (err != nil) || (registry.Credentials == "") {
log.Fatal("Please login prior to pushing an image.")
}
docker, err := client.NewDockerClient(configPath, hostName, ctx.Out())
if err != nil {
log.Fatal(err)
}
if _, err := docker.PushImage(name, tag, registry.Credentials); err != nil {
log.Fatal(err)
}
}
开发者ID:ChengTiesheng,项目名称:talk2docker,代码行数:41,代码来源:image.go
示例15: removeImages
func removeImages(ctx *cobra.Command, args []string) {
if len(args) < 1 {
ErrorExit(ctx, "Needs an argument <NAME[:TAG]> at least to remove")
}
docker, err := client.NewDockerClient(configPath, hostName, ctx.Out())
if err != nil {
log.Fatal(err)
}
var gotError = false
for _, name := range args {
if err := docker.RemoveImage(name, boolForce, boolNoPrune); err != nil {
log.Error(err)
gotError = true
}
}
if gotError {
log.Fatal("Error: failed to remove one or more images")
}
}
开发者ID:ChengTiesheng,项目名称:talk2docker,代码行数:21,代码来源:image.go
示例16: getContainerLogs
func getContainerLogs(ctx *cobra.Command, args []string) {
if len(args) < 1 {
ErrorExit(ctx, "Needs an argument <NAME|ID> to get logs")
}
docker, err := client.NewDockerClient(configPath, hostName, ctx.Out())
if err != nil {
log.Fatal(err)
}
logs, err := docker.GetContainerLogs(args[0], false, true, true, boolTimestamps, tail)
if err != nil {
log.Fatal(err)
}
if logs[0] != "" {
fmt.Fprint(os.Stdout, logs[0])
}
if logs[1] != "" {
fmt.Fprint(os.Stderr, logs[1])
}
}
开发者ID:ChengTiesheng,项目名称:talk2docker,代码行数:22,代码来源:container.go
示例17: waitContainers
func waitContainers(ctx *cobra.Command, args []string) {
if len(args) < 1 {
ErrorExit(ctx, "Needs an argument <NAME|ID> at least to wait")
}
docker, err := client.NewDockerClient(configPath, hostName, ctx.Out())
if err != nil {
log.Fatal(err)
}
var gotError = false
for _, name := range args {
if status, err := docker.WaitContainer(name); err != nil {
log.Error(err)
gotError = true
} else {
fmt.Fprintf(ctx.Out(), "%s: %d\n", name, status)
}
}
if gotError {
log.Fatal("Error: failed to wait one or more containers")
}
}
开发者ID:ChengTiesheng,项目名称:talk2docker,代码行数:23,代码来源:container.go
示例18: removeContainers
func removeContainers(ctx *cobra.Command, args []string) {
if len(args) < 1 {
ErrorExit(ctx, "Needs an argument <NAME|ID> at least to remove")
}
docker, err := client.NewDockerClient(configPath, hostName, ctx.Out())
if err != nil {
log.Fatal(err)
}
var gotError = false
for _, name := range args {
if err := docker.RemoveContainer(name, boolForce); err != nil {
log.Error(err)
gotError = true
} else {
ctx.Println(name)
}
}
if gotError {
log.Fatal("Error: failed to remove one or more containers")
}
}
开发者ID:ChengTiesheng,项目名称:talk2docker,代码行数:23,代码来源:container.go
示例19: composeContainer
func composeContainer(ctx *cobra.Command, root string, composer Composer) (string, error) {
var (
config api.Config
hostConfig api.HostConfig
localVolumes = make(map[string]struct{})
bindVolumes []string
exposedPorts = make(map[string]struct{})
portBindings = make(map[string][]api.PortBinding)
links []string
deviceMappings []api.DeviceMapping
)
if composer.Image != "" {
r, n, t, err := client.ParseRepositoryName(composer.Image)
if err != nil {
return "", err
}
composer.Image = n + ":" + t
if r != "" {
composer.Image = r + "/" + composer.Image
}
}
if (composer.WorkingDir != "") && !filepath.IsAbs(composer.WorkingDir) {
return "", fmt.Errorf("Invalid working directory: it must be absolute.")
}
docker, err := client.NewDockerClient(configPath, hostName, ctx.Out())
if err != nil {
return "", err
}
if composer.Build != "" {
if !filepath.IsAbs(composer.Build) {
composer.Build = filepath.Join(root, composer.Build)
}
message, err := docker.BuildImage(composer.Build, composer.Image, false)
if err != nil {
return "", err
}
if composer.Image == "" {
if _, err := fmt.Sscanf(message, "Successfully built %s", &composer.Image); err != nil {
return "", err
}
}
}
for _, port := range composer.Ports {
var (
rawPort = port
hostIp = ""
hostPort = ""
containerPort = ""
proto = "tcp"
)
if i := strings.LastIndex(port, "/"); i != -1 {
proto = strings.ToLower(port[i+1:])
port = port[:i]
}
parts := strings.Split(port, ":")
switch len(parts) {
case 1:
containerPort = parts[0]
case 2:
hostPort = parts[0]
containerPort = parts[1]
case 3:
hostIp = parts[0]
hostPort = parts[1]
containerPort = parts[2]
default:
return "", fmt.Errorf("Invalid port specification: %s", rawPort)
}
port := fmt.Sprintf("%s/%s", containerPort, proto)
if _, exists := exposedPorts[port]; !exists {
exposedPorts[port] = struct{}{}
}
portBinding := api.PortBinding{
HostIp: hostIp,
HostPort: hostPort,
}
bslice, exists := portBindings[port]
if !exists {
bslice = []api.PortBinding{}
}
portBindings[port] = append(bslice, portBinding)
}
for _, port := range composer.ExposedPorts {
var (
rawPort = port
containerPort = ""
proto = "tcp"
)
//.........这里部分代码省略.........
开发者ID:ChengTiesheng,项目名称:talk2docker,代码行数:101,代码来源:compose.go
示例20: showImageHistory
func showImageHistory(ctx *cobra.Command, args []string) {
if len(args) < 1 {
ErrorExit(ctx, "Needs an argument <NAME[:TAG]|ID>")
}
docker, err := client.NewDockerClient(configPath, hostName, ctx.Out())
if err != nil {
log.Fatal(err)
}
history, err := docker.GetImageHistory(args[0])
if err != nil {
log.Fatal(err)
}
// Just reverse history
var images api.ImageHistories
for i, l := 0, len(history); i < l; i++ {
images = append(images, history[l-i-1])
}
if boolYAML || boolJSON {
if err := FormatPrint(ctx.Out(), images); err != nil {
log.Fatal(err)
}
return
}
var items [][]string
for _, image := range images {
re := regexp.MustCompile("\\s+")
createdBy := re.ReplaceAllLiteralString(image.CreatedBy, " ")
re = regexp.MustCompile("^/bin/sh -c #\\(nop\\) ")
createdBy = re.ReplaceAllLiteralString(createdBy, "")
re = regexp.MustCompile("^/bin/sh -c")
createdBy = re.ReplaceAllLiteralString(createdBy, "RUN")
tags := strings.Join(image.Tags, ", ")
if !boolAll {
createdBy = FormatNonBreakingString(Truncate(createdBy, 50))
tags = FormatNonBreakingString(tags)
}
out := []string{
Truncate(image.Id, 12),
createdBy,
tags,
FormatDateTime(time.Unix(image.Created, 0)),
FormatFloat(float64(image.Size) / 1000000),
}
items = append(items, out)
}
header := []string{
"ID",
"Created by",
"Name:Tags",
"Created at",
"Size(MB)",
}
PrintInTable(ctx.Out(), header, items, 20, tablewriter.ALIGN_DEFAULT)
}
开发者ID:ChengTiesheng,项目名称:talk2docker,代码行数:62,代码来源:image.go
注:本文中的github.com/ailispaw/talk2docker/client.NewDockerClient函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论