1- FROM debian:bullseye as dependencies
1+ # Default build produces the "runtime" stage (slim). Use --target gtsam for a dev image with build tools and shell.
2+ FROM debian:trixie-20260112 AS dependencies
23ARG PYTHON_VERSION=3.11.2
34
45# Disable GUI prompts
5- ENV DEBIAN_FRONTEND noninteractive
6+ ENV DEBIAN_FRONTEND= noninteractive
67
7-
8- RUN rm /var/lib/dpkg/info/libc-bin.*
9- RUN apt-get clean && apt-get update
10- RUN apt-get -y install libc-bin
11-
12- # Install required build dependencies
13- RUN apt-get update && apt-get install -y \
8+ # Install required build dependencies (single update for better layer caching)
9+ RUN apt-get update && apt-get install -y --no-install-recommends \
10+ ca-certificates \
1411 build-essential \
1512 wget \
1613 libssl-dev \
@@ -33,56 +30,9 @@ RUN apt-get update && apt-get install -y \
3330 libmpc-dev \
3431 libmpfr-dev \
3532 libgmp-dev \
33+ make \
3634 && rm -rf /var/lib/apt/lists/*
3735
38-
39- # Download and build GCC 13.4
40- RUN wget https://ftp.gnu.org/gnu/gcc/gcc-13.4.0/gcc-13.4.0.tar.gz && \
41- tar -xzf gcc-13.4.0.tar.gz && \
42- cd gcc-13.4.0 && \
43- ./contrib/download_prerequisites && \
44- mkdir build && \
45- cd build && \
46- ../configure --prefix=/usr/local/gcc-13.4.0 \
47- --enable-languages=c,c++ \
48- --disable-multilib \
49- --disable-bootstrap \
50- --enable-checking=release && \
51- make -j$(nproc) && \
52- make install && \
53- cd ../.. && \
54- rm -rf gcc-13.4.0 gcc-13.4.0.tar.gz
55-
56- # Set up GCC 13.4 as the default compiler
57- ENV PATH="/usr/local/gcc-13.4.0/bin:${PATH}"
58- ENV LD_LIBRARY_PATH="/usr/local/gcc-13.4.0/lib64:${LD_LIBRARY_PATH}"
59- ENV CC="/usr/local/gcc-13.4.0/bin/gcc"
60- ENV CXX="/usr/local/gcc-13.4.0/bin/g++"
61-
62- # Create symlinks for easier access
63- RUN ln -sf /usr/local/gcc-13.4.0/bin/gcc /usr/local/bin/gcc && \
64- ln -sf /usr/local/gcc-13.4.0/bin/g++ /usr/local/bin/g++
65-
66- # Install Make 4.4.1
67- RUN wget https://ftp.gnu.org/gnu/make/make-4.4.1.tar.gz && \
68- tar -xzf make-4.4.1.tar.gz && \
69- cd make-4.4.1 && \
70- ./configure --prefix=/usr/local && \
71- make -j$(nproc) && \
72- make install && \
73- cd .. && \
74- rm -rf make-4.4.1 make-4.4.1.tar.gz
75-
76- # Install CMake 4.0.3
77- RUN wget https://github.com/Kitware/CMake/releases/download/v4.0.3/cmake-4.0.3.tar.gz && \
78- tar -xzf cmake-4.0.3.tar.gz && \
79- cd cmake-4.0.3 && \
80- ./bootstrap --prefix=/usr/local && \
81- make -j$(nproc) && \
82- make install && \
83- cd .. && \
84- rm -rf cmake-4.0.3 cmake-4.0.3.tar.gz
85-
8636# Set working directory
8737WORKDIR /usr/src
8838
@@ -100,33 +50,33 @@ RUN wget https://www.python.org/ftp/python/${PYTHON_VERSION}/Python-${PYTHON_VER
10050# Ensure /usr/local/bin is in the PATH
10151ENV PATH="/usr/local/bin:${PATH}"
10252
103- RUN python3 -m pip install --upgrade pip
53+ RUN python3 -m pip install --no-cache-dir -- upgrade pip
10454
10555# Use git to clone gtsam and specific GTSAM version
106- FROM alpine/git:2.52.0 as gtsam-clone
56+ FROM alpine/git:2.52.0 AS gtsam-clone
10757
10858ARG GTSAM_VERSION=4.2.0
10959WORKDIR /usr/src/
11060
111- # Clone GTSAM and checkout to given GTSAM_VERSION tag
112- RUN git clone --no-checkout https://github.com/borglab/gtsam.git && \
113- cd gtsam && \
114- git fetch origin tag ${GTSAM_VERSION} && \
115- git checkout ${GTSAM_VERSION}
61+ # Shallow clone specific tag for smaller, faster fetch
62+ RUN git clone --depth 1 --branch ${GTSAM_VERSION} https://github.com/borglab/gtsam.git
11663
11764# Create new stage called gtsam for GTSAM building
118- FROM dependencies as gtsam
65+ FROM dependencies AS gtsam
66+
67+ ARG PYTHON_VERSION=3.11.2
68+
69+ # Needed to link with GTSAM (ENV works in non-interactive shells; .bashrc does not)
70+ ENV LD_LIBRARY_PATH=/usr/local/lib
11971
12072# Move gtsam data
12173COPY --from=gtsam-clone /usr/src/gtsam /usr/src/gtsam
12274
12375WORKDIR /usr/src/gtsam/build
12476
125- # Needed to link with GTSAM
126- RUN echo "export LD_LIBRARY_PATH=/usr/local/lib:\$ LD_LIBRARY_PATH" >> /root/.bashrc
127-
128- # Install python wrapper requirements
129- RUN python3 -m pip install -U -r /usr/src/gtsam/python/requirements.txt
77+ # Install python wrapper requirements, then pin numpy for GTSAM ABI compatibility
78+ RUN python3 -m pip install --no-cache-dir -U -r /usr/src/gtsam/python/requirements.txt && \
79+ python3 -m pip install --no-cache-dir "numpy==1.26.4"
13080
13181# Run cmake
13282RUN cmake \
@@ -141,14 +91,39 @@ RUN cmake \
14191 -DCMAKE_POLICY_VERSION_MINIMUM=3.5 \
14292 ..
14393
144- # Make install and clean up
94+ # Build, install, strip binaries, and clean in one layer to reduce image size
14595RUN make -j$(nproc) install && \
14696 make python-install && \
147- make clean
97+ # find /usr/local -type f \( -name "*.so" -o -name "*.so.*" \) -exec strip --strip-unneeded {} \; 2>/dev/null || true && \
98+ # find /usr/local/bin /usr/local/lib -executable -type f -exec strip --strip-unneeded {} \; 2>/dev/null || true && \
99+ make clean && \
100+ ldconfig
101+
102+ # Final cleanup (dependencies stage already cleared apt lists)
103+ RUN rm -rf /tmp/* /var/tmp/*
104+
105+ # -----------------------------------------------------------------------------
106+ # Slim runtime stage: copy only installed artifacts, no build tools or source
107+ # -----------------------------------------------------------------------------
108+ FROM debian:trixie-slim AS runtime
109+
110+ ENV DEBIAN_FRONTEND=noninteractive
111+ ENV PATH="/usr/local/bin:${PATH}"
112+ ENV LD_LIBRARY_PATH=/usr/local/lib
113+
114+ # Runtime libs only. Python binary (ldd python3.11) needs only libc/libm/libpython; GTSAM needs Boost + TBB (see scripts/audit-runtime-deps.sh).
115+ # Add back libssl3t64 libbz2-1.0 libreadline8t64 libsqlite3-0 libffi8 zlib1g libncursesw6 if you import ssl/sqlite3/readline/etc.
116+ RUN apt-get update && apt-get install -y --no-install-recommends \
117+ ca-certificates \
118+ libtbb12 \
119+ libtbbmalloc2 \
120+ libboost-serialization1.83.0 \
121+ libboost-filesystem1.83.0 \
122+ libboost-timer1.83.0 \
123+ && rm -rf /var/lib/apt/lists/*
148124
149- RUN apt-get clean && \
150- rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
125+ COPY --from=gtsam /usr/local /usr/local
151126
152127RUN ldconfig
153128
154- CMD ["bash " ]
129+ CMD ["python3 " ]
0 commit comments