Create a statically linked executable.
(I happened to do it in a docker container. You could use any method to create a statically linked executable, really)
Source:
// hello.c
#include <stdio.h>
int main() {
printf("Hello, world!\n");
return 0;
}
Dockerfile
// Dockerfile
FROM debian
RUN apt-get update
RUN apt-get -y install gcc
ADD hello.c /hello.c
RUN gcc -static -o /hello hello.c
extract the statically linked binary:
mkdir fs
docker build -t build-hello .
docker run build-hello cat /hello > fs/hello
chmod +x fs/hello
Package up the filesystem image and import it into docker:
$ ls fs
hello
$ tar -C fs -cvf hello.tar hello
a hello
$ docker import - hello < hello.tar
ead663cd606d8bd128657f1060dd0334d1631ab129b3e877dbe5d7f14a577ca1
Run the image:
$ docker run hello /hello
Hello, world!
For anyone wanting to use alpine (tiny linux distro), here's that Dockerfile.multistage modded to do so: