【问题标题】:Pass local docker image in Docker Golang SDK imagePull method在 Docker Golang SDK imagePull 方法中传递本地 docker 镜像
【发布时间】:2021-11-17 14:03:57
【问题描述】:

我想从我的本地系统中提取一个 docker 映像。

图片名称是:示例

import (

    "github.com/docker/docker/api/types"
    "github.com/docker/docker/api/types/container"
    "github.com/docker/docker/client"
    
)

cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
    panic(err)
}

imageName := "example:latest"

out, err := cli.ImagePull(ctx, imageName, types.ImagePullOptions{})
if err != nil {
    panic(err)
}

当前代码给了我这样的东西:

panic: Error response from daemon: pull access denied for example, repository does not exist or may require 'docker login': denied: requested access to the resource is denied

是否可以将本地docker镜像传递给该方法,如果可以,如何?

【问题讨论】:

    标签: docker go


    【解决方案1】:

    首先,您可以使用标记“localhost/example:latest”在本地构建映像

     
      $ docker build -t "localhost/example:latest" .
    
    

    然后,你可以在图像名称前加上localhost,这样它会尝试本地注册表来拉图像,所以,你可以试试这个:

    
    package main
    
    import (
        "fmt"
    
        "github.com/docker/docker/api/types"
        "github.com/docker/docker/api/types/container"
        "github.com/docker/docker/client"
       
    )
    
    
    func main(){
      cli, err := client.NewClientWithOpts(client.FromEnv,   client.WithAPIVersionNegotiation())
      if err != nil {
          panic(err)
      }
    
      imageName := "localhost/example:latest" // <- localhost prepended!
    
      out, err := cli.ImagePull(ctx, imageName, types.ImagePullOptions{})
      if err != nil {
        panic(err)
      }
    
      fmt.Println(out)
    }
    
    

    【讨论】:

    • AFAIK,还应该包含:port 部分,如“当标签的第一部分是主机名和端口...”来自docs.docker.com/registry/deploying/…
    • 对我有用的解决方案:我在本地创建了一个 docker 注册表并将其映射到 localhost: 然后我能够将图像名称传递为:/example:latest跨度>
    猜你喜欢
    • 1970-01-01
    • 2019-08-13
    • 1970-01-01
    • 1970-01-01
    • 2018-09-14
    • 2021-11-04
    • 1970-01-01
    • 2020-01-06
    • 1970-01-01
    相关资源
    最近更新 更多