這篇“如何用Dockerfile制作java運行環(huán)境的鏡像”文章的知識點大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“如何用Dockerfile制作java運行環(huán)境的鏡像”文章吧。
連平網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)公司等網(wǎng)站項目制作,到程序開發(fā),運營維護。成都創(chuàng)新互聯(lián)從2013年開始到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設(shè)就選成都創(chuàng)新互聯(lián)。
當(dāng)前用到的環(huán)境是:
centos 7.5
docker-ce 18.06.1-ce
1、先用centos7.5.1804的基礎(chǔ)鏡像把一些運行需要的環(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的鏡像
進入之前建好的/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暴露端口,在物理機上測試下
[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.
在客戶端用瀏覽器測試
以上就是關(guān)于“如何用Dockerfile制作java運行環(huán)境的鏡像”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。