与主机数据目录映射时,Docker容器数据变为空白

时间:2017-02-25 19:52:05

标签: docker docker-volume

我创建了一个如下所示的Dockerfile。只需在tomcat webapps中添加一个应用程序。

FROM tomcat:9-alpine  
ADD ./Spring3HibernateApp.war /usr/local/tomcat/webapps/  
VOLUME /usr/local/tomcat/webapps  
EXPOSE 8080  
CMD ["catalina.sh","run"] 

构建新图片并将其命名为 test-app:0.1

docker build –t test-app:0.1 .

像下面一样启动新容器,我在那里使用主机数据目录来挂载容器数据目录,这样我就可以对容器进行更改或列出webapps内容。

docker run -d --name=tomcat-01 -p 80:8080 --net=bridge -v /vol2/docker/sampleapp/tomcat-webapps:/usr/local/tomcat/webapps test-app:0.1

我的问题:当我查看 / vol2 / docker / sampleapp / tomcat-webapps 时,我发现它是空白的。但是我认为它应该从位置 / usr / local / tomcat / webapps 列出容器的内容。它实际上也在清理容器数据,而不是将其持久化并将其加载到主机数据目录中。

我错过了什么吗?

如果我只是从上面的命令中删除-v,它工作正常,我能够看到默认的docker卷位置内的内容,但是当我添加-v时没有得到相同的结果。

我理解错了吗? 我指的是通过链接https://docs.docker.com/engine/tutorials/dockervolumes/

“将主机目录挂载为数据卷”

当我使用mysql映像并从中启动新容器时,同样的命令工作正常。

docker run -d --name=mysql-01 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=employeedb --net=bridge -v /vol2/docker/sampleapp/mysql-data:/var/lib/mysql mysql

这一次,我能够在 / vol2 / docker / sampleapp / mysql-data

中看到容器数据

3 个答案:

答案 0 :(得分:1)

装入卷时,会覆盖容器内的现有目录。如果您希望从容器外部部署func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { if annotation is MKUserLocation { return nil } let reuseId = "reuseid" var av = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) if av == nil { av = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId) let lbl = UILabel(frame: CGRect(x: 0, y: 0, width: 50, height: 30)) lbl.backgroundColor = .black lbl.textColor = .white lbl.alpha = 0.5 lbl.tag = 42 av?.addSubview(lbl) av?.canShowCallout = true av?.frame = lbl.frame } else { av?.annotation = annotation } let lbl = av?.viewWithTag(42) as! UILabel lbl.text = annotation.title! return av } .war文件,则需要执行以下操作:

.jar

建立它:FROM tomcat:9-alpine VOLUME /usr/local/tomcat/webapps EXPOSE 8080 CMD ["catalina.sh","run"]
然后运行您的容器,如下所示:docker build –t test-app:0.1 .docker run -d --name=tomcat-01 -p 80:8080 -v /vol2/docker/sampleapp/tomcat-webapps:/usr/local/tomcat/webapps test-app:0.1放在Spring3HibernateApp.war目录中。 完成此操作后,您可以转到/vol2/docker/sampleapp/tomcat-webapps并观看tomcat如下所示在我的sample.war中部署应用程序。

docker logs -f tomcat-01

最后,MySQL卷安装的工作原理与之相同,因为基础映像在构建Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/lib/jvm/java-1.8-openjdk/jre Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar 25-Feb-2017 21:58:23.710 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version: Apache Tomcat/9.0.0.M17 25-Feb-2017 21:58:23.717 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built: Jan 10 2017 20:59:20 UTC 25-Feb-2017 21:58:23.717 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number: 9.0.0.0 25-Feb-2017 21:58:23.717 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name: Linux 25-Feb-2017 21:58:23.717 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version: 3.13.0-93-generic 25-Feb-2017 21:58:23.717 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture: amd64 25-Feb-2017 21:58:23.717 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home: /usr/lib/jvm/java-1.8-openjdk/jre 25-Feb-2017 21:58:23.718 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version: 1.8.0_111-internal-alpine-r0-b14 25-Feb-2017 21:58:23.718 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor: Oracle Corporation 25-Feb-2017 21:58:23.718 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE: /usr/local/tomcat 25-Feb-2017 21:58:23.718 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME: /usr/local/tomcat 25-Feb-2017 21:58:23.719 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties 25-Feb-2017 21:58:23.719 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager 25-Feb-2017 21:58:23.719 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048 25-Feb-2017 21:58:23.720 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources 25-Feb-2017 21:58:23.720 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/usr/local/tomcat 25-Feb-2017 21:58:23.721 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/usr/local/tomcat 25-Feb-2017 21:58:23.721 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/usr/local/tomcat/temp 25-Feb-2017 21:58:23.721 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded APR based Apache Tomcat Native library 1.2.10 using APR version 1.5.2. 25-Feb-2017 21:58:23.721 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true]. 25-Feb-2017 21:58:23.722 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true] 25-Feb-2017 21:58:23.725 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized (OpenSSL 1.0.2j 26 Sep 2016) 25-Feb-2017 21:58:23.838 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"] 25-Feb-2017 21:58:23.861 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read 25-Feb-2017 21:58:23.868 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-nio-8009"] 25-Feb-2017 21:58:23.870 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read 25-Feb-2017 21:58:23.874 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 653 ms 25-Feb-2017 21:58:23.908 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service Catalina 25-Feb-2017 21:58:23.908 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/9.0.0.M17 25-Feb-2017 21:58:23.951 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive /usr/local/tomcat/webapps/sample.war 25-Feb-2017 22:00:25.223 INFO [localhost-startStop-1] org.apache.catalina.util.SessionIdGeneratorBase.createSecureRandom Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [120,610] milliseconds. 25-Feb-2017 22:00:25.253 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive /usr/local/tomcat/webapps/sample.war has finished in 121,302 ms 25-Feb-2017 22:00:25.258 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler [http-nio-8080] 25-Feb-2017 22:00:25.270 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler [ajp-nio-8009] 25-Feb-2017 22:00:25.278 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 121403 ms 之前已经暴露了卷,如果你想要在catalina应用程序中发生同样的事情你必须复制他们的Dockerfile并向其添加VOLUME /var/lib/mysql,如下所示。

VOLUME /usr/local/tomcat/webapps/

答案 1 :(得分:0)

我担心您的VOLUME /usr/local/tomcat/webapps会清除此目录。

你需要采用不同的方式。

答案 2 :(得分:0)

将主机目录装入容器时,该路径中容器内的任何内容都不再可用。它仍然存在于底层图像或体积中,但它将被您的卷装置取代。

这就是主机卷安装的工作方式:主机上的目录中的任何内容都被放在容器内的位置,并优先于之前在该路径上的任何内容(如果有的话)。