Make building static binaries simpler#7022
Conversation
This commit adds STATIC makefile variable to adjust LDFLAGS and build tags accordingly. Fixes containerd#5824. Signed-off-by: Kazuyoshi Kato <[email protected]>
|
Skipping CI for Draft Pull Request. |
| EXTRA_LDFLAGS='-linkmode external -extldflags "-fno-PIC -static"' \ | ||
| BUILDTAGS="netgo osusergo static_build" | ||
| make STATIC=1 | ||
| ``` |
There was a problem hiding this comment.
Maybe better to clarify that this is valid only for 1.7 and later.
There was a problem hiding this comment.
Does the patch apply "cleanly" on the release/1.6 branch? (if so, that would make things a bit easier and I could already adjust my build-scripts)
There was a problem hiding this comment.
Yes. I can backport the patch to 1.6.
There was a problem hiding this comment.
That'd be nice yes; IIUC (looking at the change), both the "old" and "new" way would be supported, so it should not be a breaking change.
|
Just gave this a try with my original reproducer in #5824; docker build -t foo -<<'EOF'
ARG GO_VERSION=1.18.3
FROM golang:${GO_VERSION}-bullseye AS build
ENV GO111MODULE=off
RUN apt-get update && apt-get install -y --no-install-recommends libbtrfs-dev
RUN git clone https://github.com/kzys/containerd.git "/go/src/github.com/containerd/containerd"
WORKDIR /go/src/github.com/containerd/containerd
RUN git checkout -q "static"
RUN make STATIC=1 bin/ctr
RUN bin/ctr --version
RUN ldd bin/ctr || echo "statically linked?"
FROM alpine:3.16
COPY --from=build /go/src/github.com/containerd/containerd/bin/. /usr/local/bin/
RUN ldd /usr/local/bin/ctr || echo "statically linked?"
RUN /usr/local/bin/ctr --version
EOFdocker run --rm foo ctr --version
ctr github.com/containerd/containerd v1.3.0-5457-g00eb74a4c
docker run --rm foo sh -c 'ldd $(command -v ctr)'
/lib/ld-musl-x86_64.so.1: /usr/local/bin/ctr: Not a valid dynamic program |
This commit adds STATIC makefile variable to adjust LDFLAGS and build
tags accordingly.
Fixes #5824.
Signed-off-by: Kazuyoshi Kato [email protected]