@@ -17,61 +17,117 @@ ARG BASE_IMAGE
1717# Apache beam version to get Beam binaries.
1818ARG BEAM_VERSION
1919
20- FROM gcr.io/tfx-oss-public/tfx_base:py310-20230921 as wheel-builder
21- # docker build command should be run under root directory of github checkout.
22- ENV TFX_DIR=/tfx
23- ADD . ${TFX_DIR}/src
24- WORKDIR ${TFX_DIR}
20+ FROM ${BASE_IMAGE} AS base-with-gcc13
21+ RUN /opt/conda/bin/conda install -y --override-channels -c conda-forge \
22+ gcc_linux-64=13 \
23+ gxx_linux-64=13 \
24+ binutils_linux-64=2.40 \
25+ ld_impl_linux-64=2.40
26+
27+ ENV CC=/opt/conda/bin/x86_64-conda-linux-gnu-gcc
28+ ENV CXX=/opt/conda/bin/x86_64-conda-linux-gnu-g++
29+ ENV LD=/opt/conda/bin/x86_64-conda-linux-gnu-ld
30+ ENV AR=/opt/conda/bin/x86_64-conda-linux-gnu-ar
31+ ENV NM=/opt/conda/bin/x86_64-conda-linux-gnu-nm
32+ ENV OBJCOPY=/opt/conda/bin/x86_64-conda-linux-gnu-objcopy
33+ ENV OBJDUMP=/opt/conda/bin/x86_64-conda-linux-gnu-objdump
34+ ENV RANLIB=/opt/conda/bin/x86_64-conda-linux-gnu-ranlib
35+ ENV STRIP=/opt/conda/bin/x86_64-conda-linux-gnu-strip
36+
37+ ENV BAZEL_COMPILER=gcc
38+
39+ FROM base-with-gcc13 AS wheel-builder
40+ ARG BASE_IMAGE
41+ ARG BEAM_VERSION
42+ # Whether to use C++ wheels (tfdv, tfx_bsl) from the host cache (/tmp/wheels).
43+ # If true, cached wheels are used if available (this saves time when debugging the docker build issues).
44+ # If false, wheels are built from source even if they exist in the cache.
45+ ARG USE_CPP_WHEELS_FROM_TEMP=false
46+ # Whether to clear the persistent build caches (/tmp/wheels and bazel cache).
47+ ARG CLEAN_CPP_TEMP_CACHE=false
2548
2649# Specify what version of dependent libraries will be used. See dependencies.py.
2750ARG TFX_DEPENDENCY_SELECTOR
2851ENV TFX_DEPENDENCY_SELECTOR=${TFX_DEPENDENCY_SELECTOR}
52+
2953ENV USE_BAZEL_VERSION=6.5.0
3054
31- RUN python -m pip install --upgrade pip wheel setuptools==70.0.0 tomli
55+ RUN apt-get update && apt-get install -y curl git && \
56+ (find /opt/conda/bin -name "python3-config" | head -n 1 | xargs -I {} ln -sf {} /usr/bin/python-config) && \
57+ (find /opt/conda/bin -name "python3-config" | head -n 1 | xargs -I {} ln -sf {} /opt/conda/bin/python-config)
58+ RUN mkdir -p /usr/local/lib/bazel/bin && \
59+ curl -fLO https://github.com/bazelbuild/bazel/releases/download/${USE_BAZEL_VERSION}/bazel-${USE_BAZEL_VERSION}-linux-x86_64 && \
60+ mv bazel-${USE_BAZEL_VERSION}-linux-x86_64 /usr/local/lib/bazel/bin/bazel && \
61+ chmod +x /usr/local/lib/bazel/bin/bazel && \
62+ ln -sf /usr/local/lib/bazel/bin/bazel /usr/local/bin/bazel
63+ ENV PATH="/usr/local/lib/bazel/bin:${PATH}"
64+
65+
66+ COPY . /tfx/src/
67+ WORKDIR /tfx/src/
68+
69+ # 1. C++ Wheels (tfdv, tfx_bsl) - Normal production build path
70+ RUN if [ "$USE_CPP_WHEELS_FROM_TEMP" = "false" ]; then \
71+ echo "Rebuild of C++ wheels (tfdv, tfx_bsl)..." && \
72+ cp tfx/tools/docker/build_tfdv_wheels.sh /tmp/ && \
73+ cp tfx/tools/docker/build_tfx_bsl_wheels.sh /tmp/ && \
74+ cp tfx/tools/docker/*.patch /tmp/ && \
75+ mkdir -p /tfx/src/dist_wheels && \
76+ bash /tmp/build_tfdv_wheels.sh /tfx/src/dist_wheels && \
77+ bash /tmp/build_tfx_bsl_wheels.sh /tfx/src/dist_wheels ; \
78+ fi
79+
80+ # 2. C++ Wheels (tfdv, tfx_bsl) - Cached Path to avoid any CPP rebuilds
81+ RUN --mount=type=cache,target=/tmp/wheels --mount=type=cache,target=/root/.cache/bazel \
82+ if [ "$USE_CPP_WHEELS_FROM_TEMP" = "true" ]; then \
83+ echo "Re-use cached build of C++ wheels (tfdv, tfx_bsl)..." && \
84+ cp tfx/tools/docker/build_tfdv_wheels.sh /tmp/ && \
85+ cp tfx/tools/docker/build_tfx_bsl_wheels.sh /tmp/ && \
86+ cp tfx/tools/docker/*.patch /tmp/ && \
87+ if [ ! -f /tmp/wheels/tensorflow_data_validation-*.whl ]; then bash /tmp/build_tfdv_wheels.sh /tmp/wheels; fi && \
88+ if [ ! -f /tmp/wheels/tfx_bsl-*.whl ]; then bash /tmp/build_tfx_bsl_wheels.sh /tmp/wheels; fi && \
89+ mkdir -p /tfx/src/dist_wheels && \
90+ cp /tmp/wheels/tensorflow_data_validation-*.whl /tfx/src/dist_wheels/ && \
91+ cp /tmp/wheels/tfx_bsl-*.whl /tfx/src/dist_wheels/ ; \
92+ fi
3293
3394# TODO(b/175089240): clean up conditional checks on whether ml-pipelines-sdk is
3495# built after TFX versions <= 0.25 are no longer eligible for cherry-picks.
35- RUN cd ${TFX_DIR}/src; \
96+ RUN echo "Building TFX and SDK wheels from source..." && \
97+ mkdir -p /tfx/src/dist_wheels && \
3698 if [ -e "package_build" ]; then \
37- bash -x package_build/initialize.sh; \
38- cd package_build/ml-pipelines-sdk; \
39- CFLAGS=$(/usr/bin/python-config --cflags) \
40- python setup.py bdist_wheel; \
41- cd ../../package_build/tfx; \
42- CFLAGS=$(/usr/bin/python-config --cflags) \
43- python setup.py bdist_wheel; \
44- cd ../..; \
45- MLSDK_WHEEL=$(find dist -name "ml_pipelines_sdk-*.whl" ); \
46- TFX_WHEEL=$(find dist -name "tfx-*.whl" ); \
99+ bash -x package_build/initialize.sh && \
100+ CFLAGS=$(python-config --cflags) python package_build/ml-pipelines-sdk/setup.py bdist_wheel && \
101+ CFLAGS=$(python-config --cflags) python package_build/tfx/setup.py bdist_wheel && \
102+ cp package_build/ml-pipelines-sdk/dist/*.whl /tfx/src/dist_wheels/ && \
103+ cp package_build/tfx/dist/*.whl /tfx/src/dist_wheels/ ; \
47104 else \
48- CFLAGS=$(/usr/bin/python-config --cflags) \
49- python setup.py bdist_wheel; \
50- MLSDK_WHEEL=; \
51- TFX_WHEEL=$(find dist -name "tfx-*.whl" ); \
52- fi; \
53- if [ "${TFX_DEPENDENCY_SELECTOR}" = "NIGHTLY" ]; then \
54- CFLAGS=$(/usr/bin/python-config --cflags) \
55- python -m pip install \
56- --extra-index-url https://pypi-nightly.tensorflow.org/simple \
57- ${MLSDK_WHEEL} ${TFX_WHEEL}[docker-image] -c tfx/tools/docker/requirements.txt -c tfx/tools/docker/build_constraints.txt; \
58- else \
59- CFLAGS=$(/usr/bin/python-config --cflags) \
60- python -m pip install ${MLSDK_WHEEL} ${TFX_WHEEL}[docker-image] -c tfx/tools/docker/requirements.txt -c tfx/tools/docker/build_constraints.txt; \
61- fi;
62-
63- # We need to name this step for the next COPY --from command.
64- FROM apache/beam_python3.10_sdk:${BEAM_VERSION} as beam-worker
105+ CFLAGS=$(python-config --cflags) python setup.py bdist_wheel && \
106+ cp dist/*.whl /tfx/src/dist_wheels/ ; \
107+ fi
65108
66- # Build stage to extend DLVM image.
67- FROM ${BASE_IMAGE} as install
109+ # --- Stage 2: beam-worker ---
110+ FROM apache/beam_python3.10_sdk:${BEAM_VERSION} AS beam-worker
68111
69- # Additional Python packages which will be installed.
112+ # --- Stage 3: install (Final) ---
113+ FROM base-with-gcc13 AS install
114+ ARG BASE_IMAGE
70115ARG ADDITIONAL_PACKAGES
71-
72- # Specify what version of dependent libraries will be used. See dependencies.py.
116+ ENV TF_CPP_MIN_LOG_LEVEL=3
117+ # Disables Legacy "Virtual" Devices
118+ ENV TF_XLA_FLAGS="--tf_xla_enable_xla_devices=false"
73119ARG TFX_DEPENDENCY_SELECTOR
74120ENV TFX_DEPENDENCY_SELECTOR=${TFX_DEPENDENCY_SELECTOR}
121+ ENV TF_USE_LEGACY_KERAS=1
122+
123+ # 1. Apply OS security updates and install required system libraries
124+ RUN apt-get update && \
125+ apt-get install -y --no-install-recommends \
126+ ca-certificates \
127+ libsnappy-dev \
128+ libcurl4-openssl-dev \
129+ && apt-get clean && \
130+ rm -rf /var/lib/apt/lists/*
75131
76132# Copy from image matching installed version of 'apache-beam'.
77133COPY --from=beam-worker /opt/apache/beam /opt/apache/beam
@@ -84,60 +140,48 @@ ENTRYPOINT ["/opt/apache/beam/boot"]
84140
85141LABEL maintainer="tensorflow-extended-dev@googlegroups.com"
86142
87- RUN sed -i 's/python3/python/g' /usr/bin/pip
88-
89- RUN python -m pip install --upgrade pip setuptools==70.0.0 wheel
90-
91- RUN CURRENT_DIR="$(pwd)" && \
92- mkdir -p /usr/local/lib/bazel/bin && \
93- cd "/usr/local/lib/bazel/bin" && \
94- curl -fLO https://releases.bazel.build/6.5.0/release/bazel-6.5.0-linux-x86_64 && \
95- chmod +x bazel-6.5.0-linux-x86_64 && \
96- ln -sf /usr/local/lib/bazel/bin/bazel-6.5.0-linux-x86_64 /usr/local/bin/bazel && \
97- cd "$CURRENT_DIR"
98-
99- # Build tensorflow-data-validation wheels from source with patch
100- RUN /opt/conda/bin/conda install -y -c conda-forge \
101- gcc_linux-64=13 \
102- gxx_linux-64=13 \
103- binutils_linux-64=2.40 \
104- ld_impl_linux-64=2.40
105-
106- ENV CC=/opt/conda/bin/x86_64-conda-linux-gnu-gcc
107-
108- ENV CXX=/opt/conda/bin/x86_64-conda-linux-gnu-g++
109-
110- ENV LD=/opt/conda/bin/x86_64-conda-linux-gnu-ld
111-
112- COPY tfx/tools/docker/build_tfdv_wheels.sh /tmp/
113- COPY tfx/tools/docker/tfdv.patch /tmp/
114- RUN mkdir -p /tmp/wheels && \
115- bash /tmp/build_tfdv_wheels.sh /tmp/wheels
116-
117- # Build tfx-bsl wheels from source with patch
118- COPY tfx/tools/docker/build_tfx_bsl_wheels.sh /tmp/
119- COPY tfx/tools/docker/tfx_bsl.patch /tmp/
120- RUN bash /tmp/build_tfx_bsl_wheels.sh /tmp/wheels
121-
122- # Copy constraint file and wheels
123- COPY tfx/tools/docker/build_constraints.txt /tmp/
124- COPY tfx/tools/docker/wheels/*.whl /tmp/wheels/
125-
126- # Install tensorflow-data-validation, tfx-bsl, tensorflow-model-analysis, tensorflow-transform, and tensorflow-cloud first
127- RUN PIP_CONSTRAINT=/tmp/build_constraints.txt python -m pip install /tmp/wheels/tensorflow_data_validation-*.whl /tmp/wheels/tfx_bsl-*.whl /tmp/wheels/tensorflow_model_analysis-*.whl /tmp/wheels/tensorflow_transform-*.whl /tmp/wheels/tensorflow_cloud-*.whl
128-
129- COPY --from=wheel-builder /tfx/src/dist/*.whl /tfx/src/dist/
130- WORKDIR /tfx/src
131-
132- RUN MLSDK_WHEEL=$(find dist -name "ml_pipelines_sdk-*.whl" ); \
133- TFX_WHEEL=$(find dist -name "tfx-*.whl" ); \
134- if [ "${TFX_DEPENDENCY_SELECTOR}" = "NIGHTLY" ]; then \
135- PIP_CONSTRAINT=/tmp/build_constraints.txt python -m pip install \
136- --extra-index-url https://pypi-nightly.tensorflow.org/simple \
137- ${MLSDK_WHEEL} ${TFX_WHEEL}[docker-image] ${ADDITIONAL_PACKAGES} ; \
143+ COPY --from=wheel-builder /tfx/src /tfx/src
144+
145+ # 2. Upgrade core python build tools and remove unused vulnerable components
146+ # setuptools==78.1.1 is required for the pkg_resources shim (needed by apache-beam),
147+ # while providing the security fix for CVE-2025-47273.
148+ RUN python -m pip install --upgrade pip setuptools==78.1.1 wheel \
149+ -c /tfx/src/tfx/tools/docker/requirements.txt \
150+ -c /tfx/src/tfx/tools/docker/build_constraints.txt
151+
152+ # 3. Main installation: consolidated to a single RUN for single-pass resolution.
153+ # We explicitly include setuptools==78.1.1 as a top-level requirement here
154+ # to ensure the resolver doesn't downgrade it or use a broken version
155+ # that lacks the pkg_resources shim (needed by apache-beam).
156+ RUN if [ "${TFX_DEPENDENCY_SELECTOR}" = "NIGHTLY" ]; then \
157+ python -m pip install --no-cache-dir \
158+ --extra-index-url https://pypi-nightly.tensorflow.org/simple \
159+ -c /tfx/src/tfx/tools/docker/requirements.txt \
160+ -c /tfx/src/tfx/tools/docker/build_constraints.txt \
161+ /tfx/src/dist_wheels/*.whl \
162+ /tfx/src/tfx/tools/docker/wheels/*.whl \
163+ "$(find /tfx/src/dist_wheels/ \( -name 'tfx_dev-*.whl' -o -name 'tfx-*.whl' \) | head -n 1)[docker-image]" \
164+ tf_keras setuptools==78.1.1 ${ADDITIONAL_PACKAGES} ; \
138165 else \
139- PIP_CONSTRAINT=/tmp/build_constraints.txt python -m pip install ${MLSDK_WHEEL} ${TFX_WHEEL}[docker-image] \
140- ${ADDITIONAL_PACKAGES} ; \
166+ python -m pip install --no-cache-dir \
167+ -c /tfx/src/tfx/tools/docker/requirements.txt \
168+ -c /tfx/src/tfx/tools/docker/build_constraints.txt \
169+ /tfx/src/dist_wheels/*.whl \
170+ /tfx/src/tfx/tools/docker/wheels/*.whl \
171+ "$(find /tfx/src/dist_wheels/ \( -name 'tfx_dev-*.whl' -o -name 'tfx-*.whl' \) | head -n 1)[docker-image]" \
172+ tf_keras setuptools==78.1.1 ${ADDITIONAL_PACKAGES} ; \
141173 fi && \
142- echo "Installed python packages:\n " && python -m pip list && \
174+ (python -m pip uninstall -y jupyter jupyter-server jupyterlab notebook nbconvert jaraco-context jaraco.context || true)
175+
176+ # 4. Final OS cleanup: remove Go toolchain and other unused tools to fix Go-related CVEs
177+ # Many High/Critical CVEs are in the Go stdlib/toolchain which we don't need at runtime.
178+ # We use a loop to avoid build failures if a package name is not found in the repo.
179+ RUN for pkg in golang-go golang git binutils wget policykit-1 packagekit gnupg2 gcc-12; do apt-get purge -y $pkg || echo "Package $pkg not found, skipping" ; done && \
180+ rm -rf /usr/local/go && \
181+ rm -rf /opt/apache/beam && \
182+ find /opt/conda/lib/python3.10/site-packages/apache_beam -type f -name "boot" -delete || true && \
183+ apt-get autoremove -y && \
184+ apt-get clean
185+
186+ RUN echo "Installed python packages:\n " && python -m pip list && \
143187 echo "Setuptools version:" && python -c "import setuptools; print(setuptools.__version__)"
0 commit comments