gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 04:21:20 24 4
gpt4 key购买 nike

我有一个工作正常的 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

最佳答案

您在基于 alpine 的发行版中构建了 dlvdlv 可执行文件链接到 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"]

图片大小为25MB,其中18MB来自dlv,2MB来自Hello World应用。

在选择图像时应注意具有相同风格的 libcgolang:bullseye 链接到 glibc。因此,最小图像必须基于 glibc

但是如果你想要更舒适一点,使用安装了 gcompat 包的 alpine。与 busybox 相比,它是一个相当丰富的 linux,具有许多外部包,仅多出 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"]

关于Dockerfile 问题 - 为什么找不到二进制 dlv - 没有这样的文件或目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74250570/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com