Skip to content

Commit 6d8ebcd

Browse files
committed
Add multi-stage logic to docker to slim down images
1 parent 7a1cb56 commit 6d8ebcd

File tree

4 files changed

+272
-186
lines changed

4 files changed

+272
-186
lines changed
Lines changed: 67 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,32 @@
1-
FROM nvcr.io/nvidia/cuda:12.8.1-cudnn-devel-rockylinux9
1+
# Multi-stage build to keep final image slim while still providing CUDA, Python, LLVM 7, and Rust.
2+
# Stage 1: build LLVM and install Rust.
3+
FROM nvcr.io/nvidia/cuda:12.8.1-cudnn-devel-rockylinux9 AS builder
24

35
RUN dnf -y update && \
46
dnf -y install \
5-
clang \
6-
openssl-devel \
7-
pkgconfig \
8-
redhat-rpm-config \
9-
which \
10-
xz \
11-
zlib-devel && \
7+
clang \
8+
cmake \
9+
fontconfig-devel \
10+
libX11-devel \
11+
libXcursor-devel \
12+
libXi-devel \
13+
libXrandr-devel \
14+
libffi-devel \
15+
libxml2-devel \
16+
libedit-devel \
17+
ncurses-devel \
18+
openssl-devel \
19+
pkgconfig \
20+
python3 \
21+
redhat-rpm-config \
22+
which \
23+
xz \
24+
zlib-devel \
25+
make && \
1226
dnf clean all
1327

14-
# Needed to build `path_tracer`, `optix/ex03_window` example
15-
RUN dnf -y install \
16-
cmake \
17-
fontconfig-devel \
18-
libX11-devel \
19-
libXcursor-devel \
20-
libXi-devel \
21-
libXrandr-devel && \
22-
dnf clean all
23-
24-
# Get LLVM 7
25-
WORKDIR /data/llvm7
26-
27-
# Install dependencies for building LLVM
28-
RUN dnf -y install epel-release && \
29-
dnf -y install \
30-
libffi-devel \
31-
ncurses-devel \
32-
libxml2-devel \
33-
libedit-devel \
34-
python3 \
35-
make && \
36-
dnf clean all
37-
38-
# Download and build LLVM 7.1.0 for all architectures
28+
# Build LLVM 7.1.0 once and install into /opt/llvm-7
29+
WORKDIR /opt/build
3930
RUN curl -sSf -L -O https://github.com/llvm/llvm-project/releases/download/llvmorg-7.1.0/llvm-7.1.0.src.tar.xz && \
4031
tar -xf llvm-7.1.0.src.tar.xz && \
4132
cd llvm-7.1.0.src && \
@@ -58,25 +49,56 @@ RUN curl -sSf -L -O https://github.com/llvm/llvm-project/releases/download/llvmo
5849
-DLLVM_INCLUDE_BENCHMARKS=OFF \
5950
-DLLVM_ENABLE_ZLIB=ON \
6051
-DLLVM_ENABLE_TERMINFO=ON \
61-
-DCMAKE_INSTALL_PREFIX=/usr \
52+
-DCMAKE_INSTALL_PREFIX=/opt/llvm-7 \
6253
.. && \
63-
make -j$(nproc) && \
54+
make -j"$(nproc)" && \
6455
make install && \
65-
cd ../.. && \
66-
rm -rf llvm-7.1.0.src* && \
67-
ln -s /usr/bin/llvm-config /usr/bin/llvm-config-7 && \
56+
cd /opt && \
57+
rm -rf /opt/build
58+
59+
# Install Rust toolchain (minimal profile) and clean caches
60+
RUN curl -sSf -L https://sh.rustup.rs | bash -s -- -y --profile minimal && \
61+
/root/.cargo/bin/rustup component add rustfmt clippy && \
62+
rm -rf /root/.cargo/registry /root/.cargo/git /root/.rustup/tmp
63+
ENV PATH="/root/.cargo/bin:${PATH}"
64+
65+
# Stage 2: final runtime image
66+
FROM nvcr.io/nvidia/cuda:12.8.1-cudnn-devel-rockylinux9 AS runtime
67+
68+
# Install runtime build dependencies (no compilers for LLVM build needed here)
69+
RUN dnf -y update && \
70+
dnf -y install \
71+
clang \
72+
cmake \
73+
fontconfig-devel \
74+
libX11-devel \
75+
libXcursor-devel \
76+
libXi-devel \
77+
libXrandr-devel \
78+
openssl-devel \
79+
pkgconfig \
80+
python3 \
81+
redhat-rpm-config \
82+
which \
83+
xz \
84+
zlib-devel && \
6885
dnf clean all
6986

70-
# Get Rust
71-
RUN curl -sSf -L https://sh.rustup.rs | bash -s -- -y
87+
# Copy LLVM and Rust from builder stage
88+
COPY --from=builder /opt/llvm-7 /opt/llvm-7
89+
RUN ln -sf /opt/llvm-7/bin/llvm-config /usr/bin/llvm-config && \
90+
ln -sf /opt/llvm-7/bin/llvm-config /usr/bin/llvm-config-7
91+
92+
COPY --from=builder /root/.cargo /root/.cargo
93+
COPY --from=builder /root/.rustup /root/.rustup
7294
ENV PATH="/root/.cargo/bin:${PATH}"
95+
RUN /root/.cargo/bin/rustup show && \
96+
rm -rf /root/.cargo/registry /root/.cargo/git /root/.rustup/tmp
7397

74-
# Setup the workspace
98+
# Set up workspace environment defaults
7599
WORKDIR /data/rust-cuda
76-
RUN --mount=type=bind,source=rust-toolchain.toml,target=/data/rust-cuda/rust-toolchain.toml \
77-
rustup show
78-
79-
# Add nvvm to LD_LIBRARY_PATH.
80100
ENV LD_LIBRARY_PATH="/usr/local/cuda/nvvm/lib64:${LD_LIBRARY_PATH}"
81101
ENV LLVM_LINK_STATIC=1
82102
ENV RUST_LOG=info
103+
104+
CMD ["bash"]
Lines changed: 69 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,32 @@
1-
FROM nvcr.io/nvidia/cuda:11.8.0-cudnn8-devel-ubuntu22.04
1+
# Multi-stage build to keep the final image lean while providing CUDA, LLVM 7, and Rust.
2+
FROM nvcr.io/nvidia/cuda:11.8.0-cudnn8-devel-ubuntu22.04 AS builder
23

3-
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get -qq -y install \
4-
build-essential \
5-
curl \
6-
clang \
7-
libssl-dev \
8-
libtinfo-dev \
9-
pkg-config \
10-
xz-utils \
11-
zlib1g-dev && \
4+
RUN apt-get update && \
5+
DEBIAN_FRONTEND=noninteractive apt-get -y install \
6+
build-essential \
7+
clang \
8+
cmake \
9+
curl \
10+
libffi-dev \
11+
libfontconfig-dev \
12+
libssl-dev \
13+
libtinfo-dev \
14+
libedit-dev \
15+
libncurses5-dev \
16+
libxml2-dev \
17+
libx11-xcb-dev \
18+
libxcursor-dev \
19+
libxi-dev \
20+
libxinerama-dev \
21+
libxrandr-dev \
22+
ninja-build \
23+
pkg-config \
24+
python3 \
25+
xz-utils \
26+
zlib1g-dev && \
1227
rm -rf /var/lib/apt/lists/*
1328

14-
# Needed to build `path_tracer`, `optix/ex03_window` example
15-
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get -qq -y install \
16-
cmake \
17-
libfontconfig-dev \
18-
libx11-xcb-dev \
19-
libxcursor-dev \
20-
libxi-dev \
21-
libxinerama-dev \
22-
libxrandr-dev && \
23-
rm -rf /var/lib/apt/lists/*
24-
25-
# Get LLVM 7
26-
WORKDIR /data/llvm7
27-
28-
# Install dependencies for building LLVM
29-
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get -qq -y install \
30-
libffi-dev \
31-
libedit-dev \
32-
libncurses5-dev \
33-
libxml2-dev \
34-
python3 \
35-
ninja-build && \
36-
rm -rf /var/lib/apt/lists/*
37-
38-
# Download and build LLVM 7.1.0 for all architectures
29+
WORKDIR /opt/build
3930
RUN curl -sSf -L -O https://github.com/llvm/llvm-project/releases/download/llvmorg-7.1.0/llvm-7.1.0.src.tar.xz && \
4031
tar -xf llvm-7.1.0.src.tar.xz && \
4132
cd llvm-7.1.0.src && \
@@ -58,25 +49,56 @@ RUN curl -sSf -L -O https://github.com/llvm/llvm-project/releases/download/llvmo
5849
-DLLVM_INCLUDE_BENCHMARKS=OFF \
5950
-DLLVM_ENABLE_ZLIB=ON \
6051
-DLLVM_ENABLE_TERMINFO=ON \
61-
-DCMAKE_INSTALL_PREFIX=/usr \
52+
-DCMAKE_INSTALL_PREFIX=/opt/llvm-7 \
6253
.. && \
63-
ninja -j$(nproc) && \
54+
ninja -j"$(nproc)" && \
6455
ninja install && \
65-
cd ../.. && \
66-
rm -rf llvm-7.1.0.src* && \
67-
ln -s /usr/bin/llvm-config /usr/bin/llvm-config-7
56+
cd /opt && rm -rf /opt/build
6857

69-
# Get Rust
70-
RUN curl -sSf -L https://sh.rustup.rs | bash -s -- -y
58+
RUN curl -sSf -L https://sh.rustup.rs | bash -s -- -y --profile minimal && \
59+
/root/.cargo/bin/rustup component add rustfmt clippy && \
60+
rm -rf /root/.cargo/registry /root/.cargo/git /root/.rustup/tmp
7161
ENV PATH="/root/.cargo/bin:${PATH}"
7262

73-
# Setup the workspace
74-
WORKDIR /data/rust-cuda
75-
RUN --mount=type=bind,source=rust-toolchain.toml,target=/data/rust-cuda/rust-toolchain.toml \
76-
rustup show
63+
FROM nvcr.io/nvidia/cuda:11.8.0-cudnn8-devel-ubuntu22.04 AS runtime
7764

78-
# Add nvvm to LD_LIBRARY_PATH.
65+
RUN apt-get update && \
66+
DEBIAN_FRONTEND=noninteractive apt-get -y install \
67+
build-essential \
68+
clang \
69+
cmake \
70+
curl \
71+
libffi-dev \
72+
libfontconfig-dev \
73+
libssl-dev \
74+
libtinfo-dev \
75+
libedit-dev \
76+
libncurses5-dev \
77+
libxml2-dev \
78+
libx11-xcb-dev \
79+
libxcursor-dev \
80+
libxi-dev \
81+
libxinerama-dev \
82+
libxrandr-dev \
83+
pkg-config \
84+
python3 \
85+
xz-utils \
86+
zlib1g-dev && \
87+
rm -rf /var/lib/apt/lists/*
88+
89+
COPY --from=builder /opt/llvm-7 /opt/llvm-7
90+
RUN ln -sf /opt/llvm-7/bin/llvm-config /usr/bin/llvm-config && \
91+
ln -sf /opt/llvm-7/bin/llvm-config /usr/bin/llvm-config-7
92+
93+
COPY --from=builder /root/.cargo /root/.cargo
94+
COPY --from=builder /root/.rustup /root/.rustup
95+
ENV PATH="/root/.cargo/bin:${PATH}"
96+
RUN /root/.cargo/bin/rustup show && \
97+
rm -rf /root/.cargo/registry /root/.cargo/git /root/.rustup/tmp
98+
99+
WORKDIR /data/rust-cuda
79100
ENV LD_LIBRARY_PATH="/usr/local/cuda/nvvm/lib64:${LD_LIBRARY_PATH}"
80101
ENV LLVM_LINK_STATIC=1
81102
ENV RUST_LOG=info
82103

104+
CMD ["bash"]

0 commit comments

Comments
 (0)