使用 rust 二进制文件运行 docker 映像时出现权限被拒绝错误

Posted

技术标签:

【中文标题】使用 rust 二进制文件运行 docker 映像时出现权限被拒绝错误【英文标题】:Permission denied error when running docker image with rust binary 【发布时间】:2022-01-17 00:48:12 【问题描述】:

尝试通过 docker 容器运行小型 rust cli 应用程序时,我的权限被拒绝。我可以很好地构建图像,但是当我尝试运行它时,我得到:

docker: Error response from daemon: OCI runtime create failed: 
container_linux.go:380: 
starting container process caused: exec: "./async-scraper": permission denied: 
unknown.

Dockerfile

FROM clux/muslrust:1.56.1-stable as builder
WORKDIR /volume
COPY . .
RUN cargo build --release

FROM alpine
COPY --from=builder /volume/target/x86_64-unknown-linux-musl/release ./async-scraper
RUN chmod +x ./async-scraper
ENTRYPOINT [ "./async-scraper","$@"]

运行命令

sudo docker run --rm -it paul-k/web_scraper:latest - 
https://example-url-arg/

Cargo.toml

[package]
name = "async_web_scraper"
version = "0.1.0"
edition = "2018"

[[bin]]
name = "rat"
path = "src/main.rs"

[lib]
name = "async_scraper"

[dependencies]
select = "0.6.0-alpha.1"
reqwest =  version = "0.11.6", features = ["json"] 
tokio =  version = "1", features = ["full"] 
futures = "0.3.1"
lazy_static = "1.4.0"
url = "2.2.2"
error-chain = "0.12.4"
async-trait = "0.1.51"
simple-error = "0.1.9"
anyhow = "1.0"
log = "0.4.14"
env_logger = "0.9.0"
thiserror = "1.0.30"

[dev-dependencies]
httpmock = "0.6"
tokio-test = "*"

【问题讨论】:

【参考方案1】:

您正在尝试执行目录"/volume/target/x86_64-unknown-linux-musl/release"。我建议你使用cargo install:

RUN cargo install --path . --root /volume

# ...

COPY --from=builder /volume/bin/rat /volume/rat
WORKDIR /volume
ENTRYPOINT [ "./rat","$@"]

【讨论】:

"/volume/target/x86_64-unknown-linux-musl/release/ 有我的可执行文件。我确实尝试了你所说的,但它出错了Step 6/8 : COPY --from=builder /volume/bin/async-scraper /volume/async-scraper COPY failed: stat volume/bin/async-scraper: file does not exist @godhar 我假设 async-scraper 是您的二进制名称,请在您的问题中提供您的 cargo.toml 我添加了 cargo.toml。 感谢您的更新,我是生锈的新手。事实上,我的二进制文件名为rat。但是,在运行这个新脚本之后,如果我在构建阶段之后检查/volume/bin/rat,它是空的。要确认,请使用exec: "./rat": stat ./rat: no such file or directory: unknown. 运行图像错误 @godhar 确保你的工作目录是/volume

以上是关于使用 rust 二进制文件运行 docker 映像时出现权限被拒绝错误的主要内容,如果未能解决你的问题,请参考以下文章

如何在我的 docker ubuntu 映像中获取 nvidia 驱动程序?

如何安装在 windowsservercore Docker 映像上执行本机 C++ DLL/EXE 所需的所有依赖二进制文件

用于 php 的 docker 映像无法在 Windows 10 的浏览器上运行

Grafana SigV4 身份验证 - Docker 映像?

使用 docker-compose 将 Docker 映像部署到谷歌云运行

docker+k8s简介