登录
首页 >  Golang >  Go问答

利用Docker SDK从文件中导入镜像

来源:stackoverflow

时间:2024-03-28 20:27:27 217浏览 收藏

IT行业相对于一般传统行业,发展更新速度更快,一旦停止了学习,很快就会被行业所淘汰。所以我们需要踏踏实实的不断学习,精进自己的技术,尤其是初学者。今天golang学习网给大家整理了《利用Docker SDK从文件中导入镜像》,聊聊,我们一起来看看吧!

问题内容

我使用 imageload 函数来加载图像:

file, err := os.openfile(filename, os.o_rdonly, 0666)
    if err != nil {
        log.debugf("error loading image %s, %s", filename, err)
    } else {
        defer file.close()
        resp, err := client.api.imageload(ctx, file, false)
        if err != nil {
            log.debugf("error loading image %s, %s", filename, err)
        }
        if resp.body == nil {
            err = errors.new("error loading image")
        } else {
            defer resp.body.close()
        }
    }

但是如果我加载一个不是图像的文件,我就没有错误。 如果我使用控制台,它会按预期工作:

$ docker load -i s.tar
open /var/lib/docker/tmp/docker-import-075289246/repositories: no such file or directory

那么,为什么我在使用 sdk 时没有出现错误?


解决方案


我已经为我添加了工作示例:

package main

import (
    "bytes"
    "fmt"
    "io/ioutil"
    "log"

    "github.com/docker/docker/client"
    "golang.org/x/net/context"
)

func main() {
    cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
    if err != nil {
        panic(err)
    }
    input := bytes.NewReader([]byte("dummy content\n"))
    imageLoadResponse, err := cli.ImageLoad(context.Background(), input, true)
    if err != nil {
        log.Fatal(err)
    }
    body, err := ioutil.ReadAll(imageLoadResponse.Body)
    fmt.Println(string(body))
}
//Output:
// {"errorDetail":{"message":"Error processing tar file(exit status 1): unexpected EOF"},"error":"Error processing tar file(exit status 1): unexpected EOF"}

您还可以查看 docker 如何加载 command

以上就是《利用Docker SDK从文件中导入镜像》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>