doushenxu7294 2018-11-28 21:30
浏览 231
已采纳

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!

  • 写回答

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败