dongqiabei7682 2019-05-30 17:52
浏览 252
已采纳

如何基于对/var/run/docker.sock的临时访问授予Docker容器

I would like to use the Docker socket on the host from Go code running inside a container based on scratch.

The Dockerfile looks something like this:


    FROM golang:1.12.4-alpine3.9 as builder

    RUN mkdir /user && \
        echo 'nobody:x:65534:65534:nobody:/:' > /user/passwd && \
        echo 'nobody:x:65534:' > /user/group

    RUN apk add --no-cache ca-certificates git

    WORKDIR /src

    COPY go.mod ./
    RUN go mod download

    COPY . .
    RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .

    FROM scratch as final

    COPY --from=builder /user/group /user/passwd /etc/
    COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
    COPY --from=builder /src/app /app
    COPY --chown=nobody:nobody data /.local

    USER nobody:nobody

    ENTRYPOINT ["/app"]

The docker service itself includes a mount for the /var/run/docker.sock

Output from docker service inspect:


  "Mounts": [
        {
            "Type": "bind",
            "Source": "/var/run/docker.sock",
            "Target": "/var/run/docker.sock"
        }
    ],

Things I've tried:

  1. touch /var/run/docker.sock on the builder and COPY --chown=nobody:nobody --from=builder /var/run /var/run in final

  2. Different user (I refuse to run as root. It's bad practice).

  3. Adding nobody in final to the docker group.

EDIT:

Under this configuration I get the following error as nobody as a user does not have permission to access /var/run/docker.socket

Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.25/services: dial unix /var/run/docker.sock: connect: permission denied

  • 写回答

1条回答 默认 最新

  • dongzheng4556 2019-05-30 21:18
    关注

    To communicate with the docker daemon you either need to run the command as root (or sudo), or your user must be a member of the docker group.

    In order to use it from a non-root user and without sudo, you will need to create the docker group inside the container and add your user to that group. NOTE: the docker group inside the container must have the same GID as the actual docker group on the host.

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

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题