Dockerfile 问题 - 为什么找不到二进制 dlv - 没有这样的文件或目录

问题描述 投票:0回答:3

我有一个运行良好的 docker 文件。然而,要远程调试它,我读到我需要在其上安装

dlv
,然后我需要运行 dlv 并传递我尝试调试的应用程序的参数。因此,在其上安装 dlv 并尝试运行它之后。我收到错误了

exec /dlv: no such file or directory

这是 docker 文件

FROM golang:1.18-alpine AS builder

# Build Delve for debugging
RUN go install github.com/go-delve/delve/cmd/dlv@latest

# Create and change to the app directory.
WORKDIR /app
ENV CGO_ENABLED=0


# Retrieve application dependencies.
COPY go.* ./
RUN go mod download

# Copy local code to the container image.
COPY . ./


# Build the binary.
RUN go build -gcflags="all=-N -l" -o fooapp

# Use the official Debian slim image for a lean production container.
FROM debian:buster-slim

EXPOSE 8000 40000

RUN set -x && apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y \
    ca-certificates && \
    rm -rf /var/lib/apt/lists/*

# Copy the binary to the production image from the builder stage.
#COPY --from=builder /app/fooapp /app/fooapp #commented this out  

COPY --from=builder /go/bin/dlv /dlv

# Run dlv as pass fooapp as parameter
CMD ["/dlv", "--listen=:40000", "--headless=true", "--api-version=2", "--accept-multiclient", "exec", "/app/fooapp"]

以上结果为

exec /dlv: no such file or directory

我不确定为什么会发生这种情况。作为 docker 的新手,我尝试了不同的方法来调试它。我尝试使用

dive
检查并查看图像在路径
dlv
中是否有
/dlv
,确实如此。我还附上了它的图片

enter image description here

docker go dockerfile delve
3个回答
4
投票

您在基于

dlv
的发行版中构建了
alpine
dlv
可执行文件链接到
libc.musl
:

# ldd dlv 
        linux-vdso.so.1 (0x00007ffcd251d000)
        libc.musl-x86_64.so.1 => not found

但随后您切换到基于

glibc
的图像
debian:buster-slim
。该图像没有所需的库。

# find / -name libc.musl*                                        
<nothing found>

这就是为什么你无法执行

dlv
- 动态链接器无法找到正确的库。

您需要构建基于

glibc
的 docker。例如,替换第一行

FROM golang:bullseye AS builder

顺便说一句。构建后,您需要在特权模式下运行容器

$ docker build . -t try-dlv
...
$ docker run --privileged --rm try-dlv
API server listening at: [::]:40000
2022-10-30T10:51:02Z warning layer=rpc Listening for remote connections (connections are not authenticated nor encrypted)

在非特权容器中

dlv
不允许生成子进程。

$ docker run --rm try-dlv
API server listening at: [::]:40000
2022-10-30T10:55:46Z warning layer=rpc Listening for remote connections (connections are not authenticated nor encrypted)
could not launch process: fork/exec /app/fooapp: operation not permitted

非常小的图像

您可以使用

debian:buster-slim
来最小化图像,它的大小为 80 MB。但如果您需要非常小的图像,请使用
busybox
,它的开销仅为 4.86 MB。

FROM golang:bullseye AS builder

# Build Delve for debugging
RUN go install github.com/go-delve/delve/cmd/dlv@latest

# Create and change to the app directory.
WORKDIR /app
ENV CGO_ENABLED=0

# Retrieve application dependencies.
COPY go.* ./
RUN go mod download

# Copy local code to the container image.
COPY . ./

# Build the binary.
RUN go build -o fooapp .

# Download certificates
RUN set -x && apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y \
    ca-certificates 

# Use the official Debian slim image for a lean production container.
FROM busybox:glibc

EXPOSE 8000 40000

# Copy the binary to the production image from the builder stage.
COPY --from=builder /app/fooapp /app/fooapp 
# COPY --from=builder /app/ /app

COPY --from=builder /go/bin/dlv /dlv

COPY --from=builder /etc/ssl /etc/ssl

# Run dlv as pass fooapp as parameter
CMD ["/dlv", "--listen=:40000", "--headless=true", "--api-version=2", "--accept-multiclient", "exec", "/app/fooapp"]
# ENTRYPOINT ["/bin/sh"]

图像大小为25 MB,其中18 MB来自

dlv
,2 MB来自
Hello World
应用程序。

在选择图像时,应注意具有相同的

libc
风格。
golang:bullseye
链接到
glibc
。因此,最小图像必须是基于
glibc
的。

但是如果您想要更舒适一点,请使用安装了

alpine
软件包的
gcompat
。这是一个相当丰富的 Linux,有很多外部软件包,与
busybox
相比,只需额外 6 MB。

FROM golang:bullseye AS builder

# Build Delve for debugging
RUN go install github.com/go-delve/delve/cmd/dlv@latest

# Create and change to the app directory.
WORKDIR /app
ENV CGO_ENABLED=0

# Copy local code to the container image.
COPY . ./

# Retrieve application dependencies.
RUN go mod tidy

# Build the binary.
RUN go build -o fooapp .

# Use alpine lean production container.
# FROM busybox:glibc
FROM alpine:latest

# gcompat is the package to glibc-based apps
# ca-certificates contains trusted TLS CA certs
# bash is just for the comfort, I hate /bin/sh
RUN apk add gcompat ca-certificates bash

EXPOSE 8000 40000

# Copy the binary to the production image from the builder stage.
COPY --from=builder /app/fooapp /app/fooapp 
# COPY --from=builder /app/ /app

COPY --from=builder /go/bin/dlv /dlv

# Run dlv as pass fooapp as parameter
CMD ["/dlv", "--listen=:40000", "--headless=true", "--api-version=2", "--accept-multiclient", "exec", "/app/fooapp"]
# ENTRYPOINT ["/bin/bash"]

2
投票

TL;博士

运行

apt-get install musl
,然后
/dlv
应该按预期工作。

说明

请按照以下步骤操作:

  1. docker run -it <image-name> sh
  2. apt-get install file
  3. file /dlv

然后你可以看到以下输出:

/dlv: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-musl-x86_64.so.1, Go BuildID=xV8RHgfpp-zlDlpElKQb/DOLzpvO_A6CJb7sj1Nxf/aCHlNjW4ruS1RXQUbuCC/JgrF83mgm55ntjRnBpHH, not stripped

令人困惑的

no such file or directory
(相关讨论请参阅这个问题)是由于缺少
/lib/ld-musl-x86_64.so.1
造成的。

因此,解决方案是按照

其文档
安装musl库。

我的答案受到这个答案的启发。


1
投票

no such file or directory
错误表示您的二进制文件不存在,或者您的二进制文件动态链接到不存在的库。

正如这个answer中所说,delve 与 libc.musl 链接。因此,对于您的 delve 构建,您可以禁用

CGO
,因为这可能会导致动态链接到 libc/libmusl:

# Build Delve for debugging
RUN CGO_ENABLED=0 go install github.com/go-delve/delve/cmd/dlv@latest
...

这甚至允许您稍后为最终目标映像使用临时构建,并且不需要您安装任何其他软件包(例如

musl
)或使用任何基于
glibc
的映像,并且要求您在特权模式下运行。

© www.soinside.com 2019 - 2024. All rights reserved.