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

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

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格