From be9078f5afdc0a99b772c1c00a4b89c78abd4d6f Mon Sep 17 00:00:00 2001 From: Guofang Date: Fri, 6 Nov 2026 11:23:25 +0800 Subject: [PATCH] Update Dockerfile and build script to support custom installation prefix --- README.md | 7 ++++++ rocky9/Dockerfile | 61 +++++++++++++++++++++++++++++------------------ rocky9/build.sh | 6 +++-- 3 files changed, 49 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 650c52e..8eaac8c 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,13 @@ Use environment variables to select the container engine or override image names CONTAINER_ENGINE=podman IMAGE_NAME=my-build-env ./rocky9/build.sh ``` +The Debian 13 and Rocky Linux 9 images install every library under `/usr/local` by default. Set `PREFIX` to install them elsewhere; the build environment variables (`PATH`, `PKG_CONFIG_PATH`, `CMAKE_PREFIX_PATH`, `CMAKE_INSTALL_PREFIX`, and so on) follow it. The Alpine image does not support `PREFIX`. + +```sh +PREFIX=/opt/stack ./debian13/build.sh +PREFIX=/opt/stack ./rocky9/build.sh +``` + ## Run ```sh diff --git a/rocky9/Dockerfile b/rocky9/Dockerfile index 321e652..b23417f 100644 --- a/rocky9/Dockerfile +++ b/rocky9/Dockerfile @@ -37,17 +37,32 @@ RUN dnf install --assumeyes --enablerepo=crb \ RUN git config --global url."https://wangguofang.net/github.com/".insteadOf "https://github.com/" +# Install prefix of every dependency. Libraries are installed under ${PREFIX}/lib64. +ARG PREFIX=/usr/local + +# Make the compiler, CMake, pkg-config and the shell find earlier dependencies under a prefix other +# than the default /usr/local. CMake (>= 3.29) takes its default install prefix from the environment. +ENV PATH=${PREFIX}/bin:${PATH} \ + C_INCLUDE_PATH=${PREFIX}/include \ + CPLUS_INCLUDE_PATH=${PREFIX}/include \ + LIBRARY_PATH=${PREFIX}/lib64:${PREFIX}/lib \ + PKG_CONFIG_PATH=${PREFIX}/lib64/pkgconfig:${PREFIX}/lib/pkgconfig:${PREFIX}/share/pkgconfig \ + CMAKE_PREFIX_PATH=${PREFIX} \ + CMAKE_INSTALL_PREFIX=${PREFIX} + +RUN test "$(cmake --version | sed -n '1s/[^0-9]*//p' | cut -d. -f2)" -ge 29 + RUN --mount=type=bind,from=sources,source=rust,target=/tmp/rust \ mkdir rust-src \ && tar -xJf "/tmp/rust/rust-1.99.1-$(uname -m)-unknown-linux-gnu.tar.xz" -C rust-src --strip-components=1 \ - && rust-src/install.sh --prefix=/usr/local --without=rust-docs \ + && rust-src/install.sh --prefix=${PREFIX} --without=rust-docs \ && rm -rf rust-src RUN --mount=type=bind,from=sources,source=boost_1_83_0.tar.bz2,target=/tmp/boost.tar.bz2 \ mkdir src \ && tar --extract --bzip2 --file=/tmp/boost.tar.bz2 --directory=src --strip-components=1 \ && cd src \ - && ./bootstrap.sh --prefix=/usr/local --libdir=/usr/local/lib64 \ + && ./bootstrap.sh --prefix=${PREFIX} --libdir=${PREFIX}/lib64 \ && ./b2 --jobs="$(nproc)" \ --with-atomic \ --with-chrono \ @@ -73,7 +88,7 @@ RUN --mount=type=bind,from=sources,source=openssl-openssl-3.0.15.tar.gz,target=/ mkdir src \ && tar --extract --gzip --file=/tmp/openssl.tar.gz --directory=src --strip-components=1 \ && cd src \ - && ./Configure --prefix=/usr/local no-shared no-tests -fPIC \ + && ./Configure --prefix=${PREFIX} no-shared no-tests -fPIC \ && make --jobs="$(nproc)" \ && make install_sw \ && cd .. \ @@ -111,7 +126,7 @@ RUN --mount=type=bind,from=sources,source=zlib-v1.3.1.tar.gz,target=/tmp/zlib.ta mkdir src \ && tar --extract --gzip --file=/tmp/zlib.tar.gz --directory=src --strip-components=1 \ && cd src \ - && CFLAGS=-fPIC ./configure --prefix=/usr/local --libdir=/usr/local/lib64 --static \ + && CFLAGS=-fPIC ./configure --prefix=${PREFIX} --libdir=${PREFIX}/lib64 --static \ && make --jobs="$(nproc)" \ && make install \ && cd .. \ @@ -204,7 +219,7 @@ RUN --mount=type=bind,from=sources,source=gflags-v2.3.1.tar.gz,target=/tmp/gflag -DINSTALL_HEADERS=ON \ -DINSTALL_SHARED_LIBS=OFF \ -DINSTALL_STATIC_LIBS=ON \ - -DLIBRARY_INSTALL_DIR=/usr/local/lib64 \ + -DLIBRARY_INSTALL_DIR=${PREFIX}/lib64 \ -DREGISTER_INSTALL_PREFIX=OFF \ && cmake --build build --parallel "$(nproc)" --target install \ && rm -rf build src @@ -227,7 +242,7 @@ RUN --mount=type=bind,from=sources,source=liburing-liburing-2.15.tar.gz,target=/ mkdir src \ && tar --extract --gzip --file=/tmp/liburing.tar.gz --directory=src --strip-components=1 \ && cd src \ - && CFLAGS=-fPIC ./configure --prefix=/usr/local --libdir=/usr/local/lib64 --libdevdir=/usr/local/lib64 \ + && CFLAGS=-fPIC ./configure --prefix=${PREFIX} --libdir=${PREFIX}/lib64 --libdevdir=${PREFIX}/lib64 \ && make --jobs="$(nproc)" ENABLE_SHARED=0 library \ && make ENABLE_SHARED=0 install \ && cd .. \ @@ -237,7 +252,7 @@ RUN --mount=type=bind,from=sources,source=libaio_0.3.113.orig.tar.gz,target=/tmp mkdir src \ && tar --extract --gzip --file=/tmp/libaio.tar.gz --directory=src --strip-components=1 \ && make --directory=src --jobs="$(nproc)" ENABLE_SHARED=0 \ - && make --directory=src ENABLE_SHARED=0 prefix=/usr/local libdir=/usr/local/lib64 install \ + && make --directory=src ENABLE_SHARED=0 prefix=${PREFIX} libdir=${PREFIX}/lib64 install \ && rm -rf src RUN --mount=type=bind,from=sources,source=libdwarf-code-v2.3.2.tar.gz,target=/tmp/libdwarf-code.tar.gz \ @@ -260,7 +275,7 @@ RUN --mount=type=bind,from=sources,source=libsodium-1.0.20-RELEASE.tar.gz,target && tar --extract --gzip --file=/tmp/libsodium.tar.gz --directory=src --strip-components=1 \ && cd src \ && ./autogen.sh \ - && ./configure --prefix=/usr/local --libdir=/usr/local/lib64 --disable-shared --enable-static --with-pic \ + && ./configure --prefix=${PREFIX} --libdir=${PREFIX}/lib64 --disable-shared --enable-static --with-pic \ && make --jobs="$(nproc)" \ && make install \ && cd .. \ @@ -294,7 +309,7 @@ RUN --mount=type=bind,from=sources,source=numactl-v2.0.19.tar.gz,target=/tmp/num && tar --extract --gzip --file=/tmp/numactl.tar.gz --directory=src --strip-components=1 \ && cd src \ && ./autogen.sh \ - && CFLAGS=-fPIC ./configure --prefix=/usr/local --libdir=/usr/local/lib64 --disable-shared --enable-static \ + && CFLAGS=-fPIC ./configure --prefix=${PREFIX} --libdir=${PREFIX}/lib64 --disable-shared --enable-static \ && make --jobs="$(nproc)" \ && make install \ && cd .. \ @@ -397,7 +412,7 @@ RUN --mount=type=bind,from=sources,source=util-linux-2.42.2.tar.xz,target=/tmp/u mkdir src \ && tar --extract --xz --file=/tmp/util-linux.tar.xz --directory=src --strip-components=1 \ && cd src \ - && CFLAGS=-fPIC ./configure --prefix=/usr/local --libdir=/usr/local/lib64 \ + && CFLAGS=-fPIC ./configure --prefix=${PREFIX} --libdir=${PREFIX}/lib64 \ --disable-all-programs \ --disable-bash-completion \ --disable-makeinstall-chown \ @@ -497,7 +512,7 @@ RUN --mount=type=bind,from=sources,source=rapidjson-v1.1.0.tar.gz,target=/tmp/ra && tar --extract --gzip --file=/tmp/rapidjson.tar.gz --directory=src --strip-components=1 \ && git -C src apply /tmp/rapidjson-generic-string-ref-assignment.patch \ && cmake -G Ninja -S src -B build -DCMAKE_BUILD_TYPE=Release \ - -DLIB_INSTALL_DIR=/usr/local/lib64 \ + -DLIB_INSTALL_DIR=${PREFIX}/lib64 \ -DRAPIDJSON_BUILD_DOC=OFF \ -DRAPIDJSON_BUILD_EXAMPLES=OFF \ -DRAPIDJSON_BUILD_TESTS=OFF \ @@ -548,7 +563,7 @@ RUN --mount=type=bind,from=sources,source=gmp-6.3.0.tar.xz,target=/tmp/gmp.tar.x mkdir src \ && tar --extract --xz --file=/tmp/gmp.tar.xz --directory=src --strip-components=1 \ && cd src \ - && CFLAGS=-fPIC ./configure --prefix=/usr/local --libdir=/usr/local/lib64 --disable-shared --enable-static --enable-fat \ + && CFLAGS=-fPIC ./configure --prefix=${PREFIX} --libdir=${PREFIX}/lib64 --disable-shared --enable-static --enable-fat \ && make --jobs="$(nproc)" \ && make install \ && cd .. \ @@ -558,7 +573,7 @@ RUN --mount=type=bind,from=sources,source=nettle-3.10.2.tar.gz,target=/tmp/nettl mkdir src \ && tar --extract --gzip --file=/tmp/nettle.tar.gz --directory=src --strip-components=1 \ && cd src \ - && CFLAGS=-fPIC ./configure --prefix=/usr/local --libdir=/usr/local/lib64 \ + && CFLAGS=-fPIC ./configure --prefix=${PREFIX} --libdir=${PREFIX}/lib64 \ --disable-documentation \ --disable-shared \ --enable-static \ @@ -571,7 +586,7 @@ RUN --mount=type=bind,from=sources,source=gnutls-3.8.13.tar.xz,target=/tmp/gnutl mkdir src \ && tar --extract --xz --file=/tmp/gnutls.tar.xz --directory=src --strip-components=1 \ && cd src \ - && CFLAGS=-fPIC ./configure --prefix=/usr/local --libdir=/usr/local/lib64 \ + && CFLAGS=-fPIC ./configure --prefix=${PREFIX} --libdir=${PREFIX}/lib64 \ --with-included-libtasn1 \ --with-included-unistring \ --disable-cxx \ @@ -610,7 +625,7 @@ RUN --mount=type=bind,from=sources,source=libyaml-0.2.5.tar.gz,target=/tmp/libya && tar --extract --gzip --file=/tmp/libyaml.tar.gz --directory=src --strip-components=1 \ && cd src \ && ./bootstrap \ - && CFLAGS=-fPIC ./configure --prefix=/usr/local --libdir=/usr/local/lib64 --disable-shared --enable-static \ + && CFLAGS=-fPIC ./configure --prefix=${PREFIX} --libdir=${PREFIX}/lib64 --disable-shared --enable-static \ && make --jobs="$(nproc)" \ && make install \ && cd .. \ @@ -648,7 +663,7 @@ RUN --mount=type=bind,from=sources,source=libunwind-v1.8.3.tar.gz,target=/tmp/li && tar --extract --gzip --file=/tmp/libunwind.tar.gz --directory=src --strip-components=1 \ && cd src \ && autoreconf --install \ - && CFLAGS=-fPIC ./configure --prefix=/usr/local --libdir=/usr/local/lib64 \ + && CFLAGS=-fPIC ./configure --prefix=${PREFIX} --libdir=${PREFIX}/lib64 \ --disable-shared \ --enable-static \ --disable-tests \ @@ -665,7 +680,7 @@ RUN --mount=type=bind,from=sources,source=jemalloc-5.3.1.tar.gz,target=/tmp/jema && tar --extract --gzip --file=/tmp/jemalloc.tar.gz --directory=src --strip-components=1 \ && cd src \ && ./autogen.sh \ - && CFLAGS=-fPIC CXXFLAGS=-fPIC ./configure --prefix=/usr/local --libdir=/usr/local/lib64 \ + && CFLAGS=-fPIC CXXFLAGS=-fPIC ./configure --prefix=${PREFIX} --libdir=${PREFIX}/lib64 \ --disable-doc \ --disable-shared \ --enable-static \ @@ -680,8 +695,8 @@ RUN --mount=type=bind,from=sources,source=libproxyprotocol-v1.3.3.tar.gz,target= mkdir src \ && tar --extract --gzip --file=/tmp/libproxyprotocol.tar.gz --directory=src --strip-components=1 \ && gcc -fPIC -c -o src/src/proxy_protocol.o src/src/proxy_protocol.c \ - && ar rcs /usr/local/lib64/libproxyprotocol.a src/src/proxy_protocol.o \ - && cp src/src/proxy_protocol.h /usr/local/include/ \ + && ar rcs ${PREFIX}/lib64/libproxyprotocol.a src/src/proxy_protocol.o \ + && cp src/src/proxy_protocol.h ${PREFIX}/include/ \ && rm -rf src RUN --mount=type=bind,from=sources,source=xsimd-14.2.0.tar.gz,target=/tmp/xsimd.tar.gz \ @@ -701,10 +716,10 @@ RUN --mount=type=bind,from=sources,source=thrift-v0.22.0.tar.gz,target=/tmp/thri -DBUILD_SHARED_LIBS=OFF \ -DBUILD_TESTING=OFF \ -DBUILD_TUTORIALS=OFF \ - -DCMAKE_INSTALL_DIR=/usr/local/lib64/cmake \ + -DCMAKE_INSTALL_DIR=${PREFIX}/lib64/cmake \ -DCMAKE_POSITION_INDEPENDENT_CODE=ON \ - -DLIB_INSTALL_DIR=/usr/local/lib64 \ - -DPKGCONFIG_INSTALL_DIR=/usr/local/lib64/pkgconfig \ + -DLIB_INSTALL_DIR=${PREFIX}/lib64 \ + -DPKGCONFIG_INSTALL_DIR=${PREFIX}/lib64/pkgconfig \ -DWITH_AS3=OFF \ -DWITH_C_GLIB=OFF \ -DWITH_CPP=ON \ @@ -718,7 +733,7 @@ RUN --mount=type=bind,from=sources,source=thrift-v0.22.0.tar.gz,target=/tmp/thri -DWITH_ZLIB=OFF \ && cmake --build build --parallel "$(nproc)" --target install \ && sed -i 's|^libdir=.*|libdir=${prefix}/lib64|' \ - /usr/local/lib64/pkgconfig/thrift.pc \ + ${PREFIX}/lib64/pkgconfig/thrift.pc \ && rm -rf build src RUN --mount=type=bind,from=sources,source=snappy-1.2.2.tar.gz,target=/tmp/snappy.tar.gz \ diff --git a/rocky9/build.sh b/rocky9/build.sh index 8018382..4e707f8 100755 --- a/rocky9/build.sh +++ b/rocky9/build.sh @@ -3,6 +3,7 @@ set -eu IMAGE_NAME="${IMAGE_NAME:-rocky9-source-stack}" +PREFIX="${PREFIX:-/usr/local}" SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd) SOURCES_DIR="${SOURCES_DIR:-$SCRIPT_DIR/../sources}" FETCH_SCRIPT="$SCRIPT_DIR/../fetch-sources.sh" @@ -14,5 +15,6 @@ fi SOURCES_DIR="$SOURCES_DIR" "$FETCH_SCRIPT" -echo "Building image $IMAGE_NAME with podman ..." -exec podman build --build-context sources="$SOURCES_DIR" --tag "$IMAGE_NAME" "$SCRIPT_DIR" +echo "Building image $IMAGE_NAME with podman (prefix $PREFIX) ..." +exec podman build --build-context sources="$SOURCES_DIR" \ + --build-arg PREFIX="$PREFIX" --tag "$IMAGE_NAME" "$SCRIPT_DIR"