2

I compiled a simple hello world C program inside of an alpine linux container and copied it onto my ubuntu host. To my surprise, I am not able to run the binary on my ubuntu host. Instead, I get the following error when I try to execute the program.

$ /bin/bash ./hello
$ ./hello: ./hello: cannot execute binary file

Why can't I run my program that was compiled on alpine on ubuntu?

UPDATE I made the mistake of trying to run the C program as a bash script. However, after correcting my mistake, I still get an error...

$ ./hello
$ bash: ./hello: No such file or directory

I have made the binary executable, so that should not be the issue. This error message does not show up when running the same program compiled on the host.

UPDATE Let me clarify a few points.

  • One executable was built in a ubuntu container and copied onto the ubuntu host. This executable works.
  • The other executable was built using an alpine container and copied onto the ubuntu host. This executable does not work.
  • Both executables have their executable bit set. When I run ./hello, I do it within the directory where the executable in question resides.
  • When I run file on the executable compiled using the ubuntu container, I get the following output.
hello: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/l, for GNU/Linux 3.2.0, BuildID[sha1]=bc9e3e2ecfc026f8077dca28dbbdee4778862d7a, not stripped
  • When I run ldd on the executable compiled using the ubuntu container I get the following output.
linux-vdso.so.1 (0x00007ffdbc79e000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f517b5ae000)
/lib64/ld-linux-x86-64.so.2 (0x00007f517bba1000)
  • When I run file on the executable compiled using the alpine container, I get the following output.
hello: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-, with debug_info, not stripped
  • When I run ldd on the executable compiled using the alpine container, I get the following output.
linux-vdso.so.1 (0x00007ffec27f7000)
libc.musl-x86_64.so.1 => not found
Tyler K
  • 110
  • 1
  • 11
  • Why bash? It is not a bash script, it is a binary executable. – n. m. could be an AI Dec 15 '19 at 07:07
  • I did not realize that using bash vs the normal "./" syntax made a difference; however, I still get the following error when using the proper syntax: "bash: ./hello: No such file or directory". – Tyler K Dec 15 '19 at 07:16
  • 2
    What does `file hello` say? And `ldd hello`? – tink Dec 15 '19 at 08:01
  • 1
    I don't mess with containers, but I suspect the container libraries that support "hello" within the container are incompatible with the libraries and C-runtine available outside of the container. – David C. Rankin Dec 15 '19 at 08:39
  • I updated my question to include the output of the file and ldd commands. The ldd output for the alpine container does seem to suggest that there is a linking error. Would this be due to a difference in runtime libraries between distributions? – Tyler K Dec 17 '19 at 14:09

1 Answers1

10

Alpine distribution uses musl as the standard C library implementation.

Ubuntu (and 99% of all the others linux) distributions use glibc as their standard C library implementation.

They are incompatible with themselves. By "incompatible" I mean that a binary compiled under alpine is linked against the musl library, not against glibc.

There are two solutions. You can install musl library in ubuntu so that the binary can link against it. You can install glibc in alpine and compile the binary in alpine while linking it against glibc. Or you can compile your binary statically against musl.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • 1
    This is not about shared libraries but about the *interpreter* a.k.a. dynamic loader. Alpine and Ubuntu use different interpreters. See the output of the `file` command. The program doesn't even come to the stage of loading shared libraries because there is no correct loader that would load them. – n. m. could be an AI Dec 17 '19 at 14:20
  • Should this answer be updated then? – Tyler K Dec 17 '19 at 14:25