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 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算