单个文件卷作为Docker中的目录安装 [英] Single file volume mounted as directory in Docker

查看:133
本文介绍了单个文件卷作为Docker中的目录安装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Docker文档说可以将单个文件安装到Docker容器中:

Docker documentation says that it's possible to mount a single file into a Docker container:


-v标志也可用于安装单个文件 - 而不是只是目录 - 从主机。

The -v flag can also be used to mount a single file - instead of just directories - from the host machine.

$ docker运行--rm -it -v〜/ .bash_history:/。bash_history ubuntu / bin / bash

这将把你放进一个新的容器中的bash shell,你将从你的主机中退出你的bash历史记录,当你退出容器中,主机将具有在容器中键入的命令的历史。

This will drop you into a bash shell in a new container, you will have your bash history from the host and when you exit the container, the host will have the history of the commands typed while in the container.

当我尝试,但是文件挂载为一个目录:

When I try that however the file mounts as a directory:

tom@u ~/project $ docker run --rm -it -v file.json:/file.json test
total 80K
drwxr-xr-x  9 root root 4.0K Dec  7 12:58 .
drwxr-xr-x 63 root root 4.0K Dec  7 12:58 ..
drwxr-xr-x  2 root root 4.0K Dec  4 16:10 file.json

我的Dockerfile如下所示:

My Dockerfile looks like this:

FROM ubuntu:14.04
MAINTAINER Tom
CMD ["ls", "-lah", "/test"]

Docker版本是1.9.1,构建a34a1d5。

Docker version is 1.9.1, build a34a1d5.

这是一个文档问题,对我的误会还是还有其他的事情?

Is this a documentation issue, a misunderstanding on my side, or is there something else going on?

推荐答案

test 是你的名字您使用 docker build -t test 构建的映像,而不是 / test 文件夹。

test is the name of your image that you have built with 'docker build -t test', not a /test folder.

尝试一个 Dockerfile 与:

CMD ["ls", "-lah", "/"]
or
CMD ["cat", "/file.json"]

And:

docker run --rm -it -v $(pwd)/file.json:/file.json test

请注意使用的 $(pwd)以完整的绝对路径装载文件(不支持相对路径)

Note the use of $(pwd) in order to mount a file with its full absolute path (relative paths are not supported)

这篇关于单个文件卷作为Docker中的目录安装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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