hboetes/mg

Containerfile to generate static builds

Closed this issue · 1 comments

Preamble: static binaries are great, but you can't really build them with glibc, to fix that problem I want to create a Containerfile, so I can simply build a static binary:

Here is that Containerfile:

FROM docker.io/library/alpine
RUN apk update
RUN apk add git libbsd-dev ncurses-dev musl-dev ncurses-static gcc make
RUN git clone https://github.com/hboetes/mg.git
WORKDIR mg
RUN TAG=$(git describe --tags); \
    git checkout $TAG; \
    make STATIC=1; \
    strip mg; \
    ln mg mg-$TAG-static-x86_64

You can create the static binary like this:

% sudo podman image build --rm -t mg-static .
[SNIP: lots of output]
% sudo podman image ls                       
REPOSITORY                TAG         IMAGE ID      CREATED         SIZE
localhost/mg-static       latest      93dc44a79f11  15 seconds ago  163 MB
docker.io/library/alpine  latest      14119a10abf4  3 weeks ago     5.87 MB
% sudo podman run -it 93dc44a79f11

To get the binary out, open another terminal and run:

% sudo podman ps                  
CONTAINER ID  IMAGE                       COMMAND     CREATED         STATUS             PORTS
f4e0aa32f16a  localhost/mg-static:latest  /bin/sh     34 seconds ago  Up 33 seconds ago
% sudo podman exec f4e0aa32f16a sh -c "ls /mg/mg-*"
/mg/mg-20210609-1-g0b69dcc-static-x86_64
% sudo podman cp f4e0aa32f16a:/mg/mg-20210609-1-g0b69dcc-static-x86_64 .

What works:

  • The binary is successfully created and can be copied out of the container:

What I don't like:

  • This is clumsy, can't I directly copy the binary out of the container with instructions in the Containerfile?
  • I need to run this as root for some reason or another. What am I missing here?

Let's ask this where it belongs.