Skip to content

Commit de7879f

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 34a954e commit de7879f

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
@@ -65,9 +65,6 @@ checkout: src
6565
@git -C src/github.com/opencontainers/runc checkout -q "$(RUNC_REF)"
6666
@git -C src/github.com/containerd/containerd checkout -q "$(REF)"
6767

68-
# NOTE: building static binaries currently only works when using an
69-
# ubuntu/debian BUILD_IMAGE, because build-dependencies are not
70-
# installed beforehand.
7168
.PHONY: static
7269
static: TARGET=binaries
7370
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
@@ -70,9 +70,11 @@ WORKDIR /root/rpmbuild
7070
COPY --from=go-md2man /go/bin/go-md2man /go/bin/go-md2man
7171
COPY rpm/containerd.spec SPECS/containerd.spec
7272
COPY scripts/build-rpm /root/
73+
COPY scripts/build-static /root/
7374
COPY scripts/.rpm-helpers /root/
7475
RUN . /root/.rpm-helpers \
75-
&& install_build_deps SPECS/containerd.spec
76+
&& install_build_deps SPECS/containerd.spec \
77+
&& install_package glibc-static
7678

7779
ARG PACKAGE
7880
ENV PACKAGE=${PACKAGE:-containerd.io}
@@ -121,6 +123,26 @@ FROM scratch AS packages
121123
COPY --from=build-packages /archive /archive
122124
COPY --from=verify-packages /build /build
123125

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