如何修复“找不到-lpq”使用Alpine Linux Docker图像时错误吗?

发布于 2025-01-25 10:47:47 字数 1522 浏览 2 评论 0原文

当我使用Alpine作为基本图像使用此命令在Docker中构建Rust应用程序时:

docker build -f ./Dockerfile -t="reddwarf-pro/reddwarf-admin:v.1.0.0" .

它显示了这样的错误:

#14 222.9   = note: /usr/lib/gcc/aarch64-alpine-linux-musl/10.3.1/../../../../aarch64-alpine-linux-musl/bin/ld: cannot find -lpq
#14 222.9           collect2: error: ld returned 1 exit status
#14 222.9
#14 222.9
#14 223.0 warning: `reddwarf-admin` (bin "reddwarf-admin") generated 1 warning
#14 223.0 error: could not compile `reddwarf-admin` due to previous error; 1 warning emitted
------
executor failed running [/bin/sh -c cargo build --release]: exit code: 101

这是Dockerfile:

# to reduce the docker image size
# build stage
FROM rust:1.54-alpine as builder
WORKDIR /app
COPY . /app
RUN rustup default stable
RUN apk update && apk add --no-cache libpq musl-dev pkgconfig openssl-dev gcc
RUN cargo build --release
# RUN cargo build

# Prod stage
FROM alpine:3.15
WORKDIR /app
ENV ROCKET_ADDRESS=0.0.0.0
# ENV ROCKET_PORT=11014
RUN apk update && apk add --no-cache libpq curl
COPY --from=builder /app/.env /app
COPY --from=builder /app/settings.toml /app
COPY --from=builder /app/target/release/reddwarf-admin /app/
COPY --from=builder /app/Rocket.toml /app
CMD ["./reddwarf-admin"]

我已经在Alpine Image中添加了libpq。为什么仍然显示此错误?我还尝试添加libpq-dev,但似乎高山没有包含此库。

我该怎么办来解决这个问题?

When I build my Rust application in Docker using alpine as the base image using this command:

docker build -f ./Dockerfile -t="reddwarf-pro/reddwarf-admin:v.1.0.0" .

It shows an error like this:

#14 222.9   = note: /usr/lib/gcc/aarch64-alpine-linux-musl/10.3.1/../../../../aarch64-alpine-linux-musl/bin/ld: cannot find -lpq
#14 222.9           collect2: error: ld returned 1 exit status
#14 222.9
#14 222.9
#14 223.0 warning: `reddwarf-admin` (bin "reddwarf-admin") generated 1 warning
#14 223.0 error: could not compile `reddwarf-admin` due to previous error; 1 warning emitted
------
executor failed running [/bin/sh -c cargo build --release]: exit code: 101

This is the Dockerfile:

# to reduce the docker image size
# build stage
FROM rust:1.54-alpine as builder
WORKDIR /app
COPY . /app
RUN rustup default stable
RUN apk update && apk add --no-cache libpq musl-dev pkgconfig openssl-dev gcc
RUN cargo build --release
# RUN cargo build

# Prod stage
FROM alpine:3.15
WORKDIR /app
ENV ROCKET_ADDRESS=0.0.0.0
# ENV ROCKET_PORT=11014
RUN apk update && apk add --no-cache libpq curl
COPY --from=builder /app/.env /app
COPY --from=builder /app/settings.toml /app
COPY --from=builder /app/target/release/reddwarf-admin /app/
COPY --from=builder /app/Rocket.toml /app
CMD ["./reddwarf-admin"]

I have already added libpq in the alpine image. Why does it still show this error? I am also tried to add libpq-dev but it seems the alpine did not contains this library.

What should I do to fix this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南风几经秋 2025-02-01 10:47:47

也许有些令人困惑的是,Dev版本称为PostgreSQL-DEV,而不是libpq-dev在v3.14中。 (据我所知,它在v3.15中重命名为libpq-dev)。

libpq确实确实安装了共享库,但是postgresql -dev创建符号链接/usr/lib/lib/libpq.so-so-> libpq.so.5.13使链接成功。

来自MAN LD

在支持共享库的系统上,LD也可能
搜索Libnamespec.a以外的文件。具体来说,开
精灵和Sunos Systems,LD将搜索目录
库称为libnamespec.so so搜索一个
称为libnamespec.a。 (按照惯例,“ .so”扩展
指示共享库。)请注意,此行为确实
不适用:文件名,始终指定一个名为的文件
文件名。

也就是说,-lname语法只会搜索libname。(a | so)而不是版本的名称。

Perhaps slightly confusingly, the dev version is called postgresql-dev and not libpq-dev in v3.14. (From what I can tell, it was renamed libpq-dev in v3.15).

libpq does indeed install the shared library, but postgresql-dev creates the symbolic link /usr/lib/libpq.so -> libpq.so.5.13 which makes linking succeed.

From man ld:

On systems which support shared libraries, ld may also
search for files other than libnamespec.a. Specifically, on
ELF and SunOS systems, ld will search a directory for a
library called libnamespec.so before searching for one
called libnamespec.a. (By convention, a ".so" extension
indicates a shared library.) Note that this behavior does
not apply to :filename, which always specifies a file called
filename.

That is, the -lname syntax will only search for libname.(a|so), not versioned names.

旧情别恋 2025-02-01 10:47:47

您需要的依赖性是:

libpq-dev

The dependency you need is:

libpq-dev

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文