Home > database >  Multi-stage docker build, prevent concurency for specific stage
Multi-stage docker build, prevent concurency for specific stage

Time:11-03

In docker multi-stage build, How do I prevent other stages running in parallel to a specific stage?

I have a multistage Dockerfile. one of the stages builds opencv stack.

FROM node:12.22.1 AS client_build
COPY client/package*.json .
RUN npm install
RUN npm run build

FROM pkg_builder AS open_cv_builder
ARG PYTHON_VERSION
RUN cd ~ && \
    python${PYTHON_VERSION} -m pip install numpy==1.18.2 && \
    cd ~ && \
    wget -O opencv.zip https://github.com/opencv/opencv/archive/3.4.4.zip && \
    wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/3.4.4.zip && \
    unzip opencv.zip && mv opencv-3.4.4 opencv && \
    unzip opencv_contrib.zip && mv opencv_contrib-3.4.4 opencv_contrib && \
    cd opencv && mkdir build && cd build && \
    cmake -D CMAKE_BUILD_TYPE=RELEASE \
        -D CMAKE_INSTALL_PREFIX=/usr/local \
        -D INSTALL_PYTHON_EXAMPLES=ON \
        -D INSTALL_C_EXAMPLES=OFF \
        -D OPENCV_ENABLE_NONFREE=ON \
        -D PYTHON_EXECUTABLE=python${PYTHON_VERSION} \
        -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
        -D BUILD_EXAMPLES=OFF .. && \
    make -j8 && make install && sh -c 'echo "/usr/local/lib" >> /etc/ld.so.conf.d/opencv.conf' && ldconfig

FROM python_runtime as main
...
COPY --from=client build /dist/* /dist
COPY --from=open_cv_builder /usr/local/python/cv2/python-${PYTHON_VERSION}/cv2.cpython-${PYTHON_VERSION_RAW}m-x86_64-linux-gnu.so /usr/local/lib/python${PYTHON_VERSION}/dist-packages/cv2.so
COPY --from=open_cv_builder /usr/local/python/cv2/python-${PYTHON_VERSION}/cv2.cpython-${PYTHON_VERSION_RAW}m-x86_64-linux-gnu.so /usr/local/lib/python${PYTHON_VERSION}/dist-packages/

this stage uses all cores for the build.

If other stages run in parallel, they freeze and usally get timeout on commands (wget, apt, etc.).

I want to prevent other stages from running while this stage runs.

CodePudding user response:

Concurrency limits were recently added to buildkit in PR #2049. With docker buildx create you can pass a config file that includes the max-parallelism option to limit concurrent steps.

  • Related