Dockerfile体系结构之保留字指令
保留字指令案例Base镜像(scratch)自定义镜像mycentos案例CMD/ENTRYPOINT镜像案例CMDENTRYPOINT ONBUILD案例自定义镜像Tomcat9案例 总结保留字指令
FROM:基础镜像,当前新镜像是基于哪个镜像的。MAINTAINER:镜像维护者的姓名和邮箱地址。RUN:容器构建时需要运行的命令。EXPOSE:当前容器对外暴露出的端口。VOLUME:容器数据卷,用于数据保存和持久化工作。WORKDIR:指定工作目录。指定在创建容器后,终端默认登录进容器后的工作目录。ENV:用来在构建镜像过程中设置环境变量。ARG:构建参数,与ENV作用一致。不过作用域不一样。ARG设置的环境变量仅对Dockerfile内有效,只有docker build的过程中有效,构建好的镜像内不存在此环境变量。ADD:ADD指令和COPY的使用类似(同样需求下,官方推荐使用COPY)将宿主机目录下的文件拷贝进镜像,且ADD命令会自动处理URL和tar压缩包。区别:ADD的优点:在执行<源文件>为tar压缩文件的话,压缩格式为gzip,bzip2及xz的情况下,会自动复制并解压到<目标路径>。 ADD的缺点:在不解压的前提下,无法复制tar压缩文件,会令镜像构建缓存失效,从而可能会令镜像构建变得比较缓慢。
COPY:复制命令,从上下文目录中复制文件或者目录到容器里的指定路径。CMD:类似于run指令,用于指定一个容器启动时运行的命令,但二者运行的时间点不同:CMD:在docker run 时运行。
RUN:在docker build 。
CMD会被docker run之后的参数替换。
如果 Dockerfile 中如果存在多个 CMD 指令,仅最后一个生效。
ENTRYPOINT:类似于CMD指令,但其不会被docker run命令行参数指定的指令所覆盖,而且这些命令行参数会被当作参数追加给ENTRYPOINT指令指定的程序。
如果 Dockerfile 中如果存在多个 ENTRYPOINT 指令,仅最后一个生效。ONBUILD:用于延迟构建命令的执行。当构建一个被继承的Dockerfile时运行命令,父镜像在被子继承后父镜像的onbuild被触发。USER:用于指定执行后续命令的用户和用户组,这边只是切换后续命令执行的用户(用户和用户组必须提前已经存在)。HEALTHCHECK:用于指定某个程序或着指令来监控docker容器服务的运行状态。LABEL:LABEL指令用来给镜像添加一些元数据(metadata),以键值对的形式。.dockerignore:忽略掉你不想追踪的文件。.dockerignore 文件的作用
Dockerfile参考文档
案例
Base镜像(scratch)
Docker hub中 99%的镜像都是通过在base镜像中安装和配置需要的软件构建出来的。
自定义镜像mycentos案例
自定义mycentos目的是我们自己的镜像具备如下:
登录后的默认路径vim编辑器查看网络配置ifconfig支持
1、编写
#Dockerfile文件
FROM centos
MAINTAINER yyds<123@>
ENV MYPATH /usr/local
WORKDIR $MYPATH
RUN yum -y install vim
RUN yum -y install net-tools
EXPOSE 80
CMD echo $MYPATH
CMD echo "success------------>>ok"
CMD /bin/bash
2、构建
docker build -f Dockerfile文件名 -t 新镜像名字:TAG .
3、运行
docker images 新镜像名字:TAG
docker run -it 新镜像名字:TAG
4、列出镜像的变更历史
docker history 镜像ID
构建镜像
PS D:\opt\work> docker build -f .\Dockerfile -t mycentos:2.0 .[+] Building 24.9s (8/8) FINISHED=> [internal] load build definition from Dockerfile0.0s=> => transferring dockerfile: 265B 0.0s=> [internal] load .dockerignore 0.0s=> => transferring context: 2B0.0s=> [internal] load metadata for docker.io/library/centos:latest 0.0s=> CACHED [1/4] FROM docker.io/library/centos 0.0s=> [2/4] WORKDIR /usr/local 0.0s=> [3/4] RUN yum -y install vim 20.9s=> [4/4] RUN yum -y install net-tools 3.2s=> exporting to image0.7s=> => exporting layers 0.7s=> => writing image sha256:58560ac56116cdcc750f20b40a2edef2b77ac5598c4e752b651a67a25a076b9b0.0s=> => naming to docker.io/library/mycentos:2.00.0sUse 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them-------------------查看构建的镜像----------------------------PS D:\opt\work> docker images mycentosREPOSITORY TAG IMAGE ID CREATED SIZEmycentos2.0 58560ac56116 2 minutes ago 311MB---------------------------------查看镜像功能是否制作成功-------------------------------------------------------PS D:\opt\work> docker run -it -d mycentos:2.0155b6b5d5e34a3d5d37d11d9b942fb5ce2b9b9efe03c3419a92c49509c964af5PS D:\opt\work> docker psCONTAINER ID IMAGECOMMAND CREATED STATUS PORTSNAMES155b6b5d5e34 mycentos:2.0 "/bin/sh -c /bin/bash" 4 seconds ago Up 3 seconds 80/tcp compassionate_joliotPS D:\opt\work> docker exec -it 155b6b5d5e34 bash[root@155b6b5d5e34 local]# pwd/usr/local[root@155b6b5d5e34 local]# ifconfigeth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet)RX packets 16 bytes 1312 (1.2 KiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 0 bytes 0 (0.0 B)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536inet 127.0.0.1 netmask 255.0.0.0loop txqueuelen 1000 (Local Loopback)RX packets 0 bytes 0 (0.0 B)RX errors 0 dropped 0 overruns 0 frame 0TX packets 0 bytes 0 (0.0 B)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0[root@155b6b5d5e34 local]# lsbin etc games include lib lib64 libexec sbin share src[root@155b6b5d5e34 local]# touch 1.txt[root@155b6b5d5e34 local]# echo 123 > 1.txt[root@155b6b5d5e34 local]# vim 1.txt-------------------------列出镜像的变更历史------------------------------------------------------PS D:\opt\work> docker images mycentosREPOSITORY TAG IMAGE ID CREATEDSIZEmycentos2.0 58560ac56116 3 days ago 311MBPS D:\opt\work> docker history 58560ac56116IMAGECREATED CREATED BY SIZECOMMENT58560ac56116 3 days agoCMD ["/bin/sh" "-c" "/bin/bash"]0B buildkit.dockerfile.v0<missing>3 days agoCMD ["/bin/sh" "-c" "echo \"success---------… 0B buildkit.dockerfile.v0<missing>3 days agoCMD ["/bin/sh" "-c" "echo $MYPATH"] 0B buildkit.dockerfile.v0<missing>3 days agoEXPOSE map[80/tcp:{}] 0B buildkit.dockerfile.v0<missing>3 days agoRUN /bin/sh -c yum -y install net-tools # bu… 14.8MB buildkit.dockerfile.v0<missing>3 days agoRUN /bin/sh -c yum -y install vim # buildkit 64.8MB buildkit.dockerfile.v0<missing>3 days agoWORKDIR /usr/local0B buildkit.dockerfile.v0<missing>3 days agoENV MYPATH=/usr/local 0B buildkit.dockerfile.v0<missing>3 days agoMAINTAINER yyds<tongh852@>0B buildkit.dockerfile.v0<missing>3 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B<missing>3 months ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B<missing>3 months ago /bin/sh -c #(nop) ADD file:805cb5e15fb6e0bb0… 231MB
CMD/ENTRYPOINT镜像案例
相同点:都是指定一个容器启动时要运行的命令
不同点:CMD在多个CDM命令时,最后一条CMD会把之前其他的CMD命令覆盖执行最后一条。ENTRYPOINT在多条命令时,后边的命令参数会被追加执行。
CMD
Dockerfile中可以有多个CMD指令,但只有最后一个生效,CMD会被docker run之后的参数替换。
case
Tomcat为例演示
首先拉取一个Tomcat镜像。
docker pull tomcat
执行启动命令并映射出端口
docker run -it -p 7777:8080 tomcat
后台会输出启动日志,Dockerfile里边最后会有一行
CMD ["catalina.sh","run"]
最后会执行以上CMD命令,启动并输出日志。
PS D:\opt\work> docker run -it tomcatUsing CATALINA_BASE: /usr/local/tomcatUsing CATALINA_HOME: /usr/local/tomcatUsing CATALINA_TMPDIR: /usr/local/tomcat/tempUsing JRE_HOME: /usr/local/openjdk-11Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jarUsing CATALINA_OPTS:NOTE: Picked up JDK_JAVA_OPTIONS: --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.util.concurrent=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED27-Dec- 08:15:02.977 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version name: Apache Tomcat/10.0.1327-Dec- 08:15:02.982 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built:Nov 9 22:12:58 UTC27-Dec- 08:15:02.982 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version number: 10.0.13.027-Dec- 08:15:02.983 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name:Linux27-Dec- 08:15:02.984 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version: 5.10.16.3-microsoft-standard-WSL227-Dec- 08:15:02.984 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture:amd6427-Dec- 08:15:02.984 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home: /usr/local/openjdk-1127-Dec- 08:15:02.984 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version: 11.0.13+827-Dec- 08:15:02.984 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor: Oracle Corporation27-Dec- 08:15:02.985 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE: /usr/local/tomcat27-Dec- 08:15:02.985 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME: /usr/local/tomcat27-Dec- 08:15:03.009 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.lang=ALL-UNNAMED27-Dec- 08:15:03.009 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.io=ALL-UNNAMED27-Dec- 08:15:03.010 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.util=ALL-UNNAMED27-Dec- 08:15:03.010 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.util.concurrent=ALL-UNNAMED27-Dec- 08:15:03.010 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED27-Dec- 08:15:03.010 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties27-Dec- 08:15:03.010 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager27-Dec- 08:15:03.011 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=204827-Dec- 08:15:03.011 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources27-Dec- 08:15:03.011 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dorg.apache.catalina.security.SecurityListener.UMASK=002727-Dec- 08:15:03.011 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs=27-Dec- 08:15:03.011 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/usr/local/tomcat27-Dec- 08:15:03.011 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/usr/local/tomcat27-Dec- 08:15:03.011 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/usr/local/tomcat/temp27-Dec- 08:15:03.026 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded Apache Tomcat Native library [1.2.31] using APR version [1.7.0].27-Dec- 08:15:03.026 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true], UDS [true].27-Dec- 08:15:03.037 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized [OpenSSL 1.1.1k 25 Mar ]27-Dec- 08:15:03.501 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]27-Dec- 08:15:03.555 INFO [main] org.apache.catalina.startup.Catalina.load Server initialization in [869] milliseconds27-Dec- 08:15:03.654 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]27-Dec- 08:15:03.654 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet engine: [Apache Tomcat/10.0.13]27-Dec- 08:15:03.673 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]27-Dec- 08:15:03.698 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in [141] milliseconds^C27-Dec- 08:15:09.779 INFO [Thread-2] org.apache.coyote.AbstractProtocol.pause Pausing ProtocolHandler ["http-nio-8080"]27-Dec- 08:15:09.786 INFO [Thread-2] org.apache.catalina.core.StandardService.stopInternal Stopping service [Catalina]27-Dec- 08:15:09.790 INFO [Thread-2] org.apache.coyote.AbstractProtocol.stop Stopping ProtocolHandler ["http-nio-8080"]27-Dec- 08:15:09.795 INFO [Thread-2] org.apache.coyote.AbstractProtocol.destroy Destroying ProtocolHandler ["http-nio-8080"]
如果在启动命令后边加上ls -l,就类似于在Dockerfile文件里边加了一行CMD命令,如下。但是Tomcat并未启动。
CMD ["catalina.sh","run"]
CMD ls -l
---------------------事例如下--------------------------------------------------PS D:\opt\work> docker run -it -p 7777:8080 tomcat ls -ltotal 160-rw-r--r-- 1 root root 18994 Nov 9 22:12 BUILDING.txt-rw-r--r-- 1 root root 6210 Nov 9 22:12 CONTRIBUTING.md-rw-r--r-- 1 root root 60269 Nov 9 22:12 LICENSE-rw-r--r-- 1 root root 2333 Nov 9 22:12 NOTICE-rw-r--r-- 1 root root 3372 Nov 9 22:12 README.md-rw-r--r-- 1 root root 6905 Nov 9 22:12 RELEASE-NOTES-rw-r--r-- 1 root root 16517 Nov 9 22:12 RUNNING.txtdrwxr-xr-x 2 root root 4096 Nov 15 20:58 bindrwxr-xr-x 2 root root 4096 Nov 9 22:12 confdrwxr-xr-x 2 root root 4096 Nov 15 20:58 libdrwxrwxrwx 2 root root 4096 Nov 9 22:12 logsdrwxr-xr-x 2 root root 4096 Nov 15 20:58 native-jni-libdrwxrwxrwx 2 root root 4096 Nov 15 20:58 tempdrwxr-xr-x 2 root root 4096 Nov 15 20:58 webappsdrwxr-xr-x 7 root root 4096 Nov 9 22:12 webapps.distdrwxrwxrwx 2 root root 4096 Nov 9 22:12 workPS D:\opt\work> docker psCONTAINER ID IMAGECOMMAND CREATEDSTATUSPORTSNAMES155b6b5d5e34 mycentos:2.0 "/bin/sh -c /bin/bash" 49 minutes ago Up 49 minutes 80/tcp compassionate_joliot
ENTRYPOINT
docker run 之后的参数会被当做参数传递给ENTRYPOINT,之后形成新的命令组合。
case
制作CMD版可以查询ip信息的容器
curl命令解释
curl命令可以用来执行下载、发送各种HTTP请求,指定HTTP头部等操作
如果系统没有curl可以使用yum install curl安装,也可以下载安装
curl是将下载文件输出到stdout
#Dockerfile2文件#制作CMD版可以查询ip信息的容器FROM centosRUN yum install -y curlCMD ["curl","-s",""]
PS D:\opt\work> docker build -f Dockerfile3 -t myid .[+] Building 16.4s (6/6) FINISHED=> [internal] load build definition from Dockerfile2 0.0s=> => transferring dockerfile: 108B 0.0s=> [internal] load .dockerignore 0.0s=> => transferring context: 2B0.0s=> [internal] load metadata for docker.io/library/centos:latest 0.0s=> CACHED [1/2] FROM docker.io/library/centos 0.0s=> [2/2] RUN yum install -y curl 16.0s=> exporting to image0.3s=> => exporting layers 0.3s=> => writing image sha256:be33705a52a2a1cfdac47abcf20b0bd18578fe5d5b6dff6f78a928de3bdcf2f00.0s=> => naming to docker.io/library/myip 0.0sUse 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix themPS D:\opt\work> docker images myipREPOSITORY TAG IMAGE ID CREATEDSIZEmyip latest be33705a52a2 58 seconds ago 277MBPS D:\opt\work> docker run -it myipPS D:\opt\work> docker psCONTAINER ID IMAGECOMMAND CREATED STATUS PORTSNAMES155b6b5d5e34 mycentos:2.0 "/bin/sh -c /bin/bash" 2 hours ago Up 2 hours 80/tcp compassionate_joliotPS D:\opt\work> docker run -it be33705a52a2
例子:
使用命令:curl
执行后,的HTML就会显示在屏幕上了
PS D:\opt\work> curl StatusCode : 200StatusDescription : OKContent : <!DOCTYPE html><!--STATUS OK--><html><head><meta http-equiv="Content-Type" content="text/html;charset=utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><meta content="always"...RawContent : HTTP/1.1 200 OKBdpagetype: 1Bdqid: 0xf1b4778700023447Connection: keep-aliveCache-Control: privateContent-Type: text/html;charset=utf-8Date: Mon, 27 Dec 09:32:04 GMTExpires: Mon, 27 ...Forms : {form}Headers : {[Bdpagetype, 1], [Bdqid, 0xf1b4778700023447], [Connection, keep-alive], [Cache-Control, private]...}Images : {@{innerHTML=; innerText=; outerHTML=<img src="/static/superman/img/topnav/newfanyi-da0cea8f7e.png">; outerText=; tagName=IMG; src=/static/superman/img/topnav/newfanyi-da0cea8f7e.png}, @{innerHTML=; innerText=; outerHTML=<img src="/static/superman/img/topnav/newxueshuicon-a5314d5c83.png">; outerText=; tagName=IMG; src=/static/superman/img/topnav/newxueshuicon-a5314d5c83.png}, @{innerHTML=; innerText=; outerHTML=<img src="/static/superman/img/topnav/newwenku-d8c9b7b0fb.png">; outerText=; tagName=IMG; src=/static/superman/img/topnav/newwenku-d8c9b7b0fb.png}, @{innerHTML=; innerText=; outerHTML=<img src="/static/superman/img/topnav/newbaike-889054f349.png">; outerText=; tagName=IMG; src=/static/superman/img/topnav/newbaike-889054f349.png}...}InputFields : {@{innerHTML=; innerText=; outerHTML=<input name="ie" type="hidden" value="utf-8">; outerText=; tagName=INPUT; name=ie; type=hidden; value=utf-8}, @{innerHTML=; innerText=; outerHTML=<input name="f" type="hidden" value="8">; outerText=; tagName=INPUT; name=f; type=hidden; value=8}, @{innerHTML=; innerText=; outerHTML=<input name="rsv_bp" type="hidden" value="1">; outerText=; tagName=INPUT; name=rsv_bp; type=hidden; value=1}, @{innerHTML=; innerText=; outerHTML=<input name="rsv_idx" type="hidden" value="1">; outerText=; tagName=INPUT; name=rsv_idx; type=hidden; value=1}...}Links : {@{innerHTML=百度首页; innerText=百度首页; outerHTML=<a class="toindex" href="/">百度首页</a>; outerText=百度首页; tagName=A; class=toindex; href=/}, @{innerHTML=设置<i class="c-icon c-icon-triangle-down"></i>; innerText=设置; outerHTML=<a name="tj_settingicon" class="pf" href="javascript:;">设置<i class="c-icon c-icon-triangle-down"></i></a>; outerText=设置; tagName=A; name=tj_settingicon; class=pf; href=javascript:;}, @{innerHTML=登录; innerText=登录; outerHTML=<a name="tj_login" class="lb" onclick="return false;" href="/v2/?login&tpl=mn&u=http%3A%2F%%2F&sms=5">登录</a>; outerText=登录; tagName=A; name=tj_login; class=lb; onclick=return false;; href=/v2/?login&tpl=mn&u=http%3A%2F%%2F&sms=5}, @{innerHTML=新闻; innerText=新闻; outerHTML=<a class="mnav c-font-normal c-color-t" href="" target="_blank">新闻</a>; outerText=新闻; tagName=A; class=mnav c-font-normal c-color-t; href=; target=_blank}...}ParsedHtml : System.__ComObjectRawContentLength : 329103
问题
如果我们希望显示http头信息,就需要加上-i参数
why
制作ENTROYPOINT版查询ip信息的容器
#Dockerfile3文件#制作ENTROYPOINT版可以查询ip信息的容器FROM centosRUN yum install -y curlENTROYPOINT ["curl","-s",""]
构建镜像myip2
docker build -f .\Dockerfile3 -t myip2 .
ONBUILD案例
ONBUILD :子类继承父类的ONBUILD指令,在子类制作镜像时,继承父类镜像的时候触发执行。
当构建一个被继承的Dockerfile时运行命令,父镜像在被子继承后父镜像的onbuild被触发。
Dockerfile4文件
FROM centosRUN yum install -y curlENTRYPOINT ["curl","-s",""]ONBUILD RUN echo "father images onbuild---------886"
构建父类镜像myip_father
docker build -f .\Dockerfile4 -t myip_father .
Dockerfile5文件
FROM myip_fatherRUN yum install -y curlENTRYPOINT ["curl","-s",""]
构建子类镜像
docker build -f .\Dockerfile5 -t myip_son .
此时FROM myip_father会触发父类的ONBUILD指令。
自定义镜像Tomcat9案例
1、mkdir -p /opt/work/tomcat9
2、在上述目录下touch c.txt
3、将jdk和Tomcat安装的压缩包拷贝进上一步目录
apache-tomcat-9.0.56.tar.gz
jdk-8u201-linux-x64.tar.gz
4、在构建/opt/work/tomcat9目录下新建Dockerfile文件
5、构建
#Dockerfile6FROM centosMAINTAINER yyds<123@>#把宿主机当前上下文的c.txt拷贝到容器/usr/local路径下COPY c.txt /usr/local/cincontainer.txt#把Java与Tomcat添加到容器中ADD jdk-8u201-linux-x64.tar.gz /usr/local/ADD apache-tomcat-9.0.56.tar.gz /usr/local/#安装vim编辑器RUN yum -y install vim#设置工作访问时候的WORKDIR路径,登录落脚点ENV MYPATH /usr/localWORKDIR $MYPATH#配置java与Tomcat环境变量ENV JAVA_HOME /usr/local/jdk1.8.0_201ENV CLASSPATH $JAVA_HOME/bin/dt.jar:$JAVA_HOME/bin/tools.jarENV CATALINA_HOME /usr/local/apache-tomcat-9.0.56ENV CATALINA_BASE /usr/local/apache-tomcat-9.0.56ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/bin:$CATALINA_HOME/bin#容器运行时监听的端口EXPOSE 8080#启动时运行Tomcat#ENTRYPOINT ["/usr/local/apache-tomcat-9.0.56/bin/startup.sh"]#CMD ["/usr/local/apache-tomcat-9.0.56/bin/catalina.sh","run"]CMD /usr/local/apache-tomcat-9.0.56/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.56/bin/logs/catalina.out
docker build -f .\Dockerfile6 -t yydstomcat9 .
PS D:\opt\work\tomcat9> docker build -f .\Dockerfile6 -t yydsTomcat9 .invalid argument "yydsTomcat9" for "-t, --tag" flag: invalid reference format: repository name must be lowercaseSee 'docker build --help'.----------------docker镜像名字必须小写----------------------------------------PS D:\opt\work\tomcat9> docker build -f .\Dockerfile6 -t yydstomcat9 .[+] Building 29.1s (11/11) FINISHED=> [internal] load build definition from Dockerfile6 0.0s=> => transferring dockerfile: 1.10kB 0.0s=> [internal] load .dockerignore 0.0s=> => transferring context: 2B0.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 2.3s=> => transferring context: 203.45MB2.3s=> [2/6] COPY c.txt /usr/local/cincontainer.txt 0.1s=> [3/6] ADD jdk-8u201-linux-x64.tar.gz /usr/local/4.4s=> [4/6] ADD apache-tomcat-9.0.56.tar.gz /usr/local/ 0.4s=> [5/6] RUN yum -y install vim 19.5s=> [6/6] WORKDIR /usr/local 0.0s=> exporting to image2.2s=> => exporting layers 2.2s=> => writing image sha256:a19b70a141974dec7f1c695692e395fb18e277bf7ad0c91a221fa4b727a109d50.0s=> => naming to docker.io/library/yydstomcat9 0.0sUse 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them-----------------查看构建好的镜像--------------------PS D:\opt\work\tomcat9> docker images yydstomcat9REPOSITORY TAG IMAGE ID CREATED SIZEyydstomcat9 latest a19b70a14197 2 minutes ago 709MB
6、run
docker启动命令:
docker run -d -p 9080:8080 --name myt9 -v D:\opt\work\tomcat9\test:/usr/local/apache-tomcat-9.0.56/webapps/test -v D:\opt\work\tomcat9\tomcat9logs:/usr/local/apache-tomcat-9.0.56/logs --privileged=true yydstomcat9–privileged=true:权限不够的话,写true
PS D:\opt\work\tomcat9> docker run -d -p 9080:8080 --name myt9 -v D:\opt\work\tomcat9\test:/usr/local/apache-tomcat-9.0.56/webapps/test -v D:\opt\work\tomcat9\tomcat9logs:/usr/local/apache-tomcat-9.0.56/logs --privileged=true yydstomcat91b2f9fab39041fd6f8e29780fb11e1411c0c074557653781f19470477f4d9d4dPS D:\opt\work\tomcat9> docker psCONTAINER ID IMAGECOMMAND CREATED STATUS PORTSNAMES1b2f9fab3904 yydstomcat9 "/bin/sh -c '/usr/lo…" 4 seconds ago Up 3 seconds 0.0.0.0:9080->8080/tcp myt9PS D:\opt\work\tomcat9> docker exec -it myt9 bash[root@1b2f9fab3904 local]#lsapache-tomcat-9.0.56 bin cincontainer.txt etc games include jdk1.8.0_201 lib lib64 libexec sbin share src[root@1b2f9fab3904 local]# pwd/usr/local[root@1b2f9fab3904 local]# lsapache-tomcat-9.0.56 bin cincontainer.txt etc games include jdk1.8.0_201 lib lib64 libexec sbin share src[root@1b2f9fab3904 local]# cd apache-tomcat-9.0.56/webapps/[root@1b2f9fab3904 webapps]# lsROOT docs examples host-manager manager test[root@1b2f9fab3904 webapps]# cd test/[root@1b2f9fab3904 test]# ls[root@1b2f9fab3904 test]# touch 1.txt[root@1b2f9fab3904 test]# cd ..[root@1b2f9fab3904 webapps]# cd ..[root@1b2f9fab3904 apache-tomcat-9.0.56]# cd logs/[root@1b2f9fab3904 logs]# lscatalina.-12-28.log catalina.out host-manager.-12-28.log localhost.-12-28.log localhost_access_log.-12-28.txt manager.-12-28.log[root@1b2f9fab3904 logs]#java -versionjava version "1.8.0_201"Java(TM) SE Runtime Environment (build 1.8.0_201-b09)Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)[root@1b2f9fab3904 logs]# pwd/usr/local/apache-tomcat-9.0.56/logs[root@1b2f9fab3904 logs]# cd ../webapps/test/[root@1b2f9fab3904 test]# ls1.txt[root@1b2f9fab3904 test]# pwd/usr/local/apache-tomcat-9.0.56/webapps/test
7、验证
http://localhost:9080/
8、结合前述的容器卷测试的web服务test发布
总结
如果觉得《Dockerfile体系结构之保留字指令》对你有帮助,请点赞、收藏,并留下你的观点哦!