Keval Bhogayata
Keval Bhogayata

Reputation: 6706

Pass local docker image in Docker Golang SDK imagePull method

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 ?

Upvotes: 2

Views: 802

Answers (1)

vodolaz095
vodolaz095

Reputation: 6986

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

Upvotes: 2

Related Questions