Skip to content

Commit

Permalink
docker: multiplatform for both amd64 and arm64
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Jan 2, 2023
1 parent 7b5c099 commit c849766
Showing 1 changed file with 98 additions and 13 deletions.
111 changes: 98 additions & 13 deletions Dockerfile.opencv
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
# to build this docker image:
# docker build -f Dockerfile.opencv -t gocv/opencv:4.6.0 .
# docker build --build-arg OPENCV_VERSION="4.x" --build-arg OPENCV_FILE="https://github.com/opencv/opencv/archive/refs/heads/4.x.zip" --build-arg OPENCV_CONTRIB_FILE="https://github.com/opencv/opencv_contrib/archive/refs/heads/4.x.zip" -f Dockerfile.opencv -t gocv/opencv:4.7.0-dev .
FROM golang:1.19-buster AS opencv
# OpenCV 4 prebuilt multiarchitecture image
#
# To build release:
# docker buildx build -f Dockerfile.opencv --t docker.io/gocv/opencv:4.7.0 --platform=linux/arm64,linux/amd64 --push .
#
# To build prerelease:
# docker buildx build --build-arg OPENCV_VERSION="4.x" --build-arg OPENCV_FILE="https://github.com/opencv/opencv/archive/refs/heads/4.x.zip" --build-arg OPENCV_CONTRIB_FILE="https://github.com/opencv/opencv_contrib/archive/refs/heads/4.x.zip" -f Dockerfile.opencv -t docker.io/gocv/opencv:4.7.0-dev --platform=linux/arm64,linux/amd64 --push .


###################
# amd64 build stage
###################

FROM --platform=linux/amd64 golang:1.19-buster AS opencv-base-amd64
LABEL maintainer="hybridgroup"

RUN apt-get update && apt-get install -y --no-install-recommends \
git build-essential cmake pkg-config unzip libgtk2.0-dev \
curl ca-certificates libcurl4-openssl-dev libssl-dev \
libavcodec-dev libavformat-dev libswscale-dev libtbb2 libtbb-dev \
libjpeg62-turbo-dev libpng-dev libtiff-dev libdc1394-22-dev nasm && \
rm -rf /var/lib/apt/lists/*
RUN apt-get update && apt-get install -y \
git build-essential cmake pkg-config unzip libgtk2.0-dev \
curl ca-certificates libcurl4-openssl-dev libssl-dev \
libavcodec-dev libavformat-dev libswscale-dev libtbb2 libtbb-dev \
libjpeg62-turbo-dev libpng-dev libtiff-dev libdc1394-22-dev nasm && \
rm -rf /var/lib/apt/lists/*

FROM --platform=linux/amd64 opencv-base-amd64 AS opencv-build-amd64

ARG OPENCV_VERSION="4.6.0"
ENV OPENCV_VERSION $OPENCV_VERSION
Expand All @@ -24,8 +36,9 @@ RUN curl -Lo opencv.zip ${OPENCV_FILE} && \
unzip -q opencv.zip && \
curl -Lo opencv_contrib.zip ${OPENCV_CONTRIB_FILE} && \
unzip -q opencv_contrib.zip && \
rm opencv.zip opencv_contrib.zip && \
cd opencv-${OPENCV_VERSION} && \
rm opencv.zip opencv_contrib.zip

RUN cd opencv-${OPENCV_VERSION} && \
mkdir build && cd build && \
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D WITH_IPP=OFF \
Expand All @@ -52,4 +65,76 @@ RUN curl -Lo opencv.zip ${OPENCV_FILE} && \
make preinstall && make install && ldconfig && \
cd / && rm -rf opencv*

CMD ["go version"]

###################
# amd64 build stage
###################

FROM --platform=linux/arm64 golang:1.19-buster AS opencv-base-arm64
LABEL maintainer="hybridgroup"

RUN apt-get update && apt-get install -y --no-install-recommends \
git build-essential cmake pkg-config unzip libgtk2.0-dev \
curl ca-certificates libcurl4-openssl-dev libssl-dev \
libavcodec-dev libavformat-dev libswscale-dev libtbb2 libtbb-dev \
libjpeg62-turbo-dev libpng-dev libtiff-dev libdc1394-22-dev && \
rm -rf /var/lib/apt/lists/*

FROM --platform=linux/arm64 opencv-base-arm64 AS opencv-build-arm64

ARG OPENCV_VERSION="4.6.0"
ENV OPENCV_VERSION $OPENCV_VERSION

ARG OPENCV_FILE="https://github.com/opencv/opencv/archive/${OPENCV_VERSION}.zip"
ENV OPENCV_FILE $OPENCV_FILE

ARG OPENCV_CONTRIB_FILE="https://github.com/opencv/opencv_contrib/archive/${OPENCV_VERSION}.zip"
ENV OPENCV_CONTRIB_FILE $OPENCV_CONTRIB_FILE

RUN curl -Lo opencv.zip ${OPENCV_FILE} && \
unzip -q opencv.zip && \
curl -Lo opencv_contrib.zip ${OPENCV_CONTRIB_FILE} && \
unzip -q opencv_contrib.zip && \
rm opencv.zip opencv_contrib.zip

RUN cd opencv-${OPENCV_VERSION} && \
mkdir build && cd build && \
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-${OPENCV_VERSION}/modules \
-D ENABLE_NEON=ON \
-D WITH_FFMPEG=ON \
-D WITH_TBB=ON \
-D BUILD_TBB=ON \
-D BUILD_TESTS=OFF \
-D WITH_EIGEN=OFF \
-D WITH_GSTREAMER=OFF \
-D WITH_V4L=ON \
-D WITH_LIBV4L=ON \
-D WITH_VTK=OFF \
-D WITH_QT=OFF \
-D BUILD_JPEG=ON \
-D OPENCV_ENABLE_NONFREE=ON \
-D BUILD_DOCS=OFF \
-D BUILD_EXAMPLES=OFF \
-D BUILD_TESTS=OFF \
-D BUILD_PERF_TESTS=OFF \
-D BUILD_opencv_java=NO \
-D BUILD_opencv_python=NO \
-D BUILD_opencv_python2=NO \
-D BUILD_opencv_python3=NO \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D CMAKE_TOOLCHAIN_FILE=../platforms/linux/aarch64-gnu.toolchain.cmake .. && \
make -j $(nproc --all) && \
make preinstall && make install && ldconfig && \
cd / && rm -rf opencv*

ARG TARGETARCH

###################
# multiarch build stage
###################

FROM opencv-build-${TARGETARCH} as opencv-final

CMD ["opencv_version", "-b"]

0 comments on commit c849766

Please sign in to comment.