有没有人有一个Dockerfile和命令行从Linux的外部目录挂载到泊坞窗图像工作简单的例子?

我正在寻找的是Dockerfile&docker build +在linux上运行命令,通过容器内的挂载点查看/ var / tmp。 这里的问题都是复杂的情况,或者涉及OS / X&Windows,或者试图做更多的事情而不是简单的安装一个卷。 我的情况我只是试图将/ var / tmp挂载到busybox映像的/ foobar上,运行一个包含映像的容器,并使用“ls / foobar”来查看内容。

运行“Docker版本1.6.1,build立97cd073”在Linux 4.0.1 w / aufs使用本地repostory。

http://docs.docker.com/userguide/dockervolumes/

注意到:

Mount a Host Directory as a Data Volume In addition to creating a volume using the -v flag you can also mount a directory from your Docker daemon's host into a container. <snip> $ sudo docker run -d -P --name web -v /src/webapp:/opt/webapp training/webapp python app.py This will mount the host directory, /src/webapp, into the container at /opt/webapp. Note: If the path /opt/webapp already exists inside the container's image, its contents will be replaced by the contents of /src/webapp on the host to stay consistent with the expected behavior of mount This is very useful for testing, for example we can mount our source code inside the container and see our application at work as we change the source code. The directory on the host must be specified as an absolute path and if the directory doesn't exist Docker will automatically create it for you. 

我正在使用本地存储库通过“-v”开关获取其“/ data”目录到docker run:

 docker run -d -p 5000 \ -v '/var/lib/docker/registry:/data/registry/storage' \ 'kampka/registry'; 

这似乎是作为主机的/ var / lib /docker/registry目录获取条目添加到它。

所以我为自己做一个简单的testing:在主机系统上创build一个可以访问/ var / tmp的busybox的最小副本。

 FROM localhost:5000/lembark/busybox MAINTAINER lembark@wrkhors.com VOLUME [ "/foobar" ] ENV PATH /bin WORKDIR / ENTRYPOINT [ "/bin/sh" ] 

在这一点上运行“dockerbuild设”执行VOLUME命令,但不创build挂载点:

 $ docker build --tag="localhost:5000/lembark/hak" . ; Sending build context to Docker daemon 7.68 kB Sending build context to Docker daemon Step 0 : FROM localhost:5000/lembark/busybox ---> c1a1f5abbf79 Step 1 : MAINTAINER lembark@wrkhors.com ---> Using cache ---> b46677881767 Step 2 : VOLUME /foobar ---> Running in 7127bdbcfb56 ---> bcf9c3f1c441 Removing intermediate container 7127bdbcfb56 Step 3 : ENV PATH /bin ---> Running in 89f92c815860 ---> 780fea54a67f Removing intermediate container 89f92c815860 Step 4 : WORKDIR / ---> Running in aa3871c408a1 ---> 403190e9415b Removing intermediate container aa3871c408a1 Step 5 : ENTRYPOINT /bin/sh ---> Running in 4850561f7ebd ---> 77c32530b4a9 Removing intermediate container 4850561f7ebd Successfully built 77c32530b4a9 

步骤2中的“VOLUME / foobar”似乎表示在运行时应该有一个挂载点。

在这一点上使用任一

 docker run --rm -t -i localhost:5000/lembark/hak; docker run --rm -t -i -v /foobar localhost:5000/lembark/hak; docker run --rm -t -i -v /var/tmp:/foobar localhost:5000/lembark/hak; 

留给我:

 # ls -al /foobar ls: /foobar: No such file or directory 

在VOLUME之前添加一个mkdir使我具有匿名卷的/ foobar目录,而不是从/ var / tmp映射:

 ... RUN [ "mkdir", "/foobar" ] VOLUME [ "/foobar" ] 

要么

 # made a local ./foobar directory, added that to the image. COPY [ "foobar", "/foobar" ] 

这些离开这些/ foobar,但没有办法映射任何外部目录。 相反,我不断得到匿名卷:

  # mount | grep foobar; /dev/mapper/vg00-var--lib on /var/lib/docker/aufs/mnt/dd12f3e11a6fcb88627412a041b7c910e4d32dc1bf0c15330899036c59d7b3d9/foobar type xfs (rw,noatime,nodiratime,attr2,inode64,logbufs=8,logbsize=256k,logdev=/dev/vg02/var-lib-extlog,noquota) 

没有或没有每个mkdir,VOLUME,COPY或-v的组合让我查看/ var / tmp undef / foobar。

谢谢

这对我有用

Dockerfile

 FROM busybox:latest VOLUME /foo/bar 

命令

 $ docker build -t bb . Sending build context to Docker daemon 2.048 kB Sending build context to Docker daemon Step 0 : FROM busybox:latest ---> 8c2e06607696 Step 1 : VOLUME /foo/bar ---> Running in a94615dd3353 ---> fa500ba91831 Successfully built fa500ba91831 $ docker run -it -v /tmp:/foobar bb ls /foobar [contents of my /tmp directory] 

请注意,您不需要VOLUME命令来执行此操作。 docker run -it -v /tmp:/foobar busybox:latest ls /foobar也可以。

我和@Nathaniel Waisbrot一样。

 vagrant@vagrant:/code/busybox$ ls Dockerfile vagrant@vagrant:/code/busybox$ docker build -t busybox:0.0.1 . Sending build context to Docker daemon 9.216 kB Sending build context to Docker daemon Step 0 : FROM busybox:latest ---> 8c2e06607696 Step 1 : VOLUME /foobar ---> Running in 0b99eac833aa ---> a93b5ae5de5f Removing intermediate container 0b99eac833aa Successfully built a93b5ae5de5f vagrant@vagrant:/code/busybox$ docker run -i -v /code/busybox/:/foobar busybox:0.0.1 ls /foobar Dockerfile vagrant@vagrant:/code/busybox$ 

这是我用来构build图像的Dockerfile。

 FROM busybox:latest VOLUME [ "/foobar" ] 

在这里你可以看到我列出了我的/code/busybox目录来显示你的文件。
然后我build立图像。 然后我运行该映像,并使用-v /code/busybox:/foobar将my /code/busybox目录映射到images /foobar目录。 然后执行我用ls命令build立的映像和我想列出ls /foobar的目录。
结果被打印到控制台。

只是要清楚,我认为你在做什么有几个错误。 这就是为什么你没有看到这些文件。

 FROM localhost:5000/lembark/busybox MAINTAINER lembark@wrkhors.com VOLUME [ "/foobar" ] ENV PATH /bin WORKDIR / ENTRYPOINT [ "/bin/sh" ] 

您正在通过电脑上的图像创build图像。 那是什么形象? 这可能是一个问题,因为您正在构build的图像inheritance它的基本属性和O / S从该图像。 WORKDIR命令设置工作目录。 既然你不input任何东西,你不需要这个命令。 您将覆盖每次通过将入口点设置为['/ bin / sh']来初始化容器的ENTRYPOINT或命令。 这可能是你的命令没有执行的原因。 您运行容器并执行入口点。

请看上面的例子。 这是一个正确的Dockerfile,将编译并提供上面列出的控制台输出,并完全按照你的要求。 没有看到Dockerfile的图像,你正在build立你的最新的形象,很难确切地确定你有什么不正确的东西。