Skip to content

Commit e25718c

Browse files
committed
Add support for static builds on rpm-based distros
this patch allows building static binaries on rpm-based distros. Building is not succesfull on all distros, but works on most recent versions (CentOS 8, Oracle Linux 8, Fedora 30, 31) Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 1fe8066 commit e25718c

3 files changed

Lines changed: 51 additions & 5 deletions

File tree

Makefile

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,6 @@ checkout: src
7474
./scripts/checkout.sh src/github.com/containerd/containerd "$(REF)"
7575
./scripts/checkout.sh src/github.com/opencontainers/runc "$$(./scripts/determine-runc-version)"
7676

77-
# NOTE: building static binaries currently only works when using an
78-
# ubuntu/debian BUILD_IMAGE, because build-dependencies are not
79-
# installed beforehand.
8077
.PHONY: static
8178
static: TARGET=binaries
8279
static: build

dockerfiles/rpm.dockerfile

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ FROM redhat-base AS amzn-base
4545

4646
FROM redhat-base AS ol-base
4747
RUN . "/etc/os-release"; if [ "${VERSION_ID%.*}" -eq 7 ]; then yum-config-manager --enable ol7_addons --enable ol7_optional_latest; fi
48-
RUN . "/etc/os-release"; if [ "${VERSION_ID%.*}" -eq 8 ]; then yum-config-manager --enable ol8_addons; fi
48+
RUN . "/etc/os-release"; if [ "${VERSION_ID%.*}" -eq 8 ]; then yum-config-manager --enable ol8_addons --enable ol8_codeready_builder; fi
4949

5050
FROM ${BUILD_IMAGE} AS fedora-base
5151
RUN dnf install -y rpm-build git dnf-plugins-core
@@ -72,9 +72,11 @@ WORKDIR /root/rpmbuild
7272
COPY --from=go-md2man /go/bin/go-md2man /go/bin/go-md2man
7373
COPY rpm/containerd.spec SPECS/containerd.spec
7474
COPY scripts/build-rpm /root/
75+
COPY scripts/build-static /root/
7576
COPY scripts/.rpm-helpers /root/
7677
RUN . /root/.rpm-helpers \
77-
&& install_build_deps SPECS/containerd.spec
78+
&& install_build_deps SPECS/containerd.spec \
79+
&& install_package glibc-static
7880

7981
ARG PACKAGE
8082
ENV PACKAGE=${PACKAGE:-containerd.io}
@@ -123,6 +125,26 @@ FROM scratch AS packages
123125
COPY --from=build-packages /archive /archive
124126
COPY --from=verify-packages /build /build
125127

128+
FROM build-env AS build-binaries
129+
# NOTE: not using a cache-mount for /root/.cache/go-build, to prevent issues
130+
# with CGO when building multiple distros on the same machine / build-cache
131+
RUN --mount=type=bind,from=golang,source=/usr/local/go/,target=/usr/local/go/ \
132+
--mount=type=bind,source=/src,target=/go/src,rw \
133+
/root/build-static
134+
ARG UID=0
135+
ARG GID=0
136+
RUN chown -R ${UID}:${GID} /build
137+
138+
FROM distro-image AS verify-binaries
139+
COPY --from=build-binaries /build /build
140+
RUN tar -C /usr/local/bin/ --strip-components 1 -xzf "$(find /build/static -type f -name containerd.io*.tar.gz)"
141+
RUN containerd --version
142+
RUN ctr --version
143+
RUN runc --version
144+
145+
FROM scratch AS binaries
146+
COPY --from=verify-binaries /build /build
147+
126148
# This stage is mainly for debugging (running the build interactively with mounted source)
127149
FROM build-env AS runtime
128150
COPY --from=golang /usr/local/go/ /usr/local/go/

scripts/build-static

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,21 @@ ARCH=$(uname -m)
2828
DEST_DIR="/build/static/${ARCH}/"
2929
mkdir -p "${DEST_DIR}"
3030

31+
. "/etc/os-release"
32+
3133
# Build containerd
3234
(
3335
set -x
3436
export BUILDTAGS='netgo osusergo static_build seccomp apparmor selinux'
3537
export EXTRA_FLAGS='-buildmode=pie'
3638
export EXTRA_LDFLAGS='-extldflags "-fno-PIC -static"'
3739

40+
case "${ID}" in
41+
centos|ol|rhel)
42+
BUILDTAGS='netgo osusergo static_build apparmor selinux no_btrfs'
43+
;;
44+
esac
45+
3846
make -C "/go/src/github.com/containerd/containerd"
3947
make -C "/go/src/github.com/containerd/containerd" DESTDIR="${DEST_DIR}" install
4048
)
@@ -43,6 +51,25 @@ mkdir -p "${DEST_DIR}"
4351
(
4452
set -x
4553
RUNC_BUILDTAGS="seccomp apparmor selinux"
54+
55+
case "${ID}" in
56+
fedora)
57+
# seccomp requires the libseccomp-static package, which is available on
58+
# Fedora, but not on RHEL/CentOS
59+
#
60+
# /usr/local/go/pkg/tool/linux_amd64/link: running gcc failed: exit status 1
61+
# /usr/bin/ld: cannot find -lseccomp
62+
#
63+
# With LD_DEBUG=libs
64+
# go build github.com/opencontainers/runc/vendor/github.com/seccomp/libseccomp-golang: invalid flag in pkg-config --cflags: 1277:
65+
# make: Leaving directory '/go/src/github.com/opencontainers/runc'
66+
dnf -y install libseccomp-static
67+
;;
68+
centos|ol|rhel)
69+
RUNC_BUILDTAGS="apparmor selinux"
70+
;;
71+
esac
72+
4673
make -C "/go/src/github.com/opencontainers/runc" BUILDTAGS="${RUNC_BUILDTAGS}" static
4774
install -D -p -t "${DEST_DIR}/bin" "/go/src/github.com/opencontainers/runc/runc"
4875
)

0 commit comments

Comments
 (0)