简体   繁体   English

为什么我不能执行复制到容器中的二进制文件?

[英]Why can't I execute binary copied into a container?

I have a container built from base image alpine:3.11我有一个从基本图像alpine:3.11构建的容器

Now I have a binary my_bin that I copied into the running container.现在我有一个二进制my_bin我复制到正在运行的容器中。 From within the running container I moved to /usr/local/bin and I confirmed that the binary is there with the right permissions.从正在运行的容器中,我移至 /usr/local/bin 并确认该二进制文件具有正确的权限。 Eg例如

/ # ls -l /usr/local/bin/my_bin
-rwxr-xr-x    1 root     root      55662376 Jun 12 18:52 /usr/local/bin/my_bin

But when I attempt to execute/run this binary I get the following:但是当我尝试执行/运行这个二进制文件时,我得到以下信息:

/ # my_bin init
/bin/sh: my_bin: not found

This is also the case if I switch into /usr/local/bin/ and run via ./my_bin如果我切换到/usr/local/bin/并通过./my_bin运行,情况也是如此

also if I try using the full path如果我尝试使用完整路径

/# /usr/local/bin/my_bin init
/bin/sh: /usr/local/bin/my_bin: not found

Why am I seeing this behavior?为什么我会看到这种行为? and how do I get to be able to execute the binary?以及如何才能执行二进制文件?

EDIT 1 I installed file and I can also confirm that the binary is copied and is an executable编辑 1我安装了file ,我还可以确认二进制文件已复制并且是可执行文件

file /usr/local/bin/my_bin 
/usr/local/bin/my_bin: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=b36f0aad307c3229850d8db8c52e00033eae900c, for GNU/Linux 3.2.0, not stripped

Maybe this gives some extra clues?也许这提供了一些额外的线索?

Edit 2编辑 2

As suggested by @BMitch in the answer I also ran ldd and here is the output正如@BMitch 在答案中所建议的那样,我也运行了ldd ,这里是 output

# ldd /usr/local/bin/my_bin 
    /lib64/ld-linux-x86-64.so.2 (0x7f91a79f3000)
    libpthread.so.0 => /lib64/ld-linux-x86-64.so.2 (0x7f91a79f3000)
    libdl.so.2 => /lib64/ld-linux-x86-64.so.2 (0x7f91a79f3000)
    libc.so.6 => /lib64/ld-linux-x86-64.so.2 (0x7f91a79f3000)

** Edit 3 ** ** 编辑 3 **

Based on the output of ldd and more googling, I find that running apk add libc6-compat installed the missing libraries and I could then run the binary.基于ldd的 output 和更多谷歌搜索,我发现运行apk add libc6-compat安装了缺少的库,然后我可以运行二进制文件。

For a binary, this most likely indicates a missing dynamic library.对于二进制文件,这很可能表明缺少动态库。 You can run ldd /usr/local/bin/my_bin to see all the libraries that binary uses.您可以运行ldd /usr/local/bin/my_bin来查看二进制文件使用的所有库。 With alpine, the most common library to be missing from an externally compiled program is libc.对于 alpine,外部编译程序中最常见的库是 libc。 Alpine is built with musl instead of libc, and therefore you'll want to compile programs specifically for Alpine. Alpine 是用 musl 而不是 libc 构建的,因此您需要专门为 Alpine 编译程序。

For others that may encounter this error in docker containers, I cover various issues in my faq presentation and other questions on the site .对于可能在 docker 容器中遇到此错误的其他人,我在我的常见问题演示文稿网站其他问题中介绍了各种问题。

 / # my_bin init
/bin/sh: my_bin: not found

When you execute above line it says file which you are trying to execute can't be found, my_bin is the file in your case.当您执行上面的行时,它说找不到您尝试执行的文件, my_bin是您的情况下的文件。

Check if file is copied properly and with the same name or you might be trying to execute file from different location.检查文件是否正确复制并具有相同的名称,或者您可能正在尝试从不同的位置执行文件。

eg Try /usr/local/bin/my_bin init if you are not doing cd /usr/local/bin after ls -l /usr/local/bin/my_bin command.例如,如果您在ls -l /usr/local/bin/my_bin命令之后没有执行cd /usr/local/bin ,请尝试/usr/local/bin/my_bin init

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM