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

查看:33
本文介绍了单个文件卷挂载为 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 run --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"]

还有:

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)

通过使用$(pwd),您将获得一个确实存在的绝对路径,并尊重大小写,而不是可能不存在的文件名或路径.
不存在的主机路径将作为文件夹挂载到容器中.

By using $(pwd), you will get an absolute path which does exists, and respect the case, as opposed to a file name or path which might not exist.
An non-existing host path would be mounted as a folder in the container.

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

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