在Docker共享卷中创建符号并在主机中访问它 [英] Creating a symbolic in shared volume of docker and accessing it in host machine

查看:50
本文介绍了在Docker共享卷中创建符号并在主机中访问它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在docker内的主机的安装卷中创建符号链接.但是我无法在主机上访问它.有可能做到这一点.如果是的话,我该怎么办.

我使用以下命令挂载目录

  docker run -it --rm --net主机-v $ {pwd):/workspace --name myproject my-container:dev 

然后我使用创建了一个符号链接

  import osfname ='/workspace/log/project_info_hostinfo_timeinfo_exe_param.log'symlink_name ='/workspace/log/project_info.log'os.symlink(fname,symlink_name) 

现在,当我尝试查看日志信息时,它看起来像

  $ lslrwxrwxrwx 1根根66 Mar 4 14:54 project_info.log->/workspace/log/project_info_hostinfo_timeinfo_exe_param.log-rw-r--r-- 1个根目录206 Mar 4 14:54 project_info_hostinfo_timeinfo_exe_param.log 

但是当我尝试打开文件时,我收到类似消息

  $ tail -f project_info.log尾巴:无法打开"project_info.log"进行读取:没有此类文件或目录尾巴:没有剩余文件 

解决方案

您可以使用所需的任何路径名创建符号链接.当您访问它时,它在其自身的上下文中用作普通文件系统路径.如果是相对路径,则相对于链接的位置进行访问.如果您在多个上下文中具有相同的文件系统(主机和容器中都有一个绑定安装的Docker目录;一个远程文件系统),则符号链接可能会在一个上下文中正确解析,而在另一个上下文中则无法正确解析.

在您的示例中:

  1. 符号链接指向绝对路径/workspace/log/project_info_hostinfo_timeinfo_exe_param.log
  2. 在容器内部,/workspace 目录是已安装的主机目录,因此可以正常工作
  3. 在容器外部,没有/workspace 目录,因此它不起作用

在您的示例中,链接及其目标也位于同一目录中.这意味着,如果链接目标只是文件名,则将在与链接相同的目录中查找它.这样可以避免绝对路径不同的问题.

  import os#如果链接目标位于同一目录中,则只需使用文件名,#不是绝对路径fname ='project_info_hostinfo_timeinfo_exe_param.log'symlink_name ='/workspace/log/project_info.log'os.symlink(fname,symlink_name) 

创建符号链接作为相对路径通常会很有帮助,以避免在重新定位目录树时出现问题;例如,即使 ln -s ../assets/index.html.仍指向同一位置,即使它位于容器上下文中,或者您的同事在其工作站上具有不同的目录结构./p>

I am creating a symbolic link in mounted volume of a host machine inside a docker. But I am unable to access it in host machine. Is it possible to do it. If yes how can I do that.

I used the following command to mount directory

docker run -it --rm --net host -v $(pwd):/workspace --name myproject my-container:dev

Then I created a symbolic link using

import os
fname = '/workspace/log/project_info_hostinfo_timeinfo_exe_param.log'
symlink_name = '/workspace/log/project_info.log'
os.symlink(fname, symlink_name)

Now when I am trying to see log info it looks like

$ls
lrwxrwxrwx 1 root root   66 Mar  4 14:54 project_info.log -> /workspace/log/project_info_hostinfo_timeinfo_exe_param.log
-rw-r--r-- 1 root root  206 Mar  4 14:54 project_info_hostinfo_timeinfo_exe_param.log

But when I try to open file I got message like

$tail -f project_info.log
tail: cannot open 'project_info.log' for reading: No such file or directory
tail: no files remaining

解决方案

You can create a symbolic link with any path name you want. When you access this, it's used as a normal filesystem path in its own context; if it's a relative path, it's accessed relative to the location of the link. If you have the same filesystem in multiple contexts (a bind-mounted Docker directory in both the host and a container; a remote filesystem) it's possible a symlink will resolve correctly in one context but not the other.

In your example:

  1. The symlink points at the absolute path /workspace/log/project_info_hostinfo_timeinfo_exe_param.log
  2. Inside the container, the /workspace directory is the mounted host directory, so it works
  3. Outside the container, there is no /workspace directory, so it doesn't work

Also in your example, the link and its target are in the same directory. This means that if the link target is just a filename, it will be looked up in the same directory as the link. That avoids the problem of the absolute paths being different.

import os
# If the link target is in the same directory, just use a filename,
# not an absolute path
fname = 'project_info_hostinfo_timeinfo_exe_param.log'
symlink_name = '/workspace/log/project_info.log'
os.symlink(fname, symlink_name)

It's often helpful to create a symlink as a relative path to avoid problems with relocating directory trees; ln -s ../assets/index.html ., for example, will still point at the same place even if it's in a container context or your colleague has a different directory structure on their workstation.

这篇关于在Docker共享卷中创建符号并在主机中访问它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆