英文:
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 "localhost/example:latest" .
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 (
"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)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论