如何从本机传送一个目录到docker image中
通过build docker image 添加文件时,需要用到ADD或COPY关键字,但是ADD或COPY后面跟的只能是相对于Dockerfile文件所在的目录的路径的目录或文件,有没有一种方法可以通过绝对路径来传送目录或文件?(貌似docker里面有一个cp命令,但是只能从container中复制文件或目录到本机中,并不能从本机到container中。(为什么??))
Answers
-
Dockerfile 中
ADD
和COPY
命令不能使用绝对路径,只能使用相对路径。这点在官方文档中说的比较清楚 https://docs.docker.com/reference/builder/#add 。Multiple resource may be specified but if they are files or directories then they must be relative to the source directory that is being built (the context of the build).
-
如果不是非要把文件 build 到 image里,可以通过 run container 的时候通过 -v 参数将 host 文件或目录加载/共享到 container 里。 参考 https://docs.docker.com/userguide/dockervolumes/#mount-a-host-directory-as-a-data-volume