Skip to content

Make building static binaries simpler#7022

Merged
kzys merged 1 commit intocontainerd:mainfrom
kzys:static
Jun 10, 2022
Merged

Make building static binaries simpler#7022
kzys merged 1 commit intocontainerd:mainfrom
kzys:static

Conversation

@kzys
Copy link
Member

@kzys kzys commented Jun 4, 2022

This commit adds STATIC makefile variable to adjust LDFLAGS and build
tags accordingly.

Fixes #5824.

Signed-off-by: Kazuyoshi Kato [email protected]

This commit adds STATIC makefile variable to adjust LDFLAGS and build
tags accordingly.

Fixes containerd#5824.

Signed-off-by: Kazuyoshi Kato <[email protected]>
@k8s-ci-robot
Copy link

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

@kzys kzys marked this pull request as ready for review June 4, 2022 03:36
EXTRA_LDFLAGS='-linkmode external -extldflags "-fno-PIC -static"' \
BUILDTAGS="netgo osusergo static_build"
make STATIC=1
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe better to clarify that this is valid only for 1.7 and later.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I can backport the patch to 1.6.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The backport PR is #7045.

@thaJeztah
Copy link
Member

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
EOF
docker 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

Copy link
Member

@thaJeztah thaJeztah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cherry-picked/1.6.x PR commits are cherry-picked into release/1.6 branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Statically compiling ctr fails on Go 1.15 and up (segfault or not statically linked)

5 participants