最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

docker - DockerFile RUN command not CACHED - Stack Overflow

programmeradmin1浏览0评论

I'm new to docker and trying to create a docker image for a opencv project.

FROM ubuntu:20.04

ENV DEBIAN_FRONTEND=noninteractive
ENV CMAKE_VERSION=3.21.5

# Update and upgrade packages
RUN apt update && apt -y install \
    sudo \
    wget \
    build-essential \
    git \
    openssl \
    libssl-dev

# Fetch all dependencies
RUN cd /tmp \
    && wget ${CMAKE_VERSION}/cmake-${CMAKE_VERSION}.tar.gz \
    && wget .9.9/Python-3.9.9.tar.xz \
    && wget .pin \
    && wget .4.0/local_installers/cuda-repo-ubuntu2004-12-4-local_12.4.0-550.54.14-1_amd64.deb \
    && wget .4.tar.gz    \
    && git clone .git \
    && git clone .git \
    && git clone .git

# Build and install cmake
RUN cd /tmp \
    && tar -xvf cmake-${CMAKE_VERSION}.tar.gz \
    && mkdir cmake-${CMAKE_VERSION}/build \
    && cd cmake-${CMAKE_VERSION}/build \
    && ../configure --prefix=/usr \
    && make -j $(nproc) \
    && make install

# Build and install ninja
RUN cd /tmp/ninja \
    && git checkout v1.12.1 \
    && cmake -Bbuild \
    && cmake --build build \
    && make -Cbuild install 

# Build and install python
RUN cd /tmp \
    && apt install zlib1g-dev \
    && tar -xf Python-3.9.9.tar.xz \
    && cd Python-3.9.9 \
    && mkdir build \
    && cd build \
    && ../configure --enable-optimizations --prefix=/usr \
    && make -j $(nproc) \
    && make install

# Build and install cuda and its deps
RUN cd /tmp \
    && mv cuda-ubuntu2004.pin /etc/apt/preferences.d/cuda-repository-pin-600

RUN cd /tmp \
    && dpkg -i cuda-repo-ubuntu2004-12-4-local_12.4.0-550.54.14-1_amd64.deb \
    && cp /var/cuda-repo-ubuntu2004-12-4-local/cuda-*-keyring.gpg /usr/share/keyrings/ \
    && apt update

RUN apt install -y cuda-toolkit-12-4 mesa-utils

RUN export PATH=$PATH:/usr/local/cuda/bin

# Build cuda samples
RUN cd /tmp \
    && tar -xvf v12.4.tar.gz \
    && cd cuda-samples-12.4 \
    && make -j $(nproc)

RUN cd /tmp \
    && cd opencv_contrib \
    && git checkout 4.11.0 \
    && cd ../opencv \
    && git checkout 4.11.0 \
    && cmake \
        -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules \
        -DBUILD_TESTS=OFF \
        -DBUILD_EXAMPLES=OFF \
        -DBUILD_PERF_TESTS=OFF \
        -DWITH_CUDA=ON \
        -Bbuild \
    && cmake --build build -j $(nproc) \
    && make -Cbuild install

Every time I rebuild the image the build will start form RUN apt install -y cuda-toolkit-12-4 mesa-utils. This command will never be CACHED. Does anyone know why?

I tried shuffling some commands around but even the apt update. But it looks like the apt install command is the one that not is getting cached.

I'm new to docker and trying to create a docker image for a opencv project.

FROM ubuntu:20.04

ENV DEBIAN_FRONTEND=noninteractive
ENV CMAKE_VERSION=3.21.5

# Update and upgrade packages
RUN apt update && apt -y install \
    sudo \
    wget \
    build-essential \
    git \
    openssl \
    libssl-dev

# Fetch all dependencies
RUN cd /tmp \
    && wget https://github/Kitware/CMake/releases/download/v${CMAKE_VERSION}/cmake-${CMAKE_VERSION}.tar.gz \
    && wget https://www.python./ftp/python/3.9.9/Python-3.9.9.tar.xz \
    && wget https://developer.download.nvidia/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pin \
    && wget https://developer.download.nvidia/compute/cuda/12.4.0/local_installers/cuda-repo-ubuntu2004-12-4-local_12.4.0-550.54.14-1_amd64.deb \
    && wget https://github/NVIDIA/cuda-samples/archive/refs/tags/v12.4.tar.gz    \
    && git clone https://github/ninja-build/ninja.git \
    && git clone https://github/opencv/opencv_contrib.git \
    && git clone https://github/opencv/opencv.git

# Build and install cmake
RUN cd /tmp \
    && tar -xvf cmake-${CMAKE_VERSION}.tar.gz \
    && mkdir cmake-${CMAKE_VERSION}/build \
    && cd cmake-${CMAKE_VERSION}/build \
    && ../configure --prefix=/usr \
    && make -j $(nproc) \
    && make install

# Build and install ninja
RUN cd /tmp/ninja \
    && git checkout v1.12.1 \
    && cmake -Bbuild \
    && cmake --build build \
    && make -Cbuild install 

# Build and install python
RUN cd /tmp \
    && apt install zlib1g-dev \
    && tar -xf Python-3.9.9.tar.xz \
    && cd Python-3.9.9 \
    && mkdir build \
    && cd build \
    && ../configure --enable-optimizations --prefix=/usr \
    && make -j $(nproc) \
    && make install

# Build and install cuda and its deps
RUN cd /tmp \
    && mv cuda-ubuntu2004.pin /etc/apt/preferences.d/cuda-repository-pin-600

RUN cd /tmp \
    && dpkg -i cuda-repo-ubuntu2004-12-4-local_12.4.0-550.54.14-1_amd64.deb \
    && cp /var/cuda-repo-ubuntu2004-12-4-local/cuda-*-keyring.gpg /usr/share/keyrings/ \
    && apt update

RUN apt install -y cuda-toolkit-12-4 mesa-utils

RUN export PATH=$PATH:/usr/local/cuda/bin

# Build cuda samples
RUN cd /tmp \
    && tar -xvf v12.4.tar.gz \
    && cd cuda-samples-12.4 \
    && make -j $(nproc)

RUN cd /tmp \
    && cd opencv_contrib \
    && git checkout 4.11.0 \
    && cd ../opencv \
    && git checkout 4.11.0 \
    && cmake \
        -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules \
        -DBUILD_TESTS=OFF \
        -DBUILD_EXAMPLES=OFF \
        -DBUILD_PERF_TESTS=OFF \
        -DWITH_CUDA=ON \
        -Bbuild \
    && cmake --build build -j $(nproc) \
    && make -Cbuild install

Every time I rebuild the image the build will start form RUN apt install -y cuda-toolkit-12-4 mesa-utils. This command will never be CACHED. Does anyone know why?

I tried shuffling some commands around but even the apt update. But it looks like the apt install command is the one that not is getting cached.

Share Improve this question edited Feb 10 at 15:16 Christoph Rackwitz 15.6k5 gold badges39 silver badges51 bronze badges asked Feb 10 at 12:00 Johnny BakkerJohnny Bakker 74 bronze badges 1
  • What tool are you using to perform the build. Show its configuration, the build command, and the output. E.g. buildx has a configurable cache size. – BMitch Commented Feb 10 at 13:17
Add a comment  | 

1 Answer 1

Reset to default 0

Adding rm -rf /var/lib/apt/lists/* after my apt update command solved the problem.

# Install CUDA and dependencies in a single RUN for better caching
RUN cd /tmp \
    && mv cuda-ubuntu2004.pin /etc/apt/preferences.d/cuda-repository-pin-600 \
    && dpkg -i cuda-repo-ubuntu2004-12-4-local_12.4.0-550.54.14-1_amd64.deb \
    && cp /var/cuda-repo-ubuntu2004-12-4-local/cuda-*-keyring.gpg /usr/share/keyrings/ \
    && apt update \
    && apt install -y --no-install-recommends cuda-toolkit-12-4 mesa-utils \
    && rm -rf /var/lib/apt/lists/* \
    && export PATH=$PATH:/usr/local/cuda/bin \
    && tar -xvf v12.4.tar.gz \
    && cd cuda-samples-12.4 \
    && make -j $(nproc)
发布评论

评论列表(0)

  1. 暂无评论