将主机path转换为泊坞窗容器内部path

我需要知道至less一个正在运行的docker容器可以访问位于主机上的文件的path。 从主机,我知道主机的文件的path,但不知道在容器中。
对于映射到容器中的每个文件,该解决scheme必须是有效的,并且如果给定的文件没有安装在容器内,则该scheme将失败

例如
docker工具容器运行选项-v /srv:/etc
在主机上有一个文件/srv/foo
从容器内部,这个文件被称为/etc/foo

从主机上,我怎么能预测/srv/foo会变成容器内的/etc/foo
(是的,我真的需要这样做…)


我想出了这个微小的丑陋的脚本,我正在跳跃寻找一个适当的解决scheme。 我已经知道循环吸吮,但我的问题是关于这个想法。

  containerid="123abc" hostpath="/srv/foo" dockerpath="" # <-- How to set that? # -- For all mapped directories (host side) for hostdir in $(docker inspect --format '{{ range .Mounts }}{{ .Source }} {{ end }}' ${containerid}); do # -- If hostpath is inside hostdir (considering symlinks...) case $(readlink -f ${hostpath})/ in $(readlink -f ${hostdir})/*) # -- Obtain the location inside the container for hostdir dockerdir=$(docker inspect --format "{{ range .Mounts }}{{ if eq .Source \"${hostdir}\" }}{{ .Destination }}{{ end }}{{ end }}" ${containerid}) # -- Deduce the path into the container realhpath=$(readlink -f $hostpath) hpathsubstraction=${realhpath#$hostdir} dockerpath="${dockerdir}/${hpathsubstraction}" # -- Break the case and the "mapped directories" for-loop break 1 esac done # Here if $dockerpath is empty, then $hostpath can not be seen inside $containerid.