這篇文章主要介紹如何利用Dockerfile制作java運(yùn)行環(huán)境的鏡像,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!
10年積累的網(wǎng)站設(shè)計、網(wǎng)站建設(shè)經(jīng)驗(yàn),可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識你,你也不認(rèn)識我。但先網(wǎng)站制作后付款的網(wǎng)站建設(shè)流程,更有東安免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。當(dāng)前用到的環(huán)境是:
Centos 7.5
docker-ce 18.06.1-ce
1、先用centos7.5.1804的基礎(chǔ)鏡像把一些運(yùn)行需要的環(huán)境裝好
在/app目錄下創(chuàng)建相對應(yīng)的文件目錄
[root@node2 /app/]# mkdir dockerfile/{web/{nginx,tomcat,jdk,apache},system/{centos,ubuntu,redhat}} -pv [root@node2 /app]# cd dockerfile/system/centos/ [root@node2 /app/dockerfile/system/centos]# mkdir centos-7.5-base [root@node2 /app/dockerfile/system/centos]# cd centos-7.5-base
創(chuàng)建一個Dockerfile文件
[root@node2 /app/dockerfile/system/centos/centos-7.5-base]#vim Dockerfile #Nginx Base Image FROM centos:7.5.1804 LABEL maintaier "mr.luo" RUN yum install -y vim wget pcre pcre-devel zlib zlib-devel openssl openssl-devel iproute net-tools iotop
創(chuàng)建一個用于直接docker build腳本后續(xù)直接執(zhí)行腳本即可
[root@node2 /app/dockerfile/system/centos/centos-7.5-base]#vim build-command.sh #!/bin/bash docker build -t 172.20.7.50/baseimages/centos-base:7.5.1804 .
執(zhí)行腳本創(chuàng)建新鏡像
[root@node2 /app/dockerfile/system/centos/centos-7.5-base]#bash build-command.sh Sending build context to Docker daemon 3.072kB Step 1/3 : FROM centos:7.5.1804 ---> 76d6bc25b8a5 Step 2/3 : LABEL maintaier 'mr.luo@dklwj.com' ---> Using cache ---> 05ccd970d71d Step 3/3 : RUN yum install -y vim wget pcre pcre-devel zlib zlib-devel openssl openssl-devel iproute net-tools iotop ---> Using cache ---> 73d683a54877 Successfully built 73d683a54877 Successfully tagged 172.20.7.50/baseimages/centos-base:7.5.1804
2、利用制作好的基礎(chǔ)環(huán)境鏡像制作jdk的鏡像
從centos-7.5-base退出來在當(dāng)前目錄創(chuàng)建一個jdk1.8的目錄.
[root@node2 /app/dockerfile/system/centos/centos-7.5-base]# cd .. [root@node2 /app/dockerfile/system/centos]# mkdir jdk1.8 [root@node2 /app/dockerfile/system/centos]# cd jdk1.8/
創(chuàng)建Dockerfile
[root@node2 /app/dockerfile/system/centos/jdk1.8]#vim Dockerfile FROM 172.20.7.50/baseimages/centos-base:7.5.1804 LABEL maintainer "mr.luo" ADD jdk-8u162-linux-x64.tar.gz /usr/local/src/ RUN ln -s /usr/local/src/jdk1.8.0_162/ /usr/local/jdk ADD profile /etc/profile ENV JAVA_HOME /usr/local/jdk ENV JRE_HOME $JAVA_HOME/jre ENV CLASSPATH $JAVA_HOME/lib/:$JRE_HOME/lib/ ENV PATH $PATH:$JAVA_HOME/bin RUN rm -rf /etc/localtime && ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo "Asia/Shanghai" > /etc/timezone
上傳jdk包到當(dāng)前目錄下:
復(fù)制/etc/profile文件到當(dāng)前目錄
[root@node2 /app/dockerfile/system/centos/jdk1.8]#cp profile /etc/profile
在profile最后添加jdk的環(huán)境變量
[root@node2 /app/dockerfile/system/centos/jdk1.8]#vim profile export JAVA_HOME=/usr/local/jdk export TOMCAT_HOME=/apps/tomcat export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$TOMCAT_HOME/bin:$PATH export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar
創(chuàng)建docker build的shell腳本
[root@node2 /app/dockerfile/system/centos/jdk1.8]#vim build-command.sh #!/bin/bash # docker build -t 172.20.7.50/baseimages/centos7.5-jdk:8.162 .
開始制作鏡像
[root@node2 /app/dockerfile/system/centos/jdk1.8]#bash build-command.sh Sending build context to Docker daemon 189.8MB Step 1/10 : FROM 172.20.7.50/baseimages/centos-base:7.5.1804 ---> 73d683a54877 Step 2/10 : LABEL maintainer "mr.luo" ---> Running in 65604dd1f392 Removing intermediate container 65604dd1f392 ---> c4720603ce38 Step 3/10 : ADD jdk-8u162-linux-x64.tar.gz /usr/local/src/ ---> bc98adffe1b4 Step 4/10 : RUN ln -s /usr/local/src/jdk1.8.0_162/ /usr/local/jdk ---> Running in df5a6f67f9fd Removing intermediate container df5a6f67f9fd ---> 0ae1af0416c6 Step 5/10 : ADD profile /etc/profile ---> eee23a69c0c8 Step 6/10 : ENV JAVA_HOME /usr/local/jdk ---> Running in edbef8563e83 Removing intermediate container edbef8563e83 ---> 5f783f642054 Step 7/10 : ENV JRE_HOME $JAVA_HOME/jre ---> Running in fa0e5f08e732 Removing intermediate container fa0e5f08e732 ---> 28a4d31463d4 Step 8/10 : ENV CLASSPATH $JAVA_HOME/lib/:$JRE_HOME/lib/ ---> Running in 3c4ebb04ac62 Removing intermediate container 3c4ebb04ac62 ---> 245f2dd82d52 Step 9/10 : ENV PATH $PATH:$JAVA_HOME/bin ---> Running in 4f5e6093f0a9 Removing intermediate container 4f5e6093f0a9 ---> 5be0e6261eea Step 10/10 : RUN rm -rf /etc/localtime && ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo "Asia/Shanghai" > /etc/timezone ---> Running in 52d8cb8463a8 Removing intermediate container 52d8cb8463a8 ---> 9fb867ae8c39 Successfully built 9fb867ae8c39 Successfully tagged 172.20.7.50/baseimages/centos7.5-jdk:8.162
查看當(dāng)前目錄下的文件
[root@node2 /app/dockerfile/system/centos/jdk1.8]#ls build-command.sh Dockerfile jdk-8u162-linux-x64.tar.gz profile
檢查制作好的鏡像是否能正常使用
[root@node2 /app/dockerfile/system/centos/jdk1.8]#docker run -it --rm 172.20.7.50/baseimages/centos7.5-jdk:8.162 bash [root@919844b164dc /]# java -version java version "1.8.0_162" Java(TM) SE Runtime Environment (build 1.8.0_162-b12) Java HotSpot(TM) 64-Bit Server VM (build 25.162-b12, mixed mode) [root@919844b164dc /]# date Thu Nov 22 21:17:49 CST 2018 [root@919844b164dc /]# exit exit
3、做Tomcat的鏡像
進(jìn)入之前建好的/app/dockerfile/web/tomcat下,創(chuàng)建一個tomcat-base目錄
[root@node2 ~]# cd /app/dockerfile/web/tomcat [root@node2 /app/dockerfile/web/tomcat]#mkdir tomcat-base
創(chuàng)建Dockerfile
[root@node2 /app/dockerfile/web/tomcat/tomcat-base]#vim Dockerfile FROM 172.20.7.50/baseimages/centos7.5-jdk:8.162 LABEL maintainer "mr.luo" RUN mkdir /apps ADD apache-tomcat-8.5.33.tar.gz /apps RUN ln -s /apps/apache-tomcat-8.5.33 /apps/tomcat
創(chuàng)建docker build腳本
[root@node2 /app/dockerfile/web/tomcat/tomcat-base]#vim build-command.sh #!/bin/bash docker build -t 172.20.7.50/baseimages/centos-tomcat:8.5.33 .
執(zhí)行創(chuàng)建鏡像文件:
[root@node2 /app/dockerfile/web/tomcat/tomcat-base]#bash build-command.sh Sending build context to Docker daemon 9.625MB Step 1/5 : FROM 172.20.7.50/baseimages/centos7.5-jdk:8.162 ---> 9fb867ae8c39 Step 2/5 : LABEL maintainer "mr.luo" ---> Running in 9ce6fc4d2850 Removing intermediate container 9ce6fc4d2850 ---> b68755061b28 Step 3/5 : RUN mkdir /apps ---> Running in b483c6b127f2 Removing intermediate container b483c6b127f2 ---> 605c1a048a5f Step 4/5 : ADD apache-tomcat-8.5.33.tar.gz /apps ---> 3c44f96ed41c Step 5/5 : RUN ln -s /apps/apache-tomcat-8.5.33 /apps/tomcat ---> Running in 4c1aa39a6c92 Removing intermediate container 4c1aa39a6c92 ---> 9b3bc4f58cc9 Successfully built 9b3bc4f58cc9 Successfully tagged 172.20.7.50/baseimages/centos-tomcat:8.5.33
利用創(chuàng)建好的鏡像起一個容器檢查下是否成功制作好,啟動時加上-p暴露端口,在物理機(jī)上測試下
[root@node2 /app/dockerfile/web/tomcat/tomcat-base]#docker run -it -p 8802:8080 172.20.7.50/baseimages/centos-tomcat:8.5.33 bash [root@917b2c2262a3 /]# cd /apps/ [root@917b2c2262a3 apps]# ll total 0 drwxr-xr-x 9 root root 220 Nov 22 22:08 apache-tomcat-8.5.33 lrwxrwxrwx 1 root root 26 Nov 22 22:08 tomcat -> /apps/apache-tomcat-8.5.33 [root@917b2c2262a3 apps]# ./tomcat/bin/catalina.sh start Using CATALINA_BASE: /apps/tomcat Using CATALINA_HOME: /apps/tomcat Using CATALINA_TMPDIR: /apps/tomcat/temp Using JRE_HOME: /usr/local/jdk/jre Using CLASSPATH: /apps/tomcat/bin/bootstrap.jar:/apps/tomcat/bin/tomcat-juli.jar Tomcat started.
在客戶端用瀏覽器測試
以上是“如何利用Dockerfile制作java運(yùn)行環(huán)境的鏡像”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計公司行業(yè)資訊頻道!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。