简体   繁体   English

如何通过 docker 容器访问文件夹主机?

[英]How to access to a folder host via a docker container?

I'm actually stuck with the problem of a shared file.我实际上遇到了共享文件的问题。

I want to access my host folders so I did something like this in my Jenkinsfile :我想访问我的主机文件夹,所以我在我的 Jenkinsfile 中做了这样的事情:

sh "sudo docker run -d -it -p 16000:16000 -v /PATH/TO/MY/HOST/FOLDER/:/var/tmp --name botvolume MY_IMAGE"

And in my scala class I'm trying to get all the folders by using :在我的 Scala 课程中,我尝试使用以下方法获取所有文件夹:

val folders: Array[File] = new File("/PATH/TO/MY/HOST/FOLDER/") 
  .listFiles
  .filter(_.isDirectory)

But it's doesn't work, it returns a size equal to 0.但它不起作用,它返回一个等于 0 的大小。

I read the docker volumes documentation我阅读了 docker 卷文档

Can someone help me?有人能帮我吗?

Inside the container the volume path is /var/tmp在容器内,卷路径是/var/tmp

Then your code should be :那么你的代码应该是:

val folders: Array[File] = new File("/var/tmp/") 
  .listFiles
  .filter(_.isDirectory)

You can find the detail on the Docker volume bind syntax on the link you provided:您可以在您提供的链接上找到有关 Docker 卷绑定语法的详细信息:

-v or --volume: Consists of three fields, separated by colon characters (:) . -v 或 --volume:由三个字段组成,以冒号(:) 分隔 The fields must be in the correct order, and the meaning of each field is not immediately obvious.字段必须按正确的顺序排列,每个字段的含义不是很明显。

  • In the case of bind mounts, the first field is the path to the file or directory on the host machine.在绑定挂载的情况下,第一个字段是主机上文件或目录的路径。
  • The second field is the path where the file or directory will be mounted in the container .第二个字段是文件或目录将在容器中安装的路径
  • The third field is optional, and is a comma-separated list of options第三个字段是可选的,是一个逗号分隔的选项列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM