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

有一项重要的参数 -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容器挂载宿主主机目录的操作方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!

您可能感兴趣的文章:

  • 详解挂载运行的docker容器中如何挂载文件系统
  • docker运行容器远程挂载卷的方法
  • 浅谈Docker 容器数据卷挂载小结
(0)

相关推荐

  • 详解挂载运行的docker容器中如何挂载文件系统

    前言 感觉最近很多人都在问docker相关的问题,关于怎么操作一个已经启动的docker容器的文件系统,首先我发现这非常困难,因为 mnt的命名空间. 为了登录进入一个已经启动的docker容器,我们需要这么做: 使用nsenter来在临时挂载点上挂载整个docker容器的文件系统. 创建一个特定目录的绑定挂载来当作卷来使用. 卸载临时挂载. 好吧,开始实践. 启动一个名为charlie的docker实例: $ docker run --name charlie -ti ubuntu bash

  • docker运行容器远程挂载卷的方法

    docker容器中卷的使用 创建卷,卷本身有多种方式例如下面创建的几种: 下面是官方的基本步骤: 创建一个卷: $ docker volume create my-vol 查看: $ docker volume ls local my-vol 查看卷的详情: $ docker volume inspect my-vol [ { "Driver": "local", "Labels": {}, "Mountpoint": &qu

  • 浅谈Docker 容器数据卷挂载小结

    为了更直观了解数据卷挂载的操作,做个实验一一验证数据卷挂载的各种情况. 情况一.本地不存在文件挂载到容器存在文件 首先是当本地不存在该文件,而容器内存在该文件的情况,尝试把不存在的文件挂载到存在该文件的容器中.以一个 Alpine 镜像为例,这里把一个修改后的 Alpine 镜像打了新标签,叫做 volume_test: # 本地目录不存在 test 文件. $ docker run --name=test -v ~/test.txt:/etc/hosts -d volume_test 0cba

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

    有一项重要的参数 -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 /ho

  • docker容器与宿主机的数据交互方式总结

    前言 在生产环境中使用 Docker ,往往需要对数据进行持久化,或者需要在多个容器之间进行数据共享,这必然涉及容器的数据管理操作. 方式一.Docker cp命令 docker cp :用于容器与主机之间的数据拷贝. 语法 # 容器内文件 copy to 宿主机 docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|- # 宿主机文件 copy to 容器内 docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_

  • Docker容器使用宿主机上的mongod/redis等服务详解

    我们在本地的开发中为了保证大家都开发环境一直,PHP 的扩展,版本保持一致, 我们使用了 docker .但是由于redis/mongodb 在宿主机上, 所以通过 127.0.0.1 是连接不上的 前提说明 这个是在 docker 运行容器时使用的桥接模式(默认) 时才会发生以上问题.如果使用 host 模式就没有. 所以,我们就是要把是 docker 的网络模式设置为 host 模式. 具体实现 ~ docker run --network host php56 补充说明 使用了 host

  • 解决docker容器与宿主机相差8小时的问题

    使用docker-compose部署时,在输出的日志以及相关事件校验及输出时,导致事件与现实相差8小时. 排查问题: 1.查看宿主机时间: # 查看时间 date Wed Sep 29 11:12:44 CST 2021 # 查看时区 date -R Wed, 29 Sep 2021 11:13:34 +0800 2.进入容器查看时间 # 查看所有容器 docker ps -a # 进入容器 docker eec -it [容器ID] /bin/bash # 查看时间 date 此时看到的宿主机

  • Docker容器访问宿主机网络的方法

    最近部署一套系统,使用nginx作反向代理,其中nginx是使用docker方式运行: $ docker run -d --name nginx $PWD:/etc/nginx -p 80:80 -p 443:443 nginx:1.15 需要代理的API服务运行在宿主机的 1234 端口, nginx.conf 相关配置如下: server { ... location /api { proxy_pass http://localhost:1234 } ... } 结果访问的时候发现老是报 5

  • docker容器访问宿主机的MySQL操作

    背景: 有一个flask项目提供接口,使用docker容器构建并且运行,MySQL在宿主机运行,需要在容器中可以让flask连接上宿主机的mysql 使用ifconfig命令可以看到,有一个docker0和eth0,在docker容器中可以通过eth0的IP地址加上端口号(3306)这样就可以连接上宿主机的MySQL了:另外,nginx可以通过docker0的IP地址加上构建容器时指定的端口号进行访问容器. 补充:docker的mysql容器运行正常,但是宿主机却连不上数据库,解决方法来啦 do

  • Docker容器与主机间的文件传输方法(复制/上传/下载)

    1.首先启动容器(以first-mysql容器为例) docker start first-mysql 2.查看容器ID root@kobe:/opt/software/temp/test# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 688e83c55129 mysql "docker-entrypoint.s-" 6 days ago Up 3 seconds 0.0.0.0:3306->33

  • docker容器的创建及常用操作命令详解

    目录 docker容器简介 1.创建容器 2.查看本机的容器列表 3.停止和启动一个容器 4.删除容器 5.查看容器内容 6.复制命令 7.进入容器 docker容器简介 容器就是对外提供服务的一个实例. 容器启动的必要条件:容器内至少有一个进程运行在前台 1.创建容器 # 格式 docker run [参数] [镜像名称] [运行容器的启动命令] # 参数 -d : 以守护进程的方式运行一个容器 docker run -d [镜像名称] [cmd] --name : 指定容器的名称 docke

  • Docker容器没有权限写入宿主机目录的解决方案

    在应用docker容器的时候,更多的时候我们会把宿主机的目录挂载到docker容器中. 在宿主机的文件夹权限隶属于root时,我们需要将文件夹的权限用户进行 chown 设置,才能保证目录的内容的正常写入, 下面是一个例子: 使用的是docker版本的jenkins,运行后,出现如下错误: [root@localhost CICD]# docker logs -f jenkins touch: cannot touch '/var/jenkins_home/copy_reference_file

随机推荐