0

I am trying to use the docker go client to connect to the google container registry to list and delete images. My golang application also exists in a docker container.

This is the dockerfile for my golang application:

FROM docker:latest

USER root

RUN apk add --update openssl

ADD ./data /app/data
ADD ./data/docker /app/data/docker
ADD mygolangapp /app

RUN chmod -R a+rwx ./app/data/docker/generate_docker_cert.sh
RUN sh ./app/data/docker/generate_docker_cert.sh

ENV GOOGLE_APPLICATION_CREDENTIALS ./app/data/myserviceaccount.json
ENV DOCKER_CONFIG ./app/data/docker
ENV DOCKER_CERT_PATH .
ENV DOCKER_HOST ????????

ENTRYPOINT ["/app/mygolangapp"]

This is the generate_docker_cert.sh file (https://gist.github.com/bradrydzewski/a6090115b3fecfc25280)

This is my golang code to create the docker go client and list containers.

jsonBytes, err := ioutil.ReadFile(os.Getenv("GOOGLE_APPLICATION_CREDENTIALS"))
if err != nil {
    panic(err)
}

dockercli, err := client.NewEnvClient()
if err != nil {
    panic(err)
}

dockercli.RegistryLogin(context.Background(), types.AuthConfig{
    Username:      "_json_key",
    Password:      string(jsonBytes),
    ServerAddress: "https://eu.gcr.io",
})

containers, err := dockercli.ContainerList(context.Background(), types.ContainerListOptions{})
if err != nil {
    panic(err)
}

for _, container := range containers {
    fmt.Printf("%s %s\n", container.ID[:10], container.Image)
}

Currently I am getting this error:

error during connect: Get https://%2Fvar%2Frun%2Fdocker.sock/v1.25/containers/json?limit=0: dial tcp: lookup /var/run/docker.sock: no such host

So I added docker.sock as a volume in my docker-compose, but it is not working?

mygolangapp:
  build: ./mygolangapp
  volumes:
    - /var/run/docker.sock:/var/run/docker.sock

My question is: How can I use the docker golang client within a golang application for google container registry. What am I doing wrong or missing here? What should DOCKER_HOST be?

Thank you for any help. Any other approach is more than welcome!

KcinnaY
  • 73
  • 1
  • 9

1 Answers1

0

As a potential alternate, you could explore the google/go-containerregistry library: https://github.com/google/go-containerregistry

It looks like you're trying to implement some sort of garbage collection tool. If so, you could also look here for an example of how to use the library: https://github.com/google/go-containerregistry/pull/300

  • I am aware of go-containerregistry, but it is not able to delete images yet according to one of its developers. – KcinnaY Dec 01 '18 at 10:14
  • I'm not very familiar with the library, but I believe it does support deletes. Deletion code: https://github.com/google/go-containerregistry/blob/master/pkg/v1/remote/delete.go Example usage: https://github.com/google/go-containerregistry/blob/master/pkg/crane/delete.go – Philippe Deslauriers Dec 02 '18 at 18:15
  • You are right, you are able to delete using crane with the NewCmdDelete command (https://github.com/google/go-containerregistry/blob/master/pkg/crane/delete.go). It removes a reference from the image. First you have to remove all the tags then you have to do another delete command using the @sha256 of the image. Now there is no reference left and it will delete the image. – KcinnaY Dec 07 '18 at 14:17