在Docker Golang SDK的imagePull方法中传递本地Docker镜像。

huangapple go评论96阅读模式
英文:

Pass local docker image in Docker Golang SDK imagePull method

问题

我想从我的本地系统中拉取一个Docker镜像。

镜像名称是:example

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镜像传递给这个方法?如果可以,如何操作?

英文:

I want to pull a docker image from my local system.

Image name is : example

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)
}

current code is giving me something like this :

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

Is it possble to pass local docker image to this method, if yes, how ?

答案1

得分: 2

首先,你可以使用标签'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!

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

  fmt.Println(out)
}

英文:

First, you can build image locally with tag 'localhost/example:latest'

 
  $ docker build -t &quot;localhost/example:latest&quot; .

Than, you can prepend localhost to image name, so it will try local registry to pull image, so, you can try this:


package main

import (
    &quot;fmt&quot;

    &quot;github.com/docker/docker/api/types&quot;
    &quot;github.com/docker/docker/api/types/container&quot;
    &quot;github.com/docker/docker/client&quot;
   
)


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

  imageName := &quot;localhost/example:latest&quot; // &lt;- localhost prepended!

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

  fmt.Println(out)
}

huangapple
  • 本文由 发表于 2021年9月24日 17:10:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/69312437.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定