关于在Docker构建阶段使用镜像源加速自定义Bitnami PostgreSQL容器构建的问询
Question
I'm trying to build a custom Bitnami PostgreSQL container (to add extensions like zhparser), but I'm stuck with extremely slow download speeds from the official Bitnami package servers—it's been hours and the build still hasn't finished. Is there a way to use mirror sources during the Docker build phase to speed up the process?
Here's my current Dockerfile:
FROM docker.io/bitnami/minideb:bullseye ENV HOME="/" \ OS_ARCH="amd64" \ OS_FLAVOUR="debian-11" \ OS_NAME="linux" ARG EXTRA_LOCALES="" ARG WITH_ALL_LOCALES="no" COPY prebuildfs / SHELL ["/bin/bash", "-o", "pipefail", "-c"] # Install required system packages and dependencies RUN install_packages acl ca-certificates curl gzip libbsd0 libbz2-1.0 libc6 libedit2 libffi7 libgcc-s1 libgmp10 libgnutls30 libhogweed6 libicu67 libidn2-0 libldap-2.4-2 liblz4-1 liblzma5 libmd0 libncurses6 libnettle8 libp11-kit0 libpcre3 libreadline8 libsasl2-2 libsqlite3-0 libssl1.1 libstdc++6 libtasn1-6 libtinfo6 libunistring2 libuuid1 libxml2 libxslt1.1 libzstd1 locales procps tar zlib1g RUN . /opt/bitnami/scripts/libcomponent.sh && component_unpack "gosu" "1.14.0-152" --checksum 0c751c7e2ec0bc900a19dbec0306d6294fe744ddfb0fa64197ba1a36040092f0 RUN . /opt/bitnami/scripts/libcomponent.sh && component_unpack "postgresql" "13.7.0-155" --checksum 0aeb44b6993a10dd3915599405c871d438509ffbb818ff548b644c627b9dd1c0 RUN apt-get update && apt-get upgrade -y && \ rm -r /var/lib/apt/lists /var/cache/apt/archives RUN chmod g+rwX /opt/bitnami RUN localedef -c -f UTF-8 -i en_US en_US.UTF-8 RUN update-locale LANG=C.UTF-8 LC_MESSAGES=POSIX && \ DEBIAN_FRONTEND=noninteractive dpkg-reconfigure locales RUN echo 'en_GB.UTF-8 UTF-8' >> /etc/locale.gen && locale-gen RUN echo 'en_US.UTF-8 UTF-8' >> /etc/locale.gen && locale-gen COPY rootfs / RUN /opt/bitnami/scripts/postgresql/postunpack.sh RUN /opt/bitnami/scripts/locales/add-extra-locales.sh ENV APP_VERSION="13.7.0" \ BITNAMI_APP_NAME="postgresql" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US:en" \ NSS_WRAPPER_LIB="/opt/bitnami/common/lib/libnss_wrapper.so" \ PATH="/opt/bitnami/common/bin:/opt/bitnami/postgresql/bin:$PATH" VOLUME [ "/bitnami/postgresql", "/docker-entrypoint-initdb.d", "/docker-entrypoint-preinitdb.d" ] EXPOSE 5432 USER 1001 ENTRYPOINT [ "/opt/bitnami/scripts/postgresql/entrypoint.sh" ] CMD [ "/opt/bitnami/scripts/postgresql/run.sh" ]
Answer
Absolutely—you can speed up your build drastically by switching to mirror sources for both Debian system packages and Bitnami's pre-built components. Here's a step-by-step solution tailored to your Dockerfile:
1. Replace Debian APT Sources with a Fast Mirror
First, override the default APT sources with a regional mirror (example uses Tsinghua University's mirror, which is great for users in Asia) before installing packages:
# Add this right after the FROM line RUN echo "deb http://mirrors.tuna.tsinghua.edu.cn/debian bullseye main contrib non-free" > /etc/apt/sources.list && \ echo "deb http://mirrors.tuna.tsinghua.edu.cn/debian bullseye-updates main contrib non-free" >> /etc/apt/sources.list && \ echo "deb http://mirrors.tuna.tsinghua.edu.cn/debian-security bullseye-security main contrib non-free" >> /etc/apt/sources.list
2. Override Bitnami Component Download URL
Bitnami's component_unpack function uses an official base URL to fetch binaries. You can override this with a mirror by setting the BITNAMI_PACKAGES_URL environment variable:
# Add this to your ENV block right after FROM ENV BITNAMI_PACKAGES_URL="https://mirrors.tuna.tsinghua.edu.cn/bitnami"
3. Full Modified Dockerfile
Here's your updated Dockerfile with both optimizations applied:
FROM docker.io/bitnami/minideb:bullseye ENV HOME="/" \ OS_ARCH="amd64" \ OS_FLAVOUR="debian-11" \ OS_NAME="linux" \ BITNAMI_PACKAGES_URL="https://mirrors.tuna.tsinghua.edu.cn/bitnami" # Bitnami mirror added ARG EXTRA_LOCALES="" ARG WITH_ALL_LOCALES="no" COPY prebuildfs / SHELL ["/bin/bash", "-o", "pipefail", "-c"] # Replace Debian APT sources with Tsinghua mirror RUN echo "deb http://mirrors.tuna.tsinghua.edu.cn/debian bullseye main contrib non-free" > /etc/apt/sources.list && \ echo "deb http://mirrors.tuna.tsinghua.edu.cn/debian bullseye-updates main contrib non-free" >> /etc/apt/sources.list && \ echo "deb http://mirrors.tuna.tsinghua.edu.cn/debian-security bullseye-security main contrib non-free" >> /etc/apt/sources.list # Install required system packages and dependencies RUN install_packages acl ca-certificates curl gzip libbsd0 libbz2-1.0 libc6 libedit2 libffi7 libgcc-s1 libgmp10 libgnutls30 libhogweed6 libicu67 libidn2-0 libldap-2.4-2 liblz4-1 liblzma5 libmd0 libncurses6 libnettle8 libp11-kit0 libpcre3 libreadline8 libsasl2-2 libsqlite3-0 libssl1.1 libstdc++6 libtasn1-6 libtinfo6 libunistring2 libuuid1 libxml2 libxslt1.1 libzstd1 locales procps tar zlib1g RUN . /opt/bitnami/scripts/libcomponent.sh && component_unpack "gosu" "1.14.0-152" --checksum 0c751c7e2ec0bc900a19dbec0306d6294fe744ddfb0fa64197ba1a36040092f0 RUN . /opt/bitnami/scripts/libcomponent.sh && component_unpack "postgresql" "13.7.0-155" --checksum 0aeb44b6993a10dd3915599405c871d438509ffbb818ff548b644c627b9dd1c0 RUN apt-get update && apt-get upgrade -y && \ rm -r /var/lib/apt/lists /var/cache/apt/archives RUN chmod g+rwX /opt/bitnami RUN localedef -c -f UTF-8 -i en_US en_US.UTF-8 RUN update-locale LANG=C.UTF-8 LC_MESSAGES=POSIX && \ DEBIAN_FRONTEND=noninteractive dpkg-reconfigure locales RUN echo 'en_GB.UTF-8 UTF-8' >> /etc/locale.gen && locale-gen RUN echo 'en_US.UTF-8 UTF-8' >> /etc/locale.gen && locale-gen COPY rootfs / RUN /opt/bitnami/scripts/postgresql/postunpack.sh RUN /opt/bitnami/scripts/locales/add-extra-locales.sh ENV APP_VERSION="13.7.0" \ BITNAMI_APP_NAME="postgresql" \ LANG="en_US.UTF-8" \ LANGUAGE="en_US:en" \ NSS_WRAPPER_LIB="/opt/bitnami/common/lib/libnss_wrapper.so" \ PATH="/opt/bitnami/common/bin:/opt/bitnami/postgresql/bin:$PATH" VOLUME [ "/bitnami/postgresql", "/docker-entrypoint-initdb.d", "/docker-entrypoint-preinitdb.d" ] EXPOSE 5432 USER 1001 ENTRYPOINT [ "/opt/bitnami/scripts/postgresql/entrypoint.sh" ] CMD [ "/opt/bitnami/scripts/postgresql/run.sh" ]
4. Extra Tips for Even Faster Builds
- Cache Packages: Use Docker's build cache effectively (avoid changing lines before package installation unless necessary) or set up a local APT proxy for repeated builds.
- Pre-Download Components: Manually fetch the Bitnami binaries from the mirror, copy them into your build context, and modify
component_unpackto use local files instead of remote URLs. - Verify Mirror Compatibility: Double-check that your chosen mirror hosts the exact component versions and checksums specified in your Dockerfile to avoid build errors.
内容的提问来源于stack exchange,提问作者Dolphin

