gocv icon indicating copy to clipboard operation
gocv copied to clipboard

Example Dockerfile for static build

Open cedricve opened this issue 1 year ago • 3 comments

As suggested in a few PR's and Makefile, it would be beneficial to add a working static build Dockerfile. I have been trying to create the static OpenCV build (which works). But when using the static tag while running go build, I'm getting a few linker errors. I'm currently doing this on an ARM64 (Mac M1), which might explain the linker issues to IPP. I wonder how we could make this work for othe architectures like ARMv7,ARM64,etc.

The static base image:

  FROM golang:1.19-buster 
  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/*

  ARG OPENCV_VERSION="4.7.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 BUILD_SHARED_LIBS=OFF \
        -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 .. && \
        make -j $(nproc --all) && \
        make preinstall && make install && ldconfig && \
        cd / && rm -rf opencv*

The static build container

# to build this docker image:
#   docker build .
FROM gocv- base

ENV GOPATH /go

COPY . /go/src/gocv.io/x/gocv/

WORKDIR /go/src/gocv.io/x/gocv
RUN go build -tags static --ldflags '-extldflags "-static"' -o static_version ./cmd/version/main.go

CMD ["/build/gocv_version"]

While creating the build

> [4/4] RUN go build -tags static --ldflags '-extldflags "-static"' -o static_version ./cmd/version/main.go:                                                                                                   
#6 30.26 # command-line-arguments                                                                       
#6 30.26 /usr/local/go/pkg/tool/linux_arm64/link: running g++ failed: exit status 1                     
#6 30.26 /usr/bin/ld: cannot find -lippiw                                                               
#6 30.26 /usr/bin/ld: cannot find -lippicv
#6 30.26 /usr/bin/ld: /usr/local/lib/libopencv_core.a(opencl_core.cpp.o): in function `opencl_check_fn(int)':
#6 30.26 opencl_core.cpp:(.text._ZL15opencl_check_fni+0x1b0): warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
#6 30.26 /usr/bin/ld: cannot find -lquadmath
#6 30.26 collect2: error: ld returned 1 exit status
#6 30.26 

cedricve avatar Jan 17 '23 09:01 cedricve