欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

docker容器挂载宿主主机目录的操作方法

程序员文章站 2022-03-18 12:59:48
有一项重要的参数 -v 目录挂载,就是让容器内部目录和宿主主机目录关联起来,这样就可以直接操作宿主主机目录而不用再操作具体容器了 比如在2中,我们要发布一个war包,是通...

有一项重要的参数 -v 目录挂载,就是让容器内部目录和宿主主机目录关联起来,这样就可以直接操作宿主主机目录而不用再操作具体容器了

比如在2中,我们要发布一个war包,是通过 

sudo docker cp demo.war tomcat_xiao:/usr/local/tomcat/webapps

 来发布的,有没有更快捷的方式呢?

肯定有, 没有 我就不会在这里写这些玩意儿了

sudo docker run -p 8822:8080 --name tomcat_xiao_volume -v /home/xiaochangwei/test:/usr/local/tomcat/webapps xiaochangwei/tomcat:v1

  -p     不解释
  --name   不解释
   -v /home/xiaochangwei/test:/usr/local/tomcat/webapps 就是将宿主主机目录和容器目录建立映射关系,冒号前为宿主主机目录,冒号后为容器对应目录

  执行上面的命令启动,这次启动就和上一篇文章中的启动日志有点差异了:差异就在容器启动时,tomcat加载了我们映射的宿主主机目录中的war包

xiaochangwei@ubuntu:~/test$ sudo docker ps -a
container id    image        command       created       status       ports        names
xiaochangwei@ubuntu:~/test$ sudo docker images
repository      tag         image id      created       size
xiaochangwei/tomcat  v1         62cedd29ce8f    about an hour ago  505mb
xiaochangwei/nginx  v1.0        9f2c1a649a75    2 hours ago     108mb
nginx         latest       3f8a4339aadd    9 days ago     108mb
ubuntu        16.04        00fd29ccc6f1    3 weeks ago     111mb
xiaochangwei@ubuntu:~/test$ 
xiaochangwei@ubuntu:~/test$ pwd
/home/xiaochangwei/test
xiaochangwei@ubuntu:~/test$ ls -l
total 14180
-rw-rw-r-- 1 xiaochangwei xiaochangwei 14501238 dec 19 23:29 demo.war
-rw-rw-r-- 1 xiaochangwei xiaochangwei   551 jan 4 18:18 dockerfile
drwxr-xr-x 8 xiaochangwei xiaochangwei   4096 dec 12 2016 jdk
drwxrwxr-x 6 xiaochangwei xiaochangwei   4096 dec 20 21:51 maven
drwxrwxr-x 9 xiaochangwei xiaochangwei   4096 dec 20 18:28 tomcat
xiaochangwei@ubuntu:~/test$ sudo docker run -p 8822:8080 --name tomcat_xiao_volume -v /home/xiaochangwei/test:/usr/local/tomcat/webapps xiaochangwei/tomcat:v1
tomcat started.
java hotspot(tm) 64-bit server vm warning: ignoring option permsize=64m; support was removed in 8.0
java hotspot(tm) 64-bit server vm warning: ignoring option maxpermsize=512m; support was removed in 8.0
05-jan-2018 08:32:20.580 info [main] org.apache.catalina.startup.versionloggerlistener.log server version:    apache tomcat/8.5.24
05-jan-2018 08:32:20.641 info [main] org.apache.catalina.startup.versionloggerlistener.log server built:     nov 27 2017 13:05:30 utc
05-jan-2018 08:32:20.641 info [main] org.apache.catalina.startup.versionloggerlistener.log server number:     8.5.24.0
05-jan-2018 08:32:20.641 info [main] org.apache.catalina.startup.versionloggerlistener.log os name:        linux
05-jan-2018 08:32:20.641 info [main] org.apache.catalina.startup.versionloggerlistener.log os version:      4.10.0-28-generic
05-jan-2018 08:32:20.641 info [main] org.apache.catalina.startup.versionloggerlistener.log architecture:     amd64
05-jan-2018 08:32:20.641 info [main] org.apache.catalina.startup.versionloggerlistener.log java home:       /usr/local/jdk/jre
05-jan-2018 08:32:20.642 info [main] org.apache.catalina.startup.versionloggerlistener.log jvm version:      1.8.0_121-b13
05-jan-2018 08:32:20.642 info [main] org.apache.catalina.startup.versionloggerlistener.log jvm vendor:      oracle corporation
05-jan-2018 08:32:20.642 info [main] org.apache.catalina.startup.versionloggerlistener.log catalina_base:     /usr/local/tomcat
05-jan-2018 08:32:20.642 info [main] org.apache.catalina.startup.versionloggerlistener.log catalina_home:     /usr/local/tomcat
05-jan-2018 08:32:20.642 info [main] org.apache.catalina.startup.versionloggerlistener.log command line argument: -djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties
05-jan-2018 08:32:20.648 info [main] org.apache.catalina.startup.versionloggerlistener.log command line argument: -djava.util.logging.manager=org.apache.juli.classloaderlogmanager
05-jan-2018 08:32:20.648 info [main] org.apache.catalina.startup.versionloggerlistener.log command line argument: -djdk.tls.ephemeraldhkeysize=2048
05-jan-2018 08:32:20.648 info [main] org.apache.catalina.startup.versionloggerlistener.log command line argument: -djava.protocol.handler.pkgs=org.apache.catalina.webresources
05-jan-2018 08:32:20.648 info [main] org.apache.catalina.startup.versionloggerlistener.log command line argument: -xms128m
05-jan-2018 08:32:20.648 info [main] org.apache.catalina.startup.versionloggerlistener.log command line argument: -xmx1024m
05-jan-2018 08:32:20.648 info [main] org.apache.catalina.startup.versionloggerlistener.log command line argument: -xx:permsize=64m
05-jan-2018 08:32:20.648 info [main] org.apache.catalina.startup.versionloggerlistener.log command line argument: -xx:maxpermsize=512m
05-jan-2018 08:32:20.648 info [main] org.apache.catalina.startup.versionloggerlistener.log command line argument: -dignore.endorsed.dirs=
05-jan-2018 08:32:20.649 info [main] org.apache.catalina.startup.versionloggerlistener.log command line argument: -dcatalina.base=/usr/local/tomcat
05-jan-2018 08:32:20.649 info [main] org.apache.catalina.startup.versionloggerlistener.log command line argument: -dcatalina.home=/usr/local/tomcat
05-jan-2018 08:32:20.649 info [main] org.apache.catalina.startup.versionloggerlistener.log command line argument: -djava.io.tmpdir=/usr/local/tomcat/temp
05-jan-2018 08:32:20.649 info [main] org.apache.catalina.core.aprlifecyclelistener.lifecycleevent the apr based apache tomcat native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib]
05-jan-2018 08:32:21.293 info [main] org.apache.coyote.abstractprotocol.init initializing protocolhandler ["http-nio-8080"]
05-jan-2018 08:32:21.358 info [main] org.apache.tomcat.util.net.nioselectorpool.getsharedselector using a shared selector for servlet write/read
05-jan-2018 08:32:21.370 info [main] org.apache.coyote.abstractprotocol.init initializing protocolhandler ["ajp-nio-8009"]
05-jan-2018 08:32:21.374 info [main] org.apache.tomcat.util.net.nioselectorpool.getsharedselector using a shared selector for servlet write/read
05-jan-2018 08:32:21.375 info [main] org.apache.catalina.startup.catalina.load initialization processed in 2178 ms
05-jan-2018 08:32:21.432 info [main] org.apache.catalina.core.standardservice.startinternal starting service [catalina]
05-jan-2018 08:32:21.432 info [main] org.apache.catalina.core.standardengine.startinternal starting servlet engine: apache tomcat/8.5.24
05-jan-2018 08:32:21.513 info [localhost-startstop-1] org.apache.catalina.startup.hostconfig.deploywar deploying web application archive [/usr/local/tomcat/webapps/demo.war]
05-jan-2018 08:32:24.342 info [localhost-startstop-1] org.apache.jasper.servlet.tldscanner.scanjars at least one jar was scanned for tlds yet contained no tlds. enable debug logging for this logger for a complete list of jars that were scanned but no tlds were found in them. skipping unneeded jars during scanning can improve startup time and jsp compilation time.
08:32:24.799 [localhost-startstop-1] debug org.springframework.web.context.support.standardservletenvironment - adding propertysource 'servletconfiginitparams' with lowest search precedence
08:32:24.813 [localhost-startstop-1] debug org.springframework.web.context.support.standardservletenvironment - adding propertysource 'servletcontextinitparams' with lowest search precedence
08:32:24.832 [localhost-startstop-1] debug org.springframework.web.context.support.standardservletenvironment - adding propertysource 'jndiproperties' with lowest search precedence
08:32:24.832 [localhost-startstop-1] debug org.springframework.web.context.support.standardservletenvironment - adding propertysource 'systemproperties' with lowest search precedence
08:32:24.832 [localhost-startstop-1] debug org.springframework.web.context.support.standardservletenvironment - adding propertysource 'systemenvironment' with lowest search precedence
08:32:24.833 [localhost-startstop-1] debug org.springframework.web.context.support.standardservletenvironment - initialized standardservletenvironment with propertysources [stubpropertysource@1897128678 {name='servletconfiginitparams', properties=java.lang.object@6b18d91f}, stubpropertysource@1347718626 {name='servletcontextinitparams', properties=java.lang.object@4ba81d05}, jndipropertysource@633520708 {name='jndiproperties', properties=org.springframework.jndi.jndilocatordelegate@83f37c2}, mappropertysource@2087751866 {name='systemproperties', properties={java.runtime.name=java(tm) se runtime environment, java.protocol.handler.pkgs=org.apache.catalina.webresources, sun.boot.library.path=/usr/local/jdk/jre/lib/amd64, java.vm.version=25.121-b13, shared.loader=, java.vm.vendor=oracle corporation, java.vendor.url=http://java.oracle.com/, path.separator=:, tomcat.util.buf.stringcache.byte.enabled=true, java.util.logging.config.file=/usr/local/tomcat/conf/logging.properties, java.vm.name=java hotspot(tm) 64-bit server vm, file.encoding.pkg=sun.io, user.country=us, sun.java.launcher=sun_standard, sun.os.patch.level=unknown, tomcat.util.scan.standardjarscanfilter.jarstoscan=log4j-web*.jar,log4j-taglib*.jar,log4javascript*.jar,slf4j-taglib*.jar, java.vm.specification.name=java virtual machine specification, user.dir=/, java.runtime.version=1.8.0_121-b13, java.awt.graphicsenv=sun.awt.x11graphicsenvironment, java.endorsed.dirs=/usr/local/jdk/jre/lib/endorsed, os.arch=amd64, java.io.tmpdir=/usr/local/tomcat/temp, line.separator=
, java.vm.specification.vendor=oracle corporation, java.naming.factory.url.pkgs=org.apache.naming, java.util.logging.manager=org.apache.juli.classloaderlogmanager, os.name=linux, ignore.endorsed.dirs=, sun.jnu.encoding=ansi_x3.4-1968, java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib, java.specification.name=java platform api specification, java.class.version=52.0, sun.management.compiler=hotspot 64-bit tiered compilers, os.version=4.10.0-28-generic, java.util.concurrent.forkjoinpool.common.threadfactory=org.apache.catalina.startup.safeforkjoinworkerthreadfactory, user.home=/root, catalina.usenaming=true, user.timezone=etc/utc, java.awt.printerjob=sun.print.psprinterjob, file.encoding=ansi_x3.4-1968, java.specification.version=1.8, tomcat.util.scan.standardjarscanfilter.jarstoskip=bootstrap.jar,commons-daemon.jar,tomcat-juli.jar,annotations-api.jar,el-api.jar,jsp-api.jar,servlet-api.jar,websocket-api.jar,jaspic-api.jar,catalina.jar,catalina-ant.jar,catalina-ha.jar,catalina-storeconfig.jar,catalina-tribes.jar,jasper.jar,jasper-el.jar,ecj-*.jar,tomcat-api.jar,tomcat-util.jar,tomcat-util-scan.jar,tomcat-coyote.jar,tomcat-dbcp.jar,tomcat-jni.jar,tomcat-websocket.jar,tomcat-i18n-en.jar,tomcat-i18n-es.jar,tomcat-i18n-fr.jar,tomcat-i18n-ja.jar,tomcat-juli-adapters.jar,catalina-jmx-remote.jar,catalina-ws.jar,tomcat-jdbc.jar,tools.jar,commons-beanutils*.jar,commons-codec*.jar,commons-collections*.jar,commons-dbcp*.jar,commons-digester*.jar,commons-fileupload*.jar,commons-httpclient*.jar,commons-io*.jar,commons-lang*.jar,commons-logging*.jar,commons-math*.jar,commons-pool*.jar,jstl.jar,taglibs-standard-spec-*.jar,geronimo-spec-jaxrpc*.jar,wsdl4j*.jar,ant.jar,ant-junit*.jar,aspectj*.jar,jmx.jar,h2*.jar,hibernate*.jar,httpclient*.jar,jmx-tools.jar,jta*.jar,log4j*.jar,mail*.jar,slf4j*.jar,xercesimpl.jar,xmlparserapis.jar,xml-apis.jar,junit.jar,junit-*.jar,hamcrest-*.jar,easymock-*.jar,cglib-*.jar,objenesis-*.jar,ant-launcher.jar,cobertura-*.jar,asm-*.jar,dom4j-*.jar,icu4j-*.jar,jaxen-*.jar,jdom-*.jar,jetty-*.jar,oro-*.jar,servlet-api-*.jar,tagsoup-*.jar,xmlparserapis-*.jar,xom-*.jar, catalina.home=/usr/local/tomcat, java.class.path=/usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar, user.name=root, java.naming.factory.initial=org.apache.naming.java.javaurlcontextfactory, package.definition=sun.,java.,org.apache.catalina.,org.apache.coyote.,org.apache.jasper.,org.apache.naming.,org.apache.tomcat., java.vm.specification.version=1.8, sun.java.command=org.apache.catalina.startup.bootstrap start, java.home=/usr/local/jdk/jre, sun.arch.data.model=64, user.language=en, java.specification.vendor=oracle corporation, awt.toolkit=sun.awt.x11.xtoolkit, java.vm.info=mixed mode, java.version=1.8.0_121, java.ext.dirs=/usr/local/jdk/jre/lib/ext:/usr/java/packages/lib/ext, sun.boot.class.path=/usr/local/jdk/jre/lib/resources.jar:/usr/local/jdk/jre/lib/rt.jar:/usr/local/jdk/jre/lib/sunrsasign.jar:/usr/local/jdk/jre/lib/jsse.jar:/usr/local/jdk/jre/lib/jce.jar:/usr/local/jdk/jre/lib/charsets.jar:/usr/local/jdk/jre/lib/jfr.jar:/usr/local/jdk/jre/classes, server.loader=, java.vendor=oracle corporation, catalina.base=/usr/local/tomcat, jdk.tls.ephemeraldhkeysize=2048, file.separator=/, java.vendor.url.bug=http://bugreport.sun.com/bugreport/, common.loader="${catalina.base}/lib","${catalina.base}/lib/*.jar","${catalina.home}/lib","${catalina.home}/lib/*.jar", sun.io.unicode.encoding=unicodelittle, sun.cpu.endian=little, package.access=sun.,org.apache.catalina.,org.apache.coyote.,org.apache.jasper.,org.apache.tomcat., sun.cpu.isalist=}}, systemenvironmentpropertysource@2000415236 {name='systemenvironment', properties={path=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/jdk/bin:/usr/local/maven/bin, jre_home=/usr/local/jdk/jre, hostname=2dcd945938e8, nlspath=/usr/dt/lib/nls/msg/%l/%n.cat, m2_home=/usr/local/maven, java_home=/usr/local/jdk, catalina_opts=-xms128m -xmx1024m -xx:permsize=64m -xx:maxpermsize=512m, jdk_java_options= --add-opens=java.base/java.lang=all-unnamed --add-opens=java.rmi/sun.rmi.transport=all-unnamed, xfilesearchpath=/usr/dt/app-defaults/%l/dt, pwd=/, classpath=/usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar, home=/root}}]
08:32:24.844 [localhost-startstop-1] debug org.springframework.web.context.support.standardservletenvironment - replacing propertysource 'servletcontextinitparams' with 'servletcontextinitparams'
 .  ____     _      __ _ _
 /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/ ___)| |_)| | | | | || (_| | ) ) ) )
 ' |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: spring boot ::    (v1.5.9.release)
2018-01-05 08:32:26.068 info 13 --- [ost-startstop-1] com.example.demo.servletinitializer   : starting servletinitializer v0.0.1-snapshot on 2dcd945938e8 with pid 13 (/usr/local/tomcat/webapps/demo/web-inf/classes started by root in /)
2018-01-05 08:32:26.180 info 13 --- [ost-startstop-1] com.example.demo.servletinitializer   : no active profile set, falling back to default profiles: default
2018-01-05 08:32:26.357 info 13 --- [ost-startstop-1] ationconfigembeddedwebapplicationcontext : refreshing org.springframework.boot.context.embedded.annotationconfigembeddedwebapplicationcontext@77b0904a: startup date [fri jan 05 08:32:26 utc 2018]; root of context hierarchy
2018-01-05 08:32:28.732 info 13 --- [ost-startstop-1] o.s.web.context.contextloader      : root webapplicationcontext: initialization completed in 2374 ms
2018-01-05 08:32:29.920 info 13 --- [ost-startstop-1] o.s.b.w.servlet.servletregistrationbean : mapping servlet: 'dispatcherservlet' to [/]
2018-01-05 08:32:29.930 info 13 --- [ost-startstop-1] o.s.b.w.servlet.filterregistrationbean  : mapping filter: 'errorpagefilter' to: [/*]
2018-01-05 08:32:29.934 info 13 --- [ost-startstop-1] o.s.b.w.servlet.filterregistrationbean  : mapping filter: 'characterencodingfilter' to: [/*]
2018-01-05 08:32:29.935 info 13 --- [ost-startstop-1] o.s.b.w.servlet.filterregistrationbean  : mapping filter: 'hiddenhttpmethodfilter' to: [/*]
2018-01-05 08:32:29.935 info 13 --- [ost-startstop-1] o.s.b.w.servlet.filterregistrationbean  : mapping filter: 'httpputformcontentfilter' to: [/*]
2018-01-05 08:32:29.935 info 13 --- [ost-startstop-1] o.s.b.w.servlet.filterregistrationbean  : mapping filter: 'requestcontextfilter' to: [/*]
2018-01-05 08:32:30.900 info 13 --- [ost-startstop-1] s.w.s.m.m.a.requestmappinghandleradapter : looking for @controlleradvice: org.springframework.boot.context.embedded.annotationconfigembeddedwebapplicationcontext@77b0904a: startup date [fri jan 05 08:32:26 utc 2018]; root of context hierarchy
2018-01-05 08:32:31.079 info 13 --- [ost-startstop-1] s.w.s.m.m.a.requestmappinghandlermapping : mapped "{[/test]}" onto public java.lang.string com.example.demo.web.test.test()
2018-01-05 08:32:31.084 info 13 --- [ost-startstop-1] s.w.s.m.m.a.requestmappinghandlermapping : mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.modelandview org.springframework.boot.autoconfigure.web.basicerrorcontroller.errorhtml(javax.servlet.http.httpservletrequest,javax.servlet.http.httpservletresponse)
2018-01-05 08:32:31.091 info 13 --- [ost-startstop-1] s.w.s.m.m.a.requestmappinghandlermapping : mapped "{[/error]}" onto public org.springframework.http.responseentity<java.util.map<java.lang.string, java.lang.object>> org.springframework.boot.autoconfigure.web.basicerrorcontroller.error(javax.servlet.http.httpservletrequest)
2018-01-05 08:32:31.185 info 13 --- [ost-startstop-1] o.s.w.s.handler.simpleurlhandlermapping : mapped url path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.resourcehttprequesthandler]
2018-01-05 08:32:31.186 info 13 --- [ost-startstop-1] o.s.w.s.handler.simpleurlhandlermapping : mapped url path [/**] onto handler of type [class org.springframework.web.servlet.resource.resourcehttprequesthandler]
2018-01-05 08:32:31.302 info 13 --- [ost-startstop-1] o.s.w.s.handler.simpleurlhandlermapping : mapped url path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.resourcehttprequesthandler]
2018-01-05 08:32:31.667 info 13 --- [ost-startstop-1] o.s.j.e.a.annotationmbeanexporter    : registering beans for jmx exposure on startup
2018-01-05 08:32:31.699 info 13 --- [ost-startstop-1] com.example.demo.servletinitializer   : started servletinitializer in 6.843 seconds (jvm running for 13.352)
05-jan-2018 08:32:31.792 info [localhost-startstop-1] org.apache.catalina.startup.hostconfig.deploywar deployment of web application archive [/usr/local/tomcat/webapps/demo.war] has finished in [10,278] ms
05-jan-2018 08:32:31.793 info [localhost-startstop-1] org.apache.catalina.startup.hostconfig.deploydirectory deploying web application directory [/usr/local/tomcat/webapps/tomcat]
05-jan-2018 08:32:31.846 info [localhost-startstop-1] org.apache.catalina.startup.hostconfig.deploydirectory deployment of web application directory [/usr/local/tomcat/webapps/tomcat] has finished in [52] ms
05-jan-2018 08:32:31.846 info [localhost-startstop-1] org.apache.catalina.startup.hostconfig.deploydirectory deploying web application directory [/usr/local/tomcat/webapps/maven]
05-jan-2018 08:32:31.874 info [localhost-startstop-1] org.apache.catalina.startup.hostconfig.deploydirectory deployment of web application directory [/usr/local/tomcat/webapps/maven] has finished in [27] ms
05-jan-2018 08:32:31.878 info [localhost-startstop-1] org.apache.catalina.startup.hostconfig.deploydirectory deploying web application directory [/usr/local/tomcat/webapps/jdk]
05-jan-2018 08:32:31.919 info [localhost-startstop-1] org.apache.catalina.startup.hostconfig.deploydirectory deployment of web application directory [/usr/local/tomcat/webapps/jdk] has finished in [41] ms
05-jan-2018 08:32:31.926 info [main] org.apache.coyote.abstractprotocol.start starting protocolhandler ["http-nio-8080"]
05-jan-2018 08:32:31.958 info [main] org.apache.coyote.abstractprotocol.start starting protocolhandler ["ajp-nio-8009"]
05-jan-2018 08:32:31.980 info [main] org.apache.catalina.startup.catalina.start server startup in 10604 ms

按照说明,如果在宿主主机的目录中再放一个war包,容器也应该自动部署,下面验证下

为了方便,我直接复制了demo.war

xiaochangwei@ubuntu:~/test$ ls -l
total 14184
drwxr-x--- 5 root     root       4096 jan 5 00:32 demo
-rw-rw-r-- 1 xiaochangwei xiaochangwei 14501238 dec 19 23:29 demo.war
-rw-rw-r-- 1 xiaochangwei xiaochangwei   551 jan 4 18:18 dockerfile
drwxr-xr-x 8 xiaochangwei xiaochangwei   4096 dec 12 2016 jdk
drwxrwxr-x 6 xiaochangwei xiaochangwei   4096 dec 20 21:51 maven
drwxrwxr-x 9 xiaochangwei xiaochangwei   4096 dec 20 18:28 tomcat
xiaochangwei@ubuntu:~/test$ 
xiaochangwei@ubuntu:~/test$ 
xiaochangwei@ubuntu:~/test$ cp -p demo.war demo2.war
xiaochangwei@ubuntu:~/test$ ll
total 28360
drwxrwxr-x 7 xiaochangwei xiaochangwei   4096 jan 5 00:41 ./
drwxr-xr-x 18 xiaochangwei xiaochangwei   4096 dec 20 23:01 ../
drwxr-x--- 5 root     root       4096 jan 5 00:32 demo/
drwxr-x--- 5 root     root       4096 jan 5 00:41 demo2/
-rw-rw-r-- 1 xiaochangwei xiaochangwei 14501238 dec 19 23:29 demo2.war
-rw-rw-r-- 1 xiaochangwei xiaochangwei 14501238 dec 19 23:29 demo.war
-rw-rw-r-- 1 xiaochangwei xiaochangwei   551 jan 4 18:18 dockerfile
drwxr-xr-x 8 xiaochangwei xiaochangwei   4096 dec 12 2016 jdk/
drwxrwxr-x 6 xiaochangwei xiaochangwei   4096 dec 20 21:51 maven/
drwxrwxr-x 9 xiaochangwei xiaochangwei   4096 dec 20 18:28 tomcat/
xiaochangwei@ubuntu:~/test$ 

然后看容器的控制台:

05-jan-2018 08:32:31.926 info [main] org.apache.coyote.abstractprotocol.start starting protocolhandler ["http-nio-8080"]
05-jan-2018 08:32:31.958 info [main] org.apache.coyote.abstractprotocol.start starting protocolhandler ["ajp-nio-8009"]
05-jan-2018 08:32:31.980 info [main] org.apache.catalina.startup.catalina.start server startup in 10604 ms
05-jan-2018 08:41:22.174 info [localhost-startstop-2] org.apache.catalina.startup.hostconfig.deploywar deploying web application archive [/usr/local/tomcat/webapps/demo2.war]
2018-01-05 08:41:23.862 info 13 --- [ost-startstop-2] org.apache.jasper.servlet.tldscanner   : at least one jar was scanned for tlds yet contained no tlds. enable debug logging for this logger for a complete list of jars that were scanned but no tlds were found in them. skipping unneeded jars during scanning can improve startup time and jsp compilation time.
08:41:24.163 [localhost-startstop-2] debug org.springframework.web.context.support.standardservletenvironment - adding propertysource 'servletconfiginitparams' with lowest search precedence
08:41:24.165 [localhost-startstop-2] debug org.springframework.web.context.support.standardservletenvironment - adding propertysource 'servletcontextinitparams' with lowest search precedence
08:41:24.173 [localhost-startstop-2] debug org.springframework.web.context.support.standardservletenvironment - adding propertysource 'jndiproperties' with lowest search precedence
08:41:24.178 [localhost-startstop-2] debug org.springframework.web.context.support.standardservletenvironment - adding propertysource 'systemproperties' with lowest search precedence
08:41:24.178 [localhost-startstop-2] debug org.springframework.web.context.support.standardservletenvironment - adding propertysource 'systemenvironment' with lowest search precedence
08:41:24.178 [localhost-startstop-2] debug org.springframework.web.context.support.standardservletenvironment - initialized standardservletenvironment with propertysources [stubpropertysource@165669309 {name='servletconfiginitparams', properties=java.lang.object@2636bd14}, stubpropertysource@1385884584 {name='servletcontextinitparams', properties=java.lang.object@33ec3b2}, jndipropertysource@1968853097 {name='jndiproperties', properties=org.springframework.jndi.jndilocatordelegate@66178dcb}, mappropertysource@1666575626 {name='systemproperties', properties={java.vendor=oracle corporation, sun.java.launcher=sun_standard, catalina.base=/usr/local/tomcat, sun.management.compiler=hotspot 64-bit tiered compilers, catalina.usenaming=true, os.name=linux, sun.boot.class.path=/usr/local/jdk/jre/lib/resources.jar:/usr/local/jdk/jre/lib/rt.jar:/usr/local/jdk/jre/lib/sunrsasign.jar:/usr/local/jdk/jre/lib/jsse.jar:/usr/local/jdk/jre/lib/jce.jar:/usr/local/jdk/jre/lib/charsets.jar:/usr/local/jdk/jre/lib/jfr.jar:/usr/local/jdk/jre/classes, java.util.logging.config.file=/usr/local/tomcat/conf/logging.properties, java.vm.specification.vendor=oracle corporation, java.runtime.version=1.8.0_121-b13, user.name=root, tomcat.util.scan.standardjarscanfilter.jarstoscan=log4j-web*.jar,log4j-taglib*.jar,log4javascript*.jar,slf4j-taglib*.jar, shared.loader=, tomcat.util.buf.stringcache.byte.enabled=true, user.language=en, java.naming.factory.initial=org.apache.naming.java.javaurlcontextfactory, sun.boot.library.path=/usr/local/jdk/jre/lib/amd64, pid=13, jdk.tls.ephemeraldhkeysize=2048, java.version=1.8.0_121, java.util.logging.manager=org.apache.juli.classloaderlogmanager, user.timezone=etc/utc, sun.arch.data.model=64, java.util.concurrent.forkjoinpool.common.threadfactory=org.apache.catalina.startup.safeforkjoinworkerthreadfactory, java.endorsed.dirs=/usr/local/jdk/jre/lib/endorsed, sun.cpu.isalist=, sun.jnu.encoding=ansi_x3.4-1968, file.encoding.pkg=sun.io, package.access=sun.,org.apache.catalina.,org.apache.coyote.,org.apache.jasper.,org.apache.tomcat., file.separator=/, java.specification.name=java platform api specification, java.class.version=52.0, user.country=us, java.home=/usr/local/jdk/jre, java.vm.info=mixed mode, os.version=4.10.0-28-generic, path.separator=:, java.vm.version=25.121-b13, org.jboss.logging.provider=slf4j, java.protocol.handler.pkgs=org.apache.catalina.webresources, java.awt.printerjob=sun.print.psprinterjob, sun.io.unicode.encoding=unicodelittle, awt.toolkit=sun.awt.x11.xtoolkit, package.definition=sun.,java.,org.apache.catalina.,org.apache.coyote.,org.apache.jasper.,org.apache.naming.,org.apache.tomcat., java.naming.factory.url.pkgs=org.apache.naming, user.home=/root, java.specification.vendor=oracle corporation, tomcat.util.scan.standardjarscanfilter.jarstoskip=bootstrap.jar,commons-daemon.jar,tomcat-juli.jar,annotations-api.jar,el-api.jar,jsp-api.jar,servlet-api.jar,websocket-api.jar,jaspic-api.jar,catalina.jar,catalina-ant.jar,catalina-ha.jar,catalina-storeconfig.jar,catalina-tribes.jar,jasper.jar,jasper-el.jar,ecj-*.jar,tomcat-api.jar,tomcat-util.jar,tomcat-util-scan.jar,tomcat-coyote.jar,tomcat-dbcp.jar,tomcat-jni.jar,tomcat-websocket.jar,tomcat-i18n-en.jar,tomcat-i18n-es.jar,tomcat-i18n-fr.jar,tomcat-i18n-ja.jar,tomcat-juli-adapters.jar,catalina-jmx-remote.jar,catalina-ws.jar,tomcat-jdbc.jar,tools.jar,commons-beanutils*.jar,commons-codec*.jar,commons-collections*.jar,commons-dbcp*.jar,commons-digester*.jar,commons-fileupload*.jar,commons-httpclient*.jar,commons-io*.jar,commons-lang*.jar,commons-logging*.jar,commons-math*.jar,commons-pool*.jar,jstl.jar,taglibs-standard-spec-*.jar,geronimo-spec-jaxrpc*.jar,wsdl4j*.jar,ant.jar,ant-junit*.jar,aspectj*.jar,jmx.jar,h2*.jar,hibernate*.jar,httpclient*.jar,jmx-tools.jar,jta*.jar,log4j*.jar,mail*.jar,slf4j*.jar,xercesimpl.jar,xmlparserapis.jar,xml-apis.jar,junit.jar,junit-*.jar,hamcrest-*.jar,easymock-*.jar,cglib-*.jar,objenesis-*.jar,ant-launcher.jar,cobertura-*.jar,asm-*.jar,dom4j-*.jar,icu4j-*.jar,jaxen-*.jar,jdom-*.jar,jetty-*.jar,oro-*.jar,servlet-api-*.jar,tagsoup-*.jar,xmlparserapis-*.jar,xom-*.jar, java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib, java.vendor.url=http://java.oracle.com/, spring.beaninfo.ignore=true, java.vm.vendor=oracle corporation, common.loader="${catalina.base}/lib","${catalina.base}/lib/*.jar","${catalina.home}/lib","${catalina.home}/lib/*.jar", java.runtime.name=java(tm) se runtime environment, sun.java.command=org.apache.catalina.startup.bootstrap start, java.class.path=/usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar, java.vm.specification.name=java virtual machine specification, java.vm.specification.version=1.8, catalina.home=/usr/local/tomcat, sun.cpu.endian=little, sun.os.patch.level=unknown, java.awt.headless=true, java.io.tmpdir=/usr/local/tomcat/temp, java.vendor.url.bug=http://bugreport.sun.com/bugreport/, server.loader=, os.arch=amd64, java.awt.graphicsenv=sun.awt.x11graphicsenvironment, java.ext.dirs=/usr/local/jdk/jre/lib/ext:/usr/java/packages/lib/ext, user.dir=/, line.separator=
, java.vm.name=java hotspot(tm) 64-bit server vm, ignore.endorsed.dirs=, file.encoding=ansi_x3.4-1968, java.specification.version=1.8}}, systemenvironmentpropertysource@377717121 {name='systemenvironment', properties={path=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/jdk/bin:/usr/local/maven/bin, jre_home=/usr/local/jdk/jre, hostname=2dcd945938e8, nlspath=/usr/dt/lib/nls/msg/%l/%n.cat, m2_home=/usr/local/maven, java_home=/usr/local/jdk, catalina_opts=-xms128m -xmx1024m -xx:permsize=64m -xx:maxpermsize=512m, jdk_java_options= --add-opens=java.base/java.lang=all-unnamed --add-opens=java.rmi/sun.rmi.transport=all-unnamed, xfilesearchpath=/usr/dt/app-defaults/%l/dt, pwd=/, classpath=/usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar, home=/root}}]
08:41:24.181 [localhost-startstop-2] debug org.springframework.web.context.support.standardservletenvironment - replacing propertysource 'servletcontextinitparams' with 'servletcontextinitparams'
 .  ____     _      __ _ _
 /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/ ___)| |_)| | | | | || (_| | ) ) ) )
 ' |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: spring boot ::    (v1.5.9.release)
2018-01-05 08:41:25.139 info 13 --- [ost-startstop-2] com.example.demo.servletinitializer   : starting servletinitializer v0.0.1-snapshot on 2dcd945938e8 with pid 13 (/usr/local/tomcat/webapps/demo2/web-inf/classes started by root in /)
2018-01-05 08:41:25.140 info 13 --- [ost-startstop-2] com.example.demo.servletinitializer   : no active profile set, falling back to default profiles: default
2018-01-05 08:41:25.292 info 13 --- [ost-startstop-2] ationconfigembeddedwebapplicationcontext : refreshing org.springframework.boot.context.embedded.annotationconfigembeddedwebapplicationcontext@7d220249: startup date [fri jan 05 08:41:25 utc 2018]; root of context hierarchy
2018-01-05 08:41:27.126 info 13 --- [ost-startstop-2] o.s.web.context.contextloader      : root webapplicationcontext: initialization completed in 1834 ms
2018-01-05 08:41:28.082 info 13 --- [ost-startstop-2] o.s.b.w.servlet.servletregistrationbean : mapping servlet: 'dispatcherservlet' to [/]
2018-01-05 08:41:28.083 info 13 --- [ost-startstop-2] o.s.b.w.servlet.filterregistrationbean  : mapping filter: 'errorpagefilter' to: [/*]
2018-01-05 08:41:28.084 info 13 --- [ost-startstop-2] o.s.b.w.servlet.filterregistrationbean  : mapping filter: 'characterencodingfilter' to: [/*]
2018-01-05 08:41:28.084 info 13 --- [ost-startstop-2] o.s.b.w.servlet.filterregistrationbean  : mapping filter: 'hiddenhttpmethodfilter' to: [/*]
2018-01-05 08:41:28.084 info 13 --- [ost-startstop-2] o.s.b.w.servlet.filterregistrationbean  : mapping filter: 'httpputformcontentfilter' to: [/*]
2018-01-05 08:41:28.084 info 13 --- [ost-startstop-2] o.s.b.w.servlet.filterregistrationbean  : mapping filter: 'requestcontextfilter' to: [/*]
2018-01-05 08:41:28.924 info 13 --- [ost-startstop-2] s.w.s.m.m.a.requestmappinghandleradapter : looking for @controlleradvice: org.springframework.boot.context.embedded.annotationconfigembeddedwebapplicationcontext@7d220249: startup date [fri jan 05 08:41:25 utc 2018]; root of context hierarchy
2018-01-05 08:41:29.087 info 13 --- [ost-startstop-2] s.w.s.m.m.a.requestmappinghandlermapping : mapped "{[/test]}" onto public java.lang.string com.example.demo.web.test.test()
2018-01-05 08:41:29.099 info 13 --- [ost-startstop-2] s.w.s.m.m.a.requestmappinghandlermapping : mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.modelandview org.springframework.boot.autoconfigure.web.basicerrorcontroller.errorhtml(javax.servlet.http.httpservletrequest,javax.servlet.http.httpservletresponse)
2018-01-05 08:41:29.100 info 13 --- [ost-startstop-2] s.w.s.m.m.a.requestmappinghandlermapping : mapped "{[/error]}" onto public org.springframework.http.responseentity<java.util.map<java.lang.string, java.lang.object>> org.springframework.boot.autoconfigure.web.basicerrorcontroller.error(javax.servlet.http.httpservletrequest)
2018-01-05 08:41:29.181 info 13 --- [ost-startstop-2] o.s.w.s.handler.simpleurlhandlermapping : mapped url path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.resourcehttprequesthandler]
2018-01-05 08:41:29.181 info 13 --- [ost-startstop-2] o.s.w.s.handler.simpleurlhandlermapping : mapped url path [/**] onto handler of type [class org.springframework.web.servlet.resource.resourcehttprequesthandler]
2018-01-05 08:41:29.296 info 13 --- [ost-startstop-2] o.s.w.s.handler.simpleurlhandlermapping : mapped url path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.resourcehttprequesthandler]
2018-01-05 08:41:29.591 info 13 --- [ost-startstop-2] o.s.j.e.a.annotationmbeanexporter    : registering beans for jmx exposure on startup
2018-01-05 08:41:29.616 info 13 --- [ost-startstop-2] com.example.demo.servletinitializer   : started servletinitializer in 5.421 seconds (jvm running for 551.268)
05-jan-2018 08:41:29.632 info [localhost-startstop-2] org.apache.catalina.startup.hostconfig.deploywar deployment of web application archive [/usr/local/tomcat/webapps/demo2.war] has finished in [7,459] ms

证明我们的猜想和实际情况是一致的

这样,我们就可以在宿主主机下根据不同的容器建立对应的便于记忆的目录路径,有发布需求的时候,直接将war包拷贝到宿主主机目录即可,容器叫什么名字,对应的容器的发布路径在哪里就与发布

总结

以上所述是小编给大家介绍的docker容器挂载宿主主机目录的操作方法,希望对大家有所帮助