Dockerfile中很多命令都很相似,我们需要了解它们的区别,最好的学习方法就是对他们进行对比测试。
实战:Tomcat镜像
1、准备镜像文件tomcat 压缩包,jdk的压缩包
2、编写dockerfile 文件,官方命名Dockerfile, build 会自动寻找这个文件,就可以不需要-f指定了
在mac系统环境,能够执行
FROM centos
MAINTAINER bruce_liu<1134593154@qq.com>
COPY readme.txt /usr/local/readme.txt
ADD jdk-8u261-macosx-x64.dmg.zip /usr/local/
ADD apache-tomcat-9.0.56.tar.gz /usr/local/
RUN yum -y install vim
ENV MYPATH /usr/local
WORKDIR $MYPATH
ENV JAVA_HOME /usr/local/jdk1.8.0_261
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.56
ENV CATALINA_BASH /usr/local/apache-tomcat-9.0.56
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
EXPOSE 8080
CMD /usr/local/apache-tomcat-9.0.56/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.56/logs/catalina.out
# 在centos7系统测试
FROM centos
MAINTAINER bruce_liu<1134593154@qq.com>
COPY readme.txt /usr/local/readme.txt
ADD jdk-17_linux-x64_bin.tar.gz /usr/local/
ADD apache-tomcat-8.5.33.tar.gz /usr/local/
RUN yum -y install vim
ENV MYPATH /usr/local
WORKDIR $MYPATH
ENV JAVA_HOME /usr/local/jdk-17.0.1
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-8.5.33
ENV CATALINA_BASH /usr/local/apache-tomcat-8.5.33
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
EXPOSE 8080
CMD /usr/local/apache-tomcat-8.5.33/bin/startup.sh && tail -F /usr/local/apache-tomcat-8.5.33/logs/catalina.out
3、构建镜像
# 由于是官方标准命名Dockerfile,所以不用添加参数-f 最后一定要加上.
# 在mac系统环境能够执行,正常生成镜像,但是按结尾提示执行docker scan会报错。
# 如果在centos7环境一切正常,目前没有找到解决办法
root@liuxiaoweideAir tomcat # docker build -t diytomcat .
[+] Building 83.8s (11/11) FINISHED
=> [internal] load build definition from Dockerfile 0.3s
=> => transferring dockerfile: 681B 0.0s
=> [internal] load .dockerignore 0.4s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/centos:latest 0.0s
=> CACHED [1/6] FROM docker.io/library/centos 0.0s
=> [internal] load build context 24.9s
=> => transferring context: 213.39MB 24.7s
=> [2/6] COPY readme.txt /usr/local/readme.txt 0.7s
=> [3/6] ADD jdk-8u261-macosx-x64.dmg.zip /usr/local/ 1.8s
=> [4/6] ADD apache-tomcat-9.0.56.tar.gz /usr/local/ 2.4s
=> [5/6] RUN yum -y install vim 44.7s
=> [6/6] WORKDIR /usr/local 0.8s
=> exporting to image 7.2s
=> => exporting layers 7.1s
=> => writing image sha256:ab919c75922607c3239f6634b06651870d267a4a94589c82ccd319fda610b882 0.0s
=> => naming to docker.io/library/diytomcat 0.0s
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
root@liuxiaoweideAir tomcat # docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
diytomcat latest ab919c759226 About a minute ago 527MB
# 在centos7测试
# 由于是官方标准命名Dockerfile,所以不用添加参数-f 最后一定要加上.
[root@localhost tomcat]# docker build -t diytomcat .
Sending build context to Docker daemon 190.3MB
Step 1/15 : FROM centos
---> 5d0da3dc9764
Step 2/15 : MAINTAINER bruce_liu<1134593154@qq.com>
---> Running in b22cb9028743
Removing intermediate container b22cb9028743
---> be05f2b3b86b
Step 3/15 : COPY readme.txt /usr/local/readme.txt
---> 1026e7aab5a8
Step 4/15 : ADD jdk-17_linux-x64_bin.tar.gz /usr/local/
---> de54e65e0916
Step 5/15 : ADD apache-tomcat-8.5.33.tar.gz /usr/local/
---> 5f002ff16d92
Step 6/15 : RUN yum -y install vim
---> Running in 2622ef5a0401
CentOS Linux 8 - AppStream 4.3 MB/s | 8.4 MB 00:01
CentOS Linux 8 - BaseOS 875 kB/s | 4.6 MB 00:05
CentOS Linux 8 - Extras 13 kB/s | 10 kB 00:00
Dependencies resolved.
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
vim-enhanced x86_64 2:8.0.1763-16.el8 appstream 1.4 M
Installing dependencies:
gpm-libs x86_64 1.20.7-17.el8 appstream 39 k
vim-common x86_64 2:8.0.1763-16.el8 appstream 6.3 M
vim-filesystem noarch 2:8.0.1763-16.el8 appstream 49 k
which x86_64 2.21-16.el8 baseos 49 k
Transaction Summary
================================================================================
Install 5 Packages
Total download size: 7.8 M
Installed size: 30 M
Downloading Packages:
(1/5): gpm-libs-1.20.7-17.el8.x86_64.rpm 355 kB/s | 39 kB 00:00
(2/5): vim-filesystem-8.0.1763-16.el8.noarch.rp 1.1 MB/s | 49 kB 00:00
(3/5): vim-enhanced-8.0.1763-16.el8.x86_64.rpm 1.7 MB/s | 1.4 MB 00:00
(4/5): vim-common-8.0.1763-16.el8.x86_64.rpm 5.8 MB/s | 6.3 MB 00:01
(5/5): which-2.21-16.el8.x86_64.rpm 44 kB/s | 49 kB 00:01
--------------------------------------------------------------------------------
Total 2.1 MB/s | 7.8 MB 00:03
warning: /var/cache/dnf/appstream-02e86d1c976ab532/packages/gpm-libs-1.20.7-17.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY
CentOS Linux 8 - AppStream 66 kB/s | 1.6 kB 00:00
Importing GPG key 0x8483C65D:
Userid : "CentOS (CentOS Official Signing Key) <security@centos.org>"
Fingerprint: 99DB 70FA E1D7 CE22 7FB6 4882 05B5 55B3 8483 C65D
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : which-2.21-16.el8.x86_64 1/5
Installing : vim-filesystem-2:8.0.1763-16.el8.noarch 2/5
Installing : vim-common-2:8.0.1763-16.el8.x86_64 3/5
Installing : gpm-libs-1.20.7-17.el8.x86_64 4/5
Running scriptlet: gpm-libs-1.20.7-17.el8.x86_64 4/5
Installing : vim-enhanced-2:8.0.1763-16.el8.x86_64 5/5
Running scriptlet: vim-enhanced-2:8.0.1763-16.el8.x86_64 5/5
Running scriptlet: vim-common-2:8.0.1763-16.el8.x86_64 5/5
Verifying : gpm-libs-1.20.7-17.el8.x86_64 1/5
Verifying : vim-common-2:8.0.1763-16.el8.x86_64 2/5
Verifying : vim-enhanced-2:8.0.1763-16.el8.x86_64 3/5
Verifying : vim-filesystem-2:8.0.1763-16.el8.noarch 4/5
Verifying : which-2.21-16.el8.x86_64 5/5
Installed:
gpm-libs-1.20.7-17.el8.x86_64 vim-common-2:8.0.1763-16.el8.x86_64
vim-enhanced-2:8.0.1763-16.el8.x86_64 vim-filesystem-2:8.0.1763-16.el8.noarch
which-2.21-16.el8.x86_64
Complete!
Removing intermediate container 2622ef5a0401
---> 57606bc79c81
Step 7/15 : ENV MYPATH /usr/local
---> Running in a2c900e33b46
Removing intermediate container a2c900e33b46
---> c536dc330700
Step 8/15 : WORKDIR $MYPATH
---> Running in 70aede458361
Removing intermediate container 70aede458361
---> a4757b245dd7
Step 9/15 : ENV JAVA_HOME /usr/local/jdk-17.0.1
---> Running in cbdbbd45a77d
Removing intermediate container cbdbbd45a77d
---> 29a5fec095c0
Step 10/15 : ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
---> Running in 44cc4bc03fff
Removing intermediate container 44cc4bc03fff
---> 7ee1d774dec6
Step 11/15 : ENV CATALINA_HOME /usr/local/apache-tomcat-8.5.33
---> Running in 7aedcdacefca
Removing intermediate container 7aedcdacefca
---> d51a4835e53b
Step 12/15 : ENV CATALINA_BASH /usr/local/apache-tomcat-8.5.33
---> Running in c5545d41cfe0
Removing intermediate container c5545d41cfe0
---> 3ca740e8fb50
Step 13/15 : ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
---> Running in 11e9db1569cc
Removing intermediate container 11e9db1569cc
---> fa987bac62df
Step 14/15 : EXPOSE 8080
---> Running in c3f3b3bf9010
Removing intermediate container c3f3b3bf9010
---> a9f6ace395f0
Step 15/15 : CMD /usr/local/apache-tomcat-8.5.33/bin/startup.sh && tail -F /usr/local/apache-tomcat-8.5.33/logs/catalina.out
---> Running in 5f8629ecaac4
Removing intermediate container 5f8629ecaac4
---> c5e5274bde3b
Successfully built c5e5274bde3b
Successfully tagged diytomcat:latest
4、启动镜像
# 在centos7环境成功
[root@localhost tomcat]# docker run -d -p 9090:8080 --name bruceliutomcat -v /home/bruce_liu/build/tomcat/test:/usr/local/apache-tomcat-8.5.33/webapps/test -v /home/bruce_liu/build/tomcat/tomcatlogs:/usr/local/apache-tomcat-8.5.33/logs diytomcat
97d08b3ba87ae920cf3fdabfa6b03e9e827310b79c3e9f14db471a1ea65b499f
5、访问测试
6、发布项目(由于做了卷挂载,我们直接在本地编写项目就可以发布)
# 例如在test文件夹下建立WEB-INFO文件夹,然后编辑web.xml
<html>
<body>
<script>
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","cd_catalog.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
document.write("<table border='1'>");
var x=xmlDoc.getElementsByTagName("CD");
for (i=0;i<x.length;i++)
{
document.write("<tr><td>");
document.write(x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue);
document.write("</td><td>");
document.write(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
document.write("</td></tr>");
}
document.write("</table>");
</script>
</body>
</html>
# 在宿主机的test路径下建一个文件index.jsp
# 在外网访问成功
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Bruce_liu</title>
</head>
<body>
Hello Bruce_liu!<br/>
<%
out.println("你的 IP 地址 " + request.getRemoteAddr());
%>
</body>
</html>
结果:项目部署成功,可以直接访问!✌️
题外话:
假设以后搞开发,那么需要掌握Dockerfile的编写,所有项目一律都使用docker镜像发布运行!