2 回答

TA貢獻1795條經驗 獲得超7個贊
您構建了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。與busybox.
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"]

TA貢獻1836條經驗 獲得超13個贊
長話短說
運行apt-get install musl
,然后/dlv
應該按預期工作。
解釋
按著這些次序:
docker run -it <image-name> sh
apt-get install file
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
(有關相關討論,請參閱此問題)是由 missing 引起的/lib/ld-musl-x86_64.so.1
。
因此,解決方案是musl
按照其文檔安裝庫。
我的回答是受這個答案的啟發。
- 2 回答
- 0 關注
- 241 瀏覽
添加回答
舉報