rust-openssl:找不到 OpenSSL 安装目录
2022-01-03
17276
我试图在 docker 中编译 rust 二进制文件,但编译失败,提示未找到 openssl,尽管已安装。其他答案建议安装
pkg-config
和
libssl-dev
可以解决此问题,但它们已经安装。我相信这个问题与交叉编译有关,因为我能够在主机和目标相同时构建此映像。
Dockerfile:
FROM rust:1.57.0 AS build
WORKDIR /usr/src
RUN rustup target add x86_64-unknown-linux-musl
RUN USER=root cargo new project
WORKDIR /usr/src/project
COPY Cargo.toml Cargo.lock ./
RUN apt-get update \
&& apt-get upgrade \
&& apt-get install -y cmake pkg-config libssl-dev
RUN cargo build --release
错误:
#18 14.01 Compiling openssl-sys v0.9.72
#18 14.17 error: failed to run custom build command for `openssl-sys v0.9.72`
#18 14.18
#18 14.18 Caused by:
#18 14.18 process didn't exit successfully: `/usr/src/project/target/release/build/openssl-sys-64e55e6c4223d7aa/build-script-main` (exit status: 101)
#18 14.18 --- stdout
#18 14.18 cargo:rustc-cfg=const_fn
#18 14.18 cargo:rerun-if-env-changed=X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_LIB_DIR
#18 14.18 X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_LIB_DIR unset
#18 14.18 cargo:rerun-if-env-changed=OPENSSL_LIB_DIR
#18 14.18 OPENSSL_LIB_DIR unset
#18 14.18 cargo:rerun-if-env-changed=X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_INCLUDE_DIR
#18 14.18 X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_INCLUDE_DIR unset
#18 14.18 cargo:rerun-if-env-changed=OPENSSL_INCLUDE_DIR
#18 14.18 OPENSSL_INCLUDE_DIR unset
#18 14.18 cargo:rerun-if-env-changed=X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_DIR
#18 14.18 X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_DIR unset
#18 14.18 cargo:rerun-if-env-changed=OPENSSL_DIR
#18 14.18 OPENSSL_DIR unset
#18 14.18 cargo:rerun-if-env-changed=OPENSSL_NO_PKG_CONFIG
#18 14.18 cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS_x86_64-unknown-linux-musl
#18 14.18 cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS_x86_64_unknown_linux_musl
#18 14.18 cargo:rerun-if-env-changed=TARGET_PKG_CONFIG_ALLOW_CROSS
#18 14.18 cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS
#18 14.18 cargo:rerun-if-env-changed=PKG_CONFIG_x86_64-unknown-linux-musl
#18 14.18 cargo:rerun-if-env-changed=PKG_CONFIG_x86_64_unknown_linux_musl
#18 14.18 cargo:rerun-if-env-changed=TARGET_PKG_CONFIG
#18 14.18 cargo:rerun-if-env-changed=PKG_CONFIG
#18 14.18 cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64-unknown-linux-musl
#18 14.18 cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64_unknown_linux_musl
#18 14.18 cargo:rerun-if-env-changed=TARGET_PKG_CONFIG_SYSROOT_DIR
#18 14.18 cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR
#18 14.18 run pkg_config fail: "pkg-config has not been configured to support cross-compilation.\n\nInstall a sysroot for the target platform and configure it via\nPKG_CONFIG_SYSROOT_DIR and PKG_CONFIG_PATH, or install a\ncross-compiling wrapper for pkg-config and set it via\nPKG_CONFIG environment variable."
#18 14.18
#18 14.18 --- stderr
#18 14.18 thread 'main' panicked at '
#18 14.18
#18 14.18 Could not find directory of OpenSSL installation, and this `-sys` crate cannot
#18 14.18 proceed without this knowledge. If OpenSSL is installed and this crate had
#18 14.18 trouble finding it, you can set the `OPENSSL_DIR` environment variable for the
#18 14.18 compilation process.
#18 14.18
#18 14.18 Make sure you also have the development packages of openssl installed.
#18 14.18 For example, `libssl-dev` on Ubuntu or `openssl-devel` on Fedora.
#18 14.18
#18 14.18 If you're in a situation where you think the directory *should* be found
#18 14.18 automatically, please open a bug at https://github.com/sfackler/rust-openssl
#18 14.18 and include information about your system as well as this message.
#18 14.18
#18 14.18 $HOST = aarch64-unknown-linux-gnu
#18 14.18 $TARGET = x86_64-unknown-linux-musl
#18 14.18 openssl-sys = 0.9.72
#18 14.18
#18 14.18 ', /usr/local/cargo/registry/src/github.com-1ecc6299db9ec823/openssl-sys-0.9.72/build/find_normal.rs:180:5
2个回答
您说得对,该问题与交叉编译有关。您正在 Debian 容器上进行编译,并且它没有可以编译的 MUSL 库。
您有两个选项:
- 告诉 Rust openssl crate 编译供应商的 openssl。
- 在 Alpine 容器上进行编译。
选项 1:使用供应商的 openssl
假设我们从 reqwest 中的示例开始:
use std::collections::HashMap;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let resp = reqwest::get("https://httpbin.org/ip")
.await?
.json::<HashMap<String, String>>()
.await?;
println!("{:#?}", resp);
Ok(())
}
并且 Cargo.toml 具有依赖项:
[dependencies]
reqwest = { version = "0.11", features = ["json"] }
tokio = { version = "1", features = ["full"] }
我们从合理的默认 Dockerfile 开始:
FROM rust:latest as build
RUN rustup target add x86_64-unknown-linux-musl
RUN apt-get update && apt-get install -y musl-tools
COPY . /app
WORKDIR /app
RUN cargo build --target=x86_64-unknown-linux-musl --release
当我们使用
docker build .
构建它时,我们会得到与您相同的错误看见了。
解决方案是在 Cargo.toml 中的
[dependencies]
中添加以下行:
openssl = { version = "0.10", features = ["vendored"] }
这使
openssl
包为
x86_64-unknown-linux-musl
目标编译 C 库本身。
请注意,无需在构建容器中安装
libssl-dev
或
pkg-config
。
选项 2:使用 Alpine 容器构建
通过在 Alpine 容器上进行编译,您可以完全避免交叉编译问题。
使用 Cargo.toml 中的原始依赖项:
[dependencies]
reqwest = { version = "0.11", features = ["json"] }
tokio = { version = "1", features = ["full"] }
您可以使用以下 Dockerfile 进行构建:
FROM rust:1.68-alpine as builder
RUN apk add openssl-dev musl-dev
COPY . /app
WORKDIR /app
RUN cargo build --release
我们安装
openssl-dev
在 Alpine 中,这样 openssl 板条箱就不必从源代码编译库,并且我们安装
musl-dev
以便它可以链接到 C 运行时。
Grandpa
2023-03-24
您忘记了 sudo
sudo apt-get update
sudo apt-get install libssl-dev
aakash4dev
2023-09-20