我是 docker 新手.我想容器一个小环境,这样我就可以运行可执行文件,但我被困住了,因为我甚至无法运行可执行文件.

我的文件夹 struct 如下所示:

example/
|-Dockerfile
|-hello_world

我的Dockerfile看起来是这样的:

# Use Alpine Linux as the base image
FROM alpine:latest

# Set the working directory inside the container
WORKDIR /app

# Copy the executable to the container
COPY hello_world /app/

# Set the permissions for the executable
RUN chmod +x /app/hello_world

# Define the command to run your server when the container starts
ENTRYPOINT ["/app/hello_world"]

然后我跑:

> sudo docker build -t example .

> sudo docker run --name example_container example

其结果是:

exec /app/hello_world: no such file or directory

我已经try 了尽可能多的变体,试图在Dockerfile中使用CMDRUNENTRYPOINT,但都有相同的结果,即在根目录下的app文件夹中找不到Hello_world程序.

我真的很困惑,因为我在我的香草Ubuntu操作系统上try 了这一点,我在根目录下放了一个测试文件夹,然后在那里放了一个hello_world,似乎可以很好地从任何地方使用这个绝对路径运行它.

/app/hello_world是一个可执行文件,它是一段编译过的Rust代码.当我在我的Ubuntu机器上运行/app/hello_world个shell 时,它运行得很好.

稳定-x86_64-未知-linux-gnu工具链/rustc 1.71.0

有人能告诉我我做错了什么吗?

推荐答案

您看到"没有这样的文件或目录"错误的原因是因为系统正在查找ELF二进制文件的.interp部分中嵌入的路径.对于在glibc下编译的二进制文件,它看起来如下所示:

$ objdump -j .interp -s hello

hello:     file format elf64-x86-64

Contents of section .interp:
 400318 2f6c6962 36342f6c 642d6c69 6e75782d  /lib64/ld-linux-
 400328 7838362d 36342e73 6f2e3200           x86-64.so.2.

在您的阿尔卑斯山图像中,没有/lib64/ld-linux-x86-64.so.2,这是导致错误消息的原因.


以一个C二进制文件为例,如果我开始:

#include <stdio.h>

int main() {
    printf("Hello world.\n");
    return 0;
}

并在我的glibc系统上编译它,然后try 在阿尔卑斯山下运行它,我们会看到:

$ podman run -it --rm -v $PWD:/src  -w /src alpine
/src # ./hello
/bin/sh: ./hello: not found

如果我们让预期的解释器可用,像这样:

$ podman run -it --rm -v $PWD:/src -w /src \
  -v /lib64/ld-linux-x86-64.so.2:/lib64/ld-linux-x86-64.so.2  alpine

我们得到一个新的错误:

/src # ./hello
./hello: error while loading shared libraries: libc.so.6: cannot open shared object file: No such file or directory

如果我们提供必要的共享库:

$ podman run -it --rm -v $PWD:/src -w /src \
  -v /lib64/ld-linux-x86-64.so.2:/lib64/ld-linux-x86-64.so.2 \
  -v /lib64/libc.so.6:/lib64/libc.so.6  alpine

然后,该命令将按预期工作:

/src # ./hello
Hello world.

Rust相关问答推荐

在actix—web中使用Redirect或NamedFile响应

如何正确地将App handler传递给Tauri中的其他模块?

如何正确重新排列代码以绕过铁 rust 借入判断器?

rust 蚀生命周期 不匹配-不一定超过此处定义的生命周期

我们能确定Rust会优化掉Clone()吗?如果它会立即掉落?

如何在嵌套的泛型 struct 中调用泛型方法?

Rust移动/复制涉及实际复制时进行检测

为什么rustc会自动降级其版本?

在 Rust 中用问号传播错误时对类型转换的困惑?

在使用粗粒度锁访问的数据 struct 中使用 RefCell 是否安全?

为什么 Rust 需要可变引用的显式生命周期而不是常规引用?

Rust 中的自动取消引用是如何工作的?

push 方法是否取得所有权?

打印 `format_args!` 时borrow 时临时值丢失

如何将参数传递给Rust 的线程?

为什么1..=100返回一个范围而不是一个整数?

在 Traits 函数中设置生命周期的问题

Iterator::collect如何进行转换?

为什么 `ref` 会导致此示例*取消引用*一个字段?

为什么在使用 self 时会消耗 struct 而在解构时不会?