Create the basic Docker image that JavaCV application depends on (CentOS7+JDK8+OpenCV4)

Welcome to my GitHub

https://github.com/zq2599/blog_demos

Content: classification and summary of all original articles and supporting source code, involving Java, Docker, Kubernetes, DevOPS, etc;

Overview of this article

Actual combat content

  • In order to reduce the impact of environment and software differences, make the program operation and debugging easier, and make the application run in the container environment, the whole application will eventually be made into a docker image, so our goal is set as the following three items:
  1. Develop the java version of object recognition application
  2. Make this application a docker image
  3. Run the application in the docker environment
  • Based on the above objectives, we can determine the following operation steps:
  1. Prepare docker basic image
  2. Developing java applications
  3. Package the java application into a package file and integrate it into the basic image to get the final java application image
  • The whole process is shown in the figure below:

  • The goal of this article is the first step above: prepare the docker basic image

Make basic image

  • If you have the experience of making java application images, you will have questions: as shown in the red box below, don't you generally take the official image of OpenJDK as the basic image? Why use an article about basic image?

  • For the application of object detection, the official image of OpenJDK is not enough, because a key technology is needed to implement detection in java: javacv. Some local libraries of OpenCV need to be used in the running process of javacv, so opencv needs to be installed in the running environment

  • As shown in the figure below, an application is composed of six parts from bottom to top. It is much more convenient to make the operating system, JDK and OpenCV at the bottom into a basic image. When we develop applications, we only need to pay attention to the upper three layers. Isn't the upper three layers an ordinary maven project?

  • At this point, you should be very clear about what you need to do next: write a Dockerfile file to create an image, which should include centos7 + jdk1 8 + OpenCV4

Go in two steps

  • I intend to create a CentOS7 + JDK8 image first, and then an image integrated with OpenCV. In this way, I can use centos7 + jdk1 alone in some scenes that do not need OpenCV 8 mirror image
  • The analysis is complete. Let's start

Image of CentOS7 + JDK8

  • CentOS is chosen as the operating system because it is the most commonly used in daily work

  • Looking at the official images of OpenJDK, we don't find CentOS as the operating system, so make one yourself. The idea is very simple: find the Dockerfile file of OpenJDK and replace its basic image with centos7 6

  • The following are all the contents of Dockerfile. It can be seen that the core is to download and install files according to different operating systems. The logic is simple and clear, so I won't say more:

FROM centos:7.6.1810

RUN set -eux; \
    yum install -y \
        gzip \
        tar \
        binutils \
        freetype fontconfig \
    ;

ENV JAVA_HOME /usr/java/openjdk-8
ENV PATH $JAVA_HOME/bin:$PATH

# Default to UTF-8 file.encoding
ENV LANG C.UTF-8

RUN set -eux; \
    \
    arch="$(objdump="$(command -v objdump)" && objdump --file-headers "$objdump" | awk -F '[:,]+[[:space:]]+' '$1 == "architecture" { print $2 }')"; \
    case "$arch" in \
        'i386:x86-64') \
            downloadUrl='https://github.com/AdoptOpenJDK/openjdk8-upstream-binaries/releases/download/jdk8u292-b10/OpenJDK8U-jdk_x64_linux_8u292b10.tar.gz'; \
            ;; \
        'aarch64') \
            downloadUrl='https://github.com/AdoptOpenJDK/openjdk8-upstream-binaries/releases/download/jdk8u292-b10/OpenJDK8U-jdk_aarch64_linux_8u292b10.tar.gz'; \
            ;; \
        *) echo >&2 "error: unsupported architecture: '$arch'"; exit 1 ;; \
    esac; \
    \
    curl -fL -o openjdk.tgz "$downloadUrl"; \
    curl -fL -o openjdk.tgz.asc "$downloadUrl.sign"; \
    \
    export GNUPGHOME="$(mktemp -d)"; \
# pre-fetch Andrew Haley's (the OpenJDK 8 and 11 Updates OpenJDK project lead) key so we can verify that the OpenJDK key was signed by it
# (https://github.com/docker-library/openjdk/pull/322#discussion_r286839190)
# we pre-fetch this so that the signature it makes on the OpenJDK key can survive "import-clean" in gpg
    gpg --batch --keyserver keyserver.ubuntu.com --recv-keys EAC843EBD3EFDB98CC772FADA5CD6035332FA671; \
# TODO find a good link for users to verify this key is right (https://mail.openjdk.java.net/pipermail/jdk-updates-dev/2019-April/000951.html is one of the only mentions of it I can find); perhaps a note added to https://adoptopenjdk.net/upstream.html would make sense?
# no-self-sigs-only: https://salsa.debian.org/debian/gnupg2/commit/c93ca04a53569916308b369c8b218dad5ae8fe07
    gpg --batch --keyserver keyserver.ubuntu.com --keyserver-options no-self-sigs-only --recv-keys CA5F11C6CE22644D42C6AC4492EF8D39DC13168F; \
    gpg --batch --list-sigs --keyid-format 0xLONG CA5F11C6CE22644D42C6AC4492EF8D39DC13168F \
        | tee /dev/stderr \
        | grep '0xA5CD6035332FA671' \
        | grep 'Andrew Haley'; \
    gpg --batch --verify openjdk.tgz.asc openjdk.tgz; \
    rm -rf "$GNUPGHOME"; \
    \
    mkdir -p "$JAVA_HOME"; \
    tar --extract \
        --file openjdk.tgz \
        --directory "$JAVA_HOME" \
        --strip-components 1 \
        --no-same-owner \
    ; \
    rm openjdk.tgz*; \
    \
    rm -rf "$JAVA_HOME/jre/lib/security/cacerts"; \
# see "update-ca-trust" script which creates/maintains this cacerts bundle
    ln -sT /etc/pki/ca-trust/extracted/java/cacerts "$JAVA_HOME/jre/lib/security/cacerts"; \
    \
# https://github.com/oracle/docker-images/blob/a56e0d1ed968ff669d2e2ba8a1483d0f3acc80c0/OracleJava/java-8/Dockerfile#L17-L19
    ln -sfT "$JAVA_HOME" /usr/java/default; \
    ln -sfT "$JAVA_HOME" /usr/java/latest; \
    for bin in "$JAVA_HOME/bin/"*; do \
        base="$(basename "$bin")"; \
        [ ! -e "/usr/bin/$base" ]; \
        alternatives --install "/usr/bin/$base" "$base" "$bin" 20000; \
    done; \
    \
# basic smoke test
    javac -version; \
    java -version
  • After writing, execute docker build - t bolingcavalry / centos7 6-jdk8:0.0.1 . The image can be generated if you have a hub docker. Com account, you can also push it to the central warehouse for more people to use

  • Use the history command to see the image content. The details are as follows. The total is more than 500 megabytes, which is not small:

will@Mac-mini$ docker history bolingcavalry/centos7.6-jdk8:0.0.1
IMAGE          CREATED       CREATED BY                                      SIZE      COMMENT
a5dead4a6505   2 days ago    /bin/sh -c set -eux;         arch="$(objdump...   209MB     
<missing>      2 days ago    /bin/sh -c #(nop)  ENV LANG=C.UTF-8             0B        
<missing>      2 days ago    /bin/sh -c #(nop)  ENV PATH=/usr/java/openjd...   0B        
<missing>      2 days ago    /bin/sh -c #(nop)  ENV JAVA_HOME=/usr/java/o...   0B        
<missing>      2 days ago    /bin/sh -c set -eux;     yum install -y     ...   144MB     
<missing>      2 years ago   /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B        
<missing>      2 years ago   /bin/sh -c #(nop)  LABEL org.label-schema.sc...   0B        
<missing>      2 years ago   /bin/sh -c #(nop) ADD file:54b004357379717df...   202MB
  • I've pushed it to hub docker. COM, execute the following command to download it locally:
docker pull bolingcavalry/centos7.6-jdk8:0.0.3

CentOS7+JDK8+OpenCV4 image

  • Next, OpenCV4 can be integrated. Here are two key points to note:
  1. Constrained by the Java CV dependency, the OpenCV version should use 4.5.3
  2. When compiling OpenCV, cmake version is required to be 3.0 x. Therefore, you need to download the corresponding version of cmake
  • Finally, the Dockerfile is as follows. The basic image is bolingcavalry / centos7 6-jdk8:0.0.1. First install a lot of applications required for compilation, then download cmake and install it, then download the source code of OpenCV-4.5.3, and then compile it. It's so simple (but there's still a lot of debugging work during this period. If you don't say it, it's tears):
FROM bolingcavalry/centos7.6-jdk8:0.0.1

RUN echo "export LC_ALL=en_US.UTF-8"  >>  /etc/profile \
    && source /etc/profile

RUN set -eux; \
    yum install -y \
        make \
        gcc \
        gcc-c++ \
        gtk+-devel \
        gimp-devel \
        gimp-devel-tools \
        gimp-help-browser \
        zlib-devel \
        libtiff-devel \
        libjpeg-devel \
        libpng-devel \
        gstreamer-devel \
        libavc1394-devel \
        libraw1394-devel \
        libdc1394-devel \
        jasper-devel \
        jasper-utils \
        swig \
        python \
        libtool \
        nasm \
        build-essential \
        ant \
        unzip \
    ;

RUN set -eux; \
    curl -fL -o cmake-3.12.2-Linux-x86_64.tar.gz https://cmake.org/files/v3.12/cmake-3.12.2-Linux-x86_64.tar.gz \
    && tar -zxvf cmake-3.12.2-Linux-x86_64.tar.gz \
    && mv cmake-3.12.2-Linux-x86_64 cmake-3.12.2 \
    && ln -sf /cmake-3.12.2/bin/* /usr/bin; \
    curl -fL -o opencv-4.5.3.zip https://codeload.github.com/opencv/opencv/zip/4.5.3; \
    unzip opencv-4.5.3.zip; \
    rm -rf opencv-4.5.3.zip; \
    cd opencv-4.5.3; \
    mkdir build; \
    cd build; \
    cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..; \
    make; \
    make install; \
    cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -DBUILD_TESTS=OFF ..;\
    make -j8; \
    make install
  • Execute the command docker build - t bolingcavalry / opencv4 5.3:0.0.1 . The image can be generated if you have a hub docker. Com account, you can also push it to the central warehouse for more people to use

  • Use the history command to see the contents of the image. The details are as follows. Take a breath. With such a large volume, will dear readers kill me...:

will@Mac-mini centos7-jdk8 % docker history bolingcavalry/opencv4.5.3:0.0.1
IMAGE          CREATED        CREATED BY                                      SIZE      COMMENT
d1518ffa4699   5 days ago     RUN /bin/sh -c set -eux;     curl -fL -o cma...   819MB     buildkit.dockerfile.v0
<missing>      5 days ago     RUN /bin/sh -c set -eux;     yum install -y ...   637MB     buildkit.dockerfile.v0
<missing>      5 days ago     RUN /bin/sh -c echo "export LC_ALL=en_US.UTF...   1.84kB    buildkit.dockerfile.v0
<missing>      3 months ago   /bin/sh -c set -eux;         arch="$(objdump...   209MB     
<missing>      3 months ago   /bin/sh -c #(nop)  ENV LANG=C.UTF-8             0B        
<missing>      3 months ago   /bin/sh -c #(nop)  ENV PATH=/usr/java/openjd...   0B        
<missing>      3 months ago   /bin/sh -c #(nop)  ENV JAVA_HOME=/usr/java/o...   0B        
<missing>      3 months ago   /bin/sh -c set -eux;     yum install -y     ...   144MB     
<missing>      2 years ago    /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B        
<missing>      2 years ago    /bin/sh -c #(nop)  LABEL org.label-schema.sc...   0B        
<missing>      2 years ago    /bin/sh -c #(nop) ADD file:54b004357379717df...   202MB
  • I've pushed it to hub docker. COM, execute the following command to download it locally:
docker pull bolingcavalry/opencv4.5.3:0.0.1
  • Here, I would like to remind you that the compilation of opencv is very time-consuming. Please ensure that the performance of the docker host is not too poor, and please wait patiently for the compilation process

  • So far, the basic image that meets our requirements is opencv4 5.3:0.0.1 has been completed. With it, we have gathered the jdk and opencv required by our Java applications. When developing javacv related applications, based on it, we don't have to worry about the environment and dependent libraries, and can finally focus on Java development

You're not alone. Xinchen's original accompanies you all the way

  1. Java series
  2. Spring collection
  3. Docker series
  4. kubernetes series
  5. Database + middleware series
  6. DevOps series

Welcome to the official account: programmer Xin Chen

Wechat search "programmer Xinchen", I'm Xinchen, looking forward to traveling with you in the Java World
https://github.com/zq2599/blog_demos

Added by sirfartalot on Thu, 13 Jan 2022 03:14:50 +0200