doushenxu7294
2018-11-28 21:30
浏览 164

Docker容器内的Docker Golang客户端

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

图片转代码服务由CSDN问答提供 功能建议

我正在尝试使用 docker go client 连接到Google容器注册表以列出和删除图像。 我的golang应用程序也存在于docker容器中。

这是我的golang应用程序的dockerfile:

  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”] 
   
 
 

这是generate_docker_cert。 sh文件 ( https://gist.github.com/bradrydzewski/a6090115b3fecfc25280 )< / p>

这是我的golang代码,用于创建docker go客户端和列表容器。

  jsonBytes,err:= ioutil.ReadFile(os。  Getenv(“ GOOGLE_APPLICATION_CREDENTIALS”))
if er  r!= nil {
 panic(err)
} 
 
dockercli,err:= client.NewEnvClient()
if err!= nil {
 panic(err)
} 
 
dockercli.RegistryLogin(  context.Background(),类型。AuthConfig{
用户名:“ _ json_key”,
密码:字符串(jsonBytes),
 ServerAddress:“ https://eu.gcr.io”,
})
 \  ncontainers,err:= dockercli.ContainerList(context.Background(),types.ContainerListOptions {})
if err!= nil {
 panic(err)
} 
 
for _,container:=范围容器{\  n fmt.Printf(“%s%s 
”,container.ID [:10],container.Image)
} 
   
 
 

当前我正在 此错误:

 连接期间错误:获取https://%2Fvar%2Frun%2Fdocker.sock/v1.25/containers/json?limit = 0:拨打tcp: 查找/var/run/docker.sock:没有这样的主机
   
 
 

所以我在docker-compose中将docker.sock作为一个卷添加了,但是它不起作用 ?

  mygolangapp:
构建:./ mygolangapp 
卷:
-/var/run/docker.sock:/var/run/docker.sock
 <  / code>  
 
 

我的问题是:如何在Google容器注册表的golang应用程序中使用docker golang客户端。 我在这里做错了什么或想念什么? DOCKER_HOST应该是什么?

感谢您的帮助。 任何其他方法都非常受欢迎!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douluan8828 2018-11-30 23:44
    已采纳

    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

    点赞 打赏 评论

相关推荐 更多相似问题