使用Dockerfile建立javaweb映象
阿新 • • 發佈:2018-12-26
一、在centos映象上進行操作
1.在宿主機的一個目錄下建立檔案Dockerfile,如:/root/static_web,並在該目錄下放專案的war包。
Dockerfile的內容:
FROM centos:latest
MAINTAINER panjq
WORKDIR /home
RUN mkdir JDK
COPY jdk-8u91-linux-x64.tar.gz /home/
RUN tar zxf /home/jdk-8u91-linux-x64.tar.gz -C /home/JDK && rm -rf /home/jdk-8u91-linux-x64.tar.gz
ENV JAVA_HOME /home/JDK/jdk1.8.0_91
ENV PATH $PATH:$JAVA_HOME/bin
RUN mkdir Tomcat
COPY apache-tomcat-8.5.20.tar.gz /home/
RUN tar zxf /home/apache-tomcat-8.5.20.tar.gz -C /home/Tomcat && rm -rf /home/apache-tomcat-8.5.20.tar.gz
COPY RobaseServer.war /home/Tomcat/apache-tomcat-8.5.20/webapps/
EXPOSE 8080
ENTRYPOINT /home/Tomcat/apache-tomcat-8.5.20/bin/startup.sh && tail -f /home/Tomcat/apache-tomcat-8.5.20/logs/catalina.out
2.進入目錄/root/static_web,執行指令:
docker build -t panjq/tomcat_test . //生成映象panjq/tomcat_test
然後執行指令:
docker run -it --name test_tomcat -p 58080:8080 panjq/tomcat_test /bin/bash //執行映象panjq/tomcat_test生成容器test_tomcat
3.呼叫http://192.168.103.209:58080/RobaseServer/File?fileid=9108315258952604735資源,得到結果:
{"Attribute":{"filename":"ss","filetypeid":2,"parentid":0,"fileid":9108315258952604735},"Data":{"password":886263699231969848,"isadmin":1,"loginname":"ss","userid":9108315258952604735}}
測試成功。
4.將建立的java web映象push到docker hub上:
首先停止容器:docker stop
docker tag <existing-image> <hub-user>/<repo-name>[:<tag>]
如:docker tag imageid panjq/tomcat_test:1.0
docker login
docker push <hub-user>/<repo-name>:<tag>
如: docker push panjq/tomcat_test:1.0
將映象從docker hub上拉下來:
docker pull panjq/tomcat_test:1.0
二、直接在tomcat映象上進行操作
Dockerfile的內容:
FROM tomcat:latest
MAINTAINER panjq
WORKDIR /usr
COPY RobaseServer.war /usr/local/tomcat/webapps/
EXPOSE 8080
ENTRYPOINT /usr/local/tomcat/bin/startup.sh && tail -f /usr/local/tomcat/logs/catalina.out
其他操作類似在centos映象上進行操作。
1.在宿主機的一個目錄下建立檔案Dockerfile,如:/root/static_web,並在該目錄下放專案的war包。
Dockerfile的內容:
FROM centos:latest
MAINTAINER panjq
WORKDIR /home
RUN mkdir JDK
COPY jdk-8u91-linux-x64.tar.gz /home/
RUN tar zxf /home/jdk-8u91-linux-x64.tar.gz -C /home/JDK && rm -rf /home/jdk-8u91-linux-x64.tar.gz
ENV JAVA_HOME /home/JDK/jdk1.8.0_91
ENV PATH $PATH:$JAVA_HOME/bin
RUN mkdir Tomcat
COPY apache-tomcat-8.5.20.tar.gz /home/
RUN tar zxf /home/apache-tomcat-8.5.20.tar.gz -C /home/Tomcat && rm -rf /home/apache-tomcat-8.5.20.tar.gz
COPY RobaseServer.war /home/Tomcat/apache-tomcat-8.5.20/webapps/
EXPOSE 8080
ENTRYPOINT /home/Tomcat/apache-tomcat-8.5.20/bin/startup.sh && tail -f /home/Tomcat/apache-tomcat-8.5.20/logs/catalina.out
2.進入目錄/root/static_web,執行指令:
docker build -t panjq/tomcat_test . //生成映象panjq/tomcat_test
然後執行指令:
docker run -it --name test_tomcat -p 58080:8080 panjq/tomcat_test /bin/bash //執行映象panjq/tomcat_test生成容器test_tomcat
3.呼叫http://192.168.103.209:58080/RobaseServer/File?fileid=9108315258952604735資源,得到結果:
{"Attribute":{"filename":"ss","filetypeid":2,"parentid":0,"fileid":9108315258952604735},"Data":{"password":886263699231969848,"isadmin":1,"loginname":"ss","userid":9108315258952604735}}
測試成功。
4.將建立的java web映象push到docker hub上:
首先停止容器:docker stop
docker tag <existing-image> <hub-user>/<repo-name>[:<tag>]
如:docker tag imageid panjq/tomcat_test:1.0
docker login
docker push <hub-user>/<repo-name>:<tag>
如: docker push panjq/tomcat_test:1.0
將映象從docker hub上拉下來:
docker pull panjq/tomcat_test:1.0
二、直接在tomcat映象上進行操作
Dockerfile的內容:
FROM tomcat:latest
MAINTAINER panjq
WORKDIR /usr
COPY RobaseServer.war /usr/local/tomcat/webapps/
EXPOSE 8080
ENTRYPOINT /usr/local/tomcat/bin/startup.sh && tail -f /usr/local/tomcat/logs/catalina.out
其他操作類似在centos映象上進行操作。