# ch-test-scope: full
# ch-test-arch-exclude: aarch64      # only x86-64, ppc64le supported by nVidia
# ch-test-builder-exclude: ch-image  # multi-stage build not yet supported

# This Dockerfile demonstrates a multi-stage build. With a single-stage build
# that brings along the nVidia build environment, the resulting unpacked image
# is 2.9 GiB; with the multi-stage build, it's 146 MiB.
#
# See: https://docs.docker.com/develop/develop-images/multistage-build


## Stage 1: Install the nVidia build environment and build a sample app.
FROM ubuntu:16.04

# OS packages needed
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update \
 && apt-get install -y --no-install-recommends \
                    ca-certificates \
                    gnupg-curl \
                    wget \
 && rm -rf /var/lib/apt/lists/*

# Install CUDA from nVidia.
# See: https://developer.nvidia.com/cuda-downloads?target_os=Linux&target_arch=x86_64&target_distro=Ubuntu&target_version=1704&target_type=debnetwork
WORKDIR /usr/local/src
RUN wget -nv http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1704/x86_64/cuda-repo-ubuntu1704_9.1.85-1_amd64.deb \
 && dpkg --install cuda-repo-ubuntu1704_9.1.85-1_amd64.deb \
 && apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1704/x86_64/7fa2af80.pub \
 && apt-get update \
 && apt-get install -y --no-install-recommends cuda-toolkit-9-1 \
 && rm -rf /var/lib/apt/lists/*

# Build the sample app we'll use to test.
WORKDIR /usr/local/cuda-9.1/samples/0_Simple/matrixMulCUBLAS
RUN make


## Stage 2: Copy the built sample app into a clean Ubuntu image.
FROM ubuntu:16.04

COPY --from=0 /usr/local/cuda-9.1/samples/0_Simple/matrixMulCUBLAS /

# This is the one nVidia shared library that the sample app needs. We could be
# smarter about finding this path. However, one thing to avoid is copying in
# all of /usr/local/cuda-9.1/targets/x86_64-linux/lib, because that directory
# is 1.3GiB.
COPY --from=0 /usr/local/cuda-9.1/targets/x86_64-linux/lib/libcublas.so.9.1.181 /usr/local/lib
RUN ldconfig
