0

I have a container built from base image alpine:3.11

Now I have a binary my_bin that I copied into the running container. From within the running container I moved to /usr/local/bin and I confirmed that the binary is there with the right permissions. 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

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

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

As suggested by @BMitch in the answer I also ran ldd and here is the 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 **

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.

Finlay Weber
  • 2,989
  • 3
  • 17
  • 37

2 Answers2

2

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. With alpine, the most common library to be missing from an externally compiled program is libc. Alpine is built with musl instead of libc, and therefore you'll want to compile programs specifically for 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.

BMitch
  • 231,797
  • 42
  • 475
  • 450
  • I pasted the output of ldd. Does that show anything missing? – Finlay Weber Jun 12 '20 at 17:33
  • 1
    @FinlayWeber you've got the container, we don't. Do the files listed on the right exist? – BMitch Jun 12 '20 at 17:34
  • I think we are on to something here. Running `file /lib64/ld-linux-x86-64.so.2` leads to `/lib64/ld-linux-x86-64.so.2: cannot open `/lib64/ld-linux-x86-64.so.2' (No such file or directory)` and this applies to all the other files listed on the right...the next question is, how do I make these files available? What "lib" do I need to install? I guess I should google for how to install `libc.so.6`, `libdl.so.2`, and `libpthread.so.0`? – Finlay Weber Jun 12 '20 at 17:39
  • As mentioned in my answer above, the best option is to compile it for Alpine, second best is to statically link the compiled binary, or switch to a non-Alpine base image, or worst case you can attempt to install libc inside Alpine, but that's ... less than ideal ... to say it politely. – BMitch Jun 12 '20 at 17:42
  • I got it fixed by running `apk add libc6-compat` thanks @BMitch for the pointer – Finlay Weber Jun 12 '20 at 17:46
0
 / # 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.

Check if file is copied properly and with the same name or you might be trying to execute file from different location.

e.g. 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.

  • That is the thing, I tried all of this. I can confirm the file exist, but when I try to execute (even using absolute path) it does not find the file! – Finlay Weber Jun 12 '20 at 17:25
  • what shebang you are using, alpine does not have bash installed so **`#!/bin/bash` will not work. you need to use `#!/bin/sh` in the first line.** – Rama Kant yadav Jun 12 '20 at 17:32