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!
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