歡迎訪問(wèn)我的GitHub
https://github.com/zq2599/blog_demos
內(nèi)容:所有原創(chuàng)文章分類(lèi)匯總及配套源碼针炉,涉及Java床绪、Docker、Kubernetes、DevOPS等;
本篇概覽
- 如果您看過(guò)《三分鐘極速體驗(yàn):Java版人臉檢測(cè)》一文,甚至動(dòng)手實(shí)際操作過(guò),您應(yīng)該會(huì)對(duì)背后的技術(shù)細(xì)節(jié)感興趣,接下來(lái)就請(qǐng)隨欣宸一起動(dòng)手實(shí)戰(zhàn)度陆,從無(wú)到有將這個(gè)應(yīng)用開(kāi)發(fā)出來(lái);
- 首先確定咱們的目標(biāo):
- 開(kāi)發(fā)出java版的人臉檢測(cè)應(yīng)用
- 將此應(yīng)用制作成docker鏡像
- 在docker環(huán)境運(yùn)行這個(gè)應(yīng)用
- 基于上述目標(biāo)献幔,我們可以確定以下操作步驟:
- 準(zhǔn)備好docker基礎(chǔ)鏡像
- 開(kāi)發(fā)java應(yīng)用
- 將java應(yīng)用打包成package文件懂傀,集成到基礎(chǔ)鏡像中,得到最終的java應(yīng)用鏡像
- 本篇的目標(biāo)就是上述第一步:<font color="blue">準(zhǔn)備好docker基礎(chǔ)鏡像</font>
關(guān)于人臉檢測(cè)應(yīng)用的基礎(chǔ)鏡像
- 如果您有過(guò)制作java應(yīng)用鏡像的經(jīng)歷蜡感,一定會(huì)產(chǎn)生疑問(wèn):如下圖紅框所示蹬蚁,一般不都是把OpenJDK的官方鏡像作為基礎(chǔ)鏡像嗎?為啥要拿一篇文章來(lái)講基礎(chǔ)鏡像的事情郑兴?
對(duì)于人臉檢測(cè)的應(yīng)用來(lái)說(shuō)犀斋,OpenJDK的官方鏡像是不夠用的,因?yàn)橛胘ava實(shí)現(xiàn)人臉檢測(cè)需要一個(gè)關(guān)鍵技術(shù):<font color="red">javacv</font>情连,在javacv運(yùn)行過(guò)程中需要用到opencv的一些本地庫(kù)叽粹,因此需要在運(yùn)行環(huán)境中安裝opencv
如下圖所示,一個(gè)人臉檢測(cè)應(yīng)用自下而上由以下六部分構(gòu)成却舀,如果將底部的操作系統(tǒng)虫几、JDK、OpenCV做成基礎(chǔ)鏡像就方便多了挽拔,咱們開(kāi)發(fā)應(yīng)用的時(shí)候只要需關(guān)注上面三層即可辆脸,而上面三層不就是個(gè)普通的maven工程嗎?
- 至此篱昔,聰明的您對(duì)接下來(lái)要做的事情應(yīng)該很清楚了:編寫(xiě)Dockerfile文件用來(lái)制作鏡像每强,這個(gè)鏡像中要有<font color="blue">CentOS + JDK1.8 + OpenCV</font>
分兩步走
- 我打算先做一個(gè)CentOS + JDK的鏡像始腾,在此基礎(chǔ)上再做一個(gè)集成了OpenCV的鏡像州刽,這樣在一些不需要OpenCV的場(chǎng)景中,我還能單獨(dú)使用CentOS + JDK1.8的鏡像
- 分析完成浪箭,開(kāi)始動(dòng)手吧
CentOS + JDK的鏡像
之所以選擇CentOS作為操作系統(tǒng)穗椅,是因?yàn)槠綍r(shí)工作中最常用的就是它了
縱觀OpenJDK官方的各種鏡像,并沒(méi)有發(fā)現(xiàn)CentOS作為操作系統(tǒng)的奶栖,因此自己動(dòng)手吧匹表,思路很簡(jiǎn)單:找到OpenJDK的Dockerfile文件门坷,把它的基礎(chǔ)鏡像換成CentOS7.6即可
以下是Dockerfile的所有內(nèi)容,可見(jiàn)核心是根據(jù)不同的操作系統(tǒng)下載文件然后安裝袍镀,邏輯簡(jiǎn)單清晰就不多說(shuō)了:
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
寫(xiě)完之后執(zhí)行<font color="blue">docker build -t bolingcavalry/centos7.6-jdk8:0.0.1 .</font>即可生成鏡像默蚌,如果您有hub.docker.com的賬號(hào),還可以將其推送到中央倉(cāng)庫(kù)苇羡,給更多人使用
用history命令看看鏡像內(nèi)容绸吸,詳情如下,合計(jì)五百多兆设江,已經(jīng)不小了:
CN0014009475M:~ will$ 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
- 我這里已經(jīng)推送到hub.docker.com上去了锦茁,執(zhí)行以下命令即可下載到本地:
docker pull bolingcavalry/centos7.6-jdk8:0.0.3
CentOS+JDK+OpenCV鏡像
- 接下來(lái)可以集成OpenCV了,Dockerfile內(nèi)容如下所示叉存,基礎(chǔ)鏡像是剛剛做好的<font color="blue">bolingcavalry/centos7.6-jdk8:0.0.1</font>码俩,先是安裝一大堆編譯所需的應(yīng)用,然后下載OpenCV-3.4.3版本的源碼歼捏,然后編譯稿存,就這么簡(jiǎn)單(但其間的調(diào)試工作還是不少的,不說(shuō)了瞳秽,說(shuō)多了都是淚):
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 \
cmake \
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 opencv-3.4.3.zip https://codeload.github.com/opencv/opencv/zip/3.4.3; \
unzip opencv-3.4.3.zip; \
rm -rf opencv-3.4.3.zip; \
cd opencv-3.4.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
- 寫(xiě)完之后執(zhí)行<font color="blue">docker build -t bolingcavalry/opencv3.4.3:0.0.3 .</font>即可生成鏡像挠铲,如果您有hub.docker.com的賬號(hào),還可以將其推送到中央倉(cāng)庫(kù)寂诱,給更多人使用
- 用history命令看看鏡像內(nèi)容拂苹,詳情如下,倒吸一口涼氣痰洒,這么大的體積瓢棒,親愛(ài)的讀者們會(huì)不會(huì)打死我...:
CN0014009475M:~ will$ docker history bolingcavalry/opencv3.4.3:0.0.3
IMAGE CREATED CREATED BY SIZE COMMENT
f0306d7a2594 2 days ago /bin/sh -c set -eux; curl -fL -o opencv-… 2.99GB
<missing> 2 days ago /bin/sh -c set -eux; yum install -y … 638MB
<missing> 2 days ago /bin/sh -c echo "export LC_ALL=en_US.UTF-8" … 1.84kB
<missing> 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
- 我這里已經(jīng)推送到hub.docker.com上去了,執(zhí)行以下命令即可下載到本地:
docker pull bolingcavalry/opencv3.4.3:0.0.3
- 這里要提醒一下丘喻,opencv的編譯是非常耗時(shí)的脯宿,請(qǐng)確保docker宿主機(jī)的性能不要太差,也請(qǐng)您耐心等待編譯過(guò)程
- 至此泉粉,符合咱們要求的基礎(chǔ)鏡像<font color="blue">bolingcavalry/opencv3.4.3:0.0.3</font>已制作完成连霉,有了它,咱們的Java應(yīng)用所需的jdk和opencv就湊齊了嗡靡,接下來(lái)的文章咱們一起去開(kāi)發(fā)這個(gè)人臉檢測(cè)的應(yīng)用吧跺撼;
你不孤單,欣宸原創(chuàng)一路相伴
歡迎關(guān)注公眾號(hào):程序員欣宸
微信搜索「程序員欣宸」讨彼,我是欣宸歉井,期待與您一同暢游Java世界...
https://github.com/zq2599/blog_demos