Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build: use multi-stage to support new version and multi platform #74

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.git
Dockerfile
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*.rsa
*.pkg*
*.gz
src
pkg
packages
59 changes: 59 additions & 0 deletions APKBUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Maintainer: Sasha Gerrand <[email protected]>

pkgname="glibc"
pkgver="2.39"
pkgrel="1"
pkgdesc="GNU C Library compatibility layer"
arch="aarch64"
url="https://github.com/sgerrand/alpine-pkg-glibc"
license="LGPL"
source="./glibc-bin-$pkgver.tar.gz
ld.so.conf"
subpackages="$pkgname-bin $pkgname-dev $pkgname-i18n"
triggers="$pkgname-bin.trigger=/lib:/usr/lib:/usr/glibc-compat/lib"

package() {
mkdir -p "$pkgdir/lib" "$pkgdir/usr/glibc-compat/lib/locale" "$pkgdir"/usr/glibc-compat/lib64 "$pkgdir"/etc
cp -a "$srcdir"/usr "$pkgdir"
cp "$srcdir"/ld.so.conf "$pkgdir"/usr/glibc-compat/etc/ld.so.conf
rm "$pkgdir"/usr/glibc-compat/etc/rpc
rm -rf "$pkgdir"/usr/glibc-compat/bin
rm -rf "$pkgdir"/usr/glibc-compat/sbin
rm -rf "$pkgdir"/usr/glibc-compat/lib/gconv
rm -rf "$pkgdir"/usr/glibc-compat/lib/getconf
rm -rf "$pkgdir"/usr/glibc-compat/lib/audit
rm -rf "$pkgdir"/usr/glibc-compat/share
rm -rf "$pkgdir"/usr/glibc-compat/var

# set up symbolic links based on architecture
case "$arch" in
x86_64)
ln -s /usr/glibc-compat/lib/ld-linux-x86_64.so.2 ${pkgdir}/lib/ld-linux-x86_64.so.2
ln -s /usr/glibc-compat/lib/ld-linux-x86_64.so.2 ${pkgdir}/usr/glibc-compat/lib64/ld-linux-x86_64.so.2
;;
aarch64)
ln -s /usr/glibc-compat/lib/ld-linux-aarch64.so.1 ${pkgdir}/lib/ld-linux-aarch64.so.1
ln -s /usr/glibc-compat/lib/ld-linux-aarch64.so.1 ${pkgdir}/usr/glibc-compat/lib64/ld-linux-aarch64.so.1
;;
*)
echo "not support: $arch"
exit 1
;;
esac

ln -s /usr/glibc-compat/etc/ld.so.cache ${pkgdir}/etc/ld.so.cache
}

bin() {
depends="$pkgname bash libc6-compat libgcc"
mkdir -p "$subpkgdir"/usr/glibc-compat
cp -a "$srcdir"/usr/glibc-compat/bin "$subpkgdir"/usr/glibc-compat
cp -a "$srcdir"/usr/glibc-compat/sbin "$subpkgdir"/usr/glibc-compat
}

i18n() {
depends="$pkgname-bin"
arch="noarch"
mkdir -p "$subpkgdir"/usr/glibc-compat
cp -a "$srcdir"/usr/glibc-compat/share "$subpkgdir"/usr/glibc-compat
}
114 changes: 95 additions & 19 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM alpine:3.20
FROM alpine:3.20 AS remote-builder
ChaimHong marked this conversation as resolved.
Show resolved Hide resolved

ENV LANG=C.UTF-8

Expand All @@ -10,24 +10,24 @@ RUN ALPINE_GLIBC_BASE_URL="https://github.com/sgerrand/alpine-pkg-glibc/releases
ALPINE_GLIBC_I18N_PACKAGE_FILENAME="glibc-i18n-$ALPINE_GLIBC_PACKAGE_VERSION.apk" && \
apk add --no-cache --virtual=.build-dependencies wget ca-certificates && \
echo \
"-----BEGIN PUBLIC KEY-----\
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApZ2u1KJKUu/fW4A25y9m\
y70AGEa/J3Wi5ibNVGNn1gT1r0VfgeWd0pUybS4UmcHdiNzxJPgoWQhV2SSW1JYu\
tOqKZF5QSN6X937PTUpNBjUvLtTQ1ve1fp39uf/lEXPpFpOPL88LKnDBgbh7wkCp\
m2KzLVGChf83MS0ShL6G9EQIAUxLm99VpgRjwqTQ/KfzGtpke1wqws4au0Ab4qPY\
KXvMLSPLUp7cfulWvhmZSegr5AdhNw5KNizPqCJT8ZrGvgHypXyiFvvAH5YRtSsc\
Zvo9GI2e2MaZyo9/lvb+LbLEJZKEQckqRj4P26gmASrZEPStwc+yqy1ShHLA0j6m\
1QIDAQAB\
-----END PUBLIC KEY-----" | sed 's/ */\n/g' > "/etc/apk/keys/sgerrand.rsa.pub" && \
"-----BEGIN PUBLIC KEY-----\
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApZ2u1KJKUu/fW4A25y9m\
y70AGEa/J3Wi5ibNVGNn1gT1r0VfgeWd0pUybS4UmcHdiNzxJPgoWQhV2SSW1JYu\
tOqKZF5QSN6X937PTUpNBjUvLtTQ1ve1fp39uf/lEXPpFpOPL88LKnDBgbh7wkCp\
m2KzLVGChf83MS0ShL6G9EQIAUxLm99VpgRjwqTQ/KfzGtpke1wqws4au0Ab4qPY\
KXvMLSPLUp7cfulWvhmZSegr5AdhNw5KNizPqCJT8ZrGvgHypXyiFvvAH5YRtSsc\
Zvo9GI2e2MaZyo9/lvb+LbLEJZKEQckqRj4P26gmASrZEPStwc+yqy1ShHLA0j6m\
1QIDAQAB\
-----END PUBLIC KEY-----" | sed 's/ */\n/g' > "/etc/apk/keys/sgerrand.rsa.pub" && \
wget \
"$ALPINE_GLIBC_BASE_URL/$ALPINE_GLIBC_PACKAGE_VERSION/$ALPINE_GLIBC_BASE_PACKAGE_FILENAME" \
"$ALPINE_GLIBC_BASE_URL/$ALPINE_GLIBC_PACKAGE_VERSION/$ALPINE_GLIBC_BIN_PACKAGE_FILENAME" \
"$ALPINE_GLIBC_BASE_URL/$ALPINE_GLIBC_PACKAGE_VERSION/$ALPINE_GLIBC_I18N_PACKAGE_FILENAME" && \
"$ALPINE_GLIBC_BASE_URL/$ALPINE_GLIBC_PACKAGE_VERSION/$ALPINE_GLIBC_BASE_PACKAGE_FILENAME" \
"$ALPINE_GLIBC_BASE_URL/$ALPINE_GLIBC_PACKAGE_VERSION/$ALPINE_GLIBC_BIN_PACKAGE_FILENAME" \
"$ALPINE_GLIBC_BASE_URL/$ALPINE_GLIBC_PACKAGE_VERSION/$ALPINE_GLIBC_I18N_PACKAGE_FILENAME" && \
mv /etc/nsswitch.conf /etc/nsswitch.conf.bak && \
apk add --no-cache --force-overwrite \
"$ALPINE_GLIBC_BASE_PACKAGE_FILENAME" \
"$ALPINE_GLIBC_BIN_PACKAGE_FILENAME" \
"$ALPINE_GLIBC_I18N_PACKAGE_FILENAME" && \
"$ALPINE_GLIBC_BASE_PACKAGE_FILENAME" \
"$ALPINE_GLIBC_BIN_PACKAGE_FILENAME" \
"$ALPINE_GLIBC_I18N_PACKAGE_FILENAME" && \
\
mv /etc/nsswitch.conf.bak /etc/nsswitch.conf && \
rm "/etc/apk/keys/sgerrand.rsa.pub" && \
Expand All @@ -39,6 +39,82 @@ RUN ALPINE_GLIBC_BASE_URL="https://github.com/sgerrand/alpine-pkg-glibc/releases
rm "/root/.wget-hsts" && \
apk del .build-dependencies && \
rm \
"$ALPINE_GLIBC_BASE_PACKAGE_FILENAME" \
"$ALPINE_GLIBC_BIN_PACKAGE_FILENAME" \
"$ALPINE_GLIBC_I18N_PACKAGE_FILENAME"
"$ALPINE_GLIBC_BASE_PACKAGE_FILENAME" \
"$ALPINE_GLIBC_BIN_PACKAGE_FILENAME" \
"$ALPINE_GLIBC_I18N_PACKAGE_FILENAME"


# Stage 1: use docker-glibc-builder build glibc.tar.gz
FROM ubuntu:22.04 AS builder
LABEL maintainer="Sasha Gerrand <[email protected]>"
ARG GLIBC_VERSION=2.39
ENV DEBIAN_FRONTEND=noninteractive \
GLIBC_VERSION=${GLIBC_VERSION} \
PREFIX_DIR=/usr/glibc-compat
RUN apt-get -q update \
&& apt-get -qy install \
bison \
build-essential \
gawk \
gettext \
openssl \
python3 \
texinfo \
wget
COPY configparams /glibc-build/configparams
COPY builder /builder
RUN /builder

# Stage 2: use docker-alpine-abuild package apk and keys
FROM alpine:3.20 AS packager
ARG GLIBC_VERSION=2.39
ARG ALPINE_VERSION=3.20
ARG TARGETARCH
RUN apk --no-cache add alpine-sdk coreutils cmake sudo bash \
&& adduser -G abuild -g "Alpine Package Builder" -s /bin/ash -D builder \
&& echo "builder ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers \
&& mkdir /packages \
&& chown builder:abuild /packages
COPY ./abuilder /bin/
USER builder
ENV PACKAGER="[email protected]"
RUN abuild-keygen -na && sudo cp /home/builder/.abuild/${PACKAGER}-*.rsa.pub /etc/apk/keys/
WORKDIR /home/builder/package
COPY --from=builder /glibc-bin-${GLIBC_VERSION}.tar.gz /home/builder/package/
COPY . /home/builder/package/

ENV REPODEST=/packages
RUN case "$TARGETARCH" in \
amd64) export TARGET_ARCH="x86_64" ;; \
arm64) export TARGET_ARCH="aarch64" ;; \
*) echo "Unsupported architecture: $TARGETARCH" && exit 1 ;; \
esac && \
sed -i "s/^pkgver=.*/pkgver=${GLIBC_VERSION}/" APKBUILD && \
sed -i "s/^arch=.*$/arch=\"${TARGET_ARCH}\"/" APKBUILD && \
abuild checksum && abuilder -r && cp /packages/builder/${TARGET_ARCH}/*.apk /tmp/

# Stage 3: apk add apk, build alpine-glibc
FROM alpine:3.20
ARG GLIBC_VERSION=2.39
ARG TARGETARCH
ENV GLIBC_VERSION=${GLIBC_VERSION}
ENV PACKAGER="[email protected]"

RUN case "$TARGETARCH" in \
amd64) export LD_LINUX_PATH="/lib/ld-linux-x86_64.so.2" ;; \
arm64) export LD_LINUX_PATH="/lib/ld-linux-aarch64.so.1" ;; \
*) echo "Unsupported architecture: $TARGETARCH" && exit 1 ;; \
esac

# use the key used during the build process
COPY --from=packager /tmp/*.apk /tmp/
COPY --from=packager /home/builder/.abuild/${PACKAGER}-*.pub /etc/apk/keys/

# install glibc APK
RUN apk add --no-cache gcompat && rm -rf ${LD_LINUX_PATH} && \
apk add --no-cache --force-overwrite /tmp/glibc-${GLIBC_VERSION}-*.apk && \
apk add --no-cache /tmp/glibc-bin-${GLIBC_VERSION}-*.apk && \
apk add --no-cache /tmp/glibc-i18n-${GLIBC_VERSION}-*.apk && \
rm -rf /tmp/*.apk

CMD ["/bin/sh"]
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ Download size of this image is only:

[![](https://images.microbadger.com/badges/image/frolvlad/alpine-glibc.svg)](http://microbadger.com/images/frolvlad/alpine-glibc "Get your own image badge on microbadger.com")

Builder
-------------

```Shell
docker buildx build --platform linux/arm64,linux/amd64 --build-arg GLIBC_VERSION=2.39 -t alpine-glibc:2.39 .
```

Usage Example
-------------
Expand Down
24 changes: 24 additions & 0 deletions abuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env ash

set -e

main() {
RSA_FILE=$(ls /home/builder/.abuild/${PACKAGER}-*.rsa | head -n 1)
export PACKAGER_PRIVKEY="$RSA_FILE"

mkdir -p "$REPODEST" /home/builder/.abuild
abuild-apk update
[ "$RSA_PRIVATE_KEY" ] && {
echo -e "$RSA_PRIVATE_KEY" >"/home/builder/.abuild/$RSA_PRIVATE_KEY_NAME"
export PACKAGER_PRIVKEY="/home/builder/.abuild/$RSA_PRIVATE_KEY_NAME"
}
ls -lh /home/builder/.abuild
echo "PACKAGE_PRIVKEY ${PACKAGER_PRIVKEY}"
sudo chown -R builder:abuild /home/builder/package
sudo chown -R builder:abuild $REPODEST

# exec abuild -r clean
exec abuild "$@"
}

main "$@"
28 changes: 28 additions & 0 deletions builder
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env bash

set -eo pipefail
[[ "$TRACE" ]] && set -x

main() {
declare version="${1:-$GLIBC_VERSION}" prefix="${2:-$PREFIX_DIR}"

: "${version:?}" "${prefix:?}"

{
wget -qO- "https://ftpmirror.gnu.org/libc/glibc-$version.tar.gz" |
tar zxf -
mkdir -p /glibc-build && cd /glibc-build
"/glibc-$version/configure" \
--prefix="$prefix" \
--libdir="$prefix/lib" \
--libexecdir="$prefix/lib" \
--enable-multi-arch \
--enable-stack-protector=strong
make --jobs=4 && make install
tar --dereference --hard-dereference -zcf "/glibc-bin-$version.tar.gz" "$prefix"
} >&2

# [[ $STDOUT ]] && cat "/glibc-bin-$version.tar.gz"
}

main "$@"
5 changes: 5 additions & 0 deletions configparams
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
slibdir=/usr/glibc-compat/lib
rtlddir=/usr/glibc-compat/lib
sbindir=/usr/glibc-compat/bin
rootsbindir=/usr/glibc-compat/bin
build-programs=yes
2 changes: 2 additions & 0 deletions glibc-bin.trigger
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
/usr/glibc-compat/sbin/ldconfig
6 changes: 6 additions & 0 deletions ld.so.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# libc default configuration
/usr/local/lib

/usr/glibc-compat/lib
/usr/lib
/lib