Skip to content

Commit 46fbe58

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 168d446 commit 46fbe58

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
@@ -71,9 +71,6 @@ checkout: src
7171
./scripts/checkout.sh src/github.com/opencontainers/runc "$(RUNC_REF)"
7272
./scripts/checkout.sh src/github.com/containerd/containerd "$(REF)"
7373

74-
# NOTE: building static binaries currently only works when using an
75-
# ubuntu/debian BUILD_IMAGE, because build-dependencies are not
76-
# installed beforehand.
7774
.PHONY: static
7875
static: TARGET=binaries
7976
static: build

dockerfiles/rpm.dockerfile

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

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

4949
FROM ${BUILD_IMAGE} AS fedora-base
5050
RUN dnf install -y rpm-build git dnf-plugins-core
@@ -71,9 +71,11 @@ WORKDIR /root/rpmbuild
7171
COPY --from=go-md2man /go/bin/go-md2man /go/bin/go-md2man
7272
COPY rpm/containerd.spec SPECS/containerd.spec
7373
COPY scripts/build-rpm /root/
74+
COPY scripts/build-static /root/
7475
COPY scripts/.rpm-helpers /root/
7576
RUN . /root/.rpm-helpers \
76-
&& install_build_deps SPECS/containerd.spec
77+
&& install_build_deps SPECS/containerd.spec \
78+
&& install_package glibc-static
7779

7880
ARG PACKAGE
7981
ENV PACKAGE=${PACKAGE:-containerd.io}
@@ -122,6 +124,26 @@ FROM scratch AS packages
122124
COPY --from=build-packages /archive /archive
123125
COPY --from=verify-packages /build /build
124126

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