本文共 6916 字,大约阅读时间需要 23 分钟。
本文翻译自:
I am trying to mount a host directory into a Docker container so that any updates done on the host is reflected into the Docker containers. 我正在尝试将主机目录挂载到Docker容器中,以便主机上完成的所有更新都将反映到Docker容器中。
Where am I doing something wrong. 我在哪里做错了。 Here is what I did: 这是我所做的:
kishore$ cat DockerfileFROM ubuntu:trustyRUN apt-get updateRUN apt-get -y install git curl vimCMD ["/bin/bash"]WORKDIR /test_containerVOLUME ["/test_container"]
kishore$ tree.├── Dockerfile└── main_folder ├── tfile1.txt ├── tfile2.txt ├── tfile3.txt └── tfile4.txt
1 directory, 5 files kishore$ pwd /Users/kishore/tdock
kishore$ docker build --tag=k3_s3:latest .
Uploading context 7.168 kBUploading contextStep 0 : FROM ubuntu:trusty ---> 99ec81b80c55Step 1 : RUN apt-get update ---> Using cache ---> 1c7282005040Step 2 : RUN apt-get -y install git curl vim ---> Using cache ---> aed48634e300Step 3 : CMD ["/bin/bash"] ---> Running in d081b576878d ---> 65db8df48595Step 4 : WORKDIR /test_container ---> Running in 5b8d2ccd719d ---> 250369b30e1fStep 5 : VOLUME ["/test_container"] ---> Running in 72ca332d9809 ---> 163deb2b1bc5Successfully built 163deb2b1bc5Removing intermediate container b8bfcb071441Removing intermediate container d081b576878dRemoving intermediate container 5b8d2ccd719dRemoving intermediate container 72ca332d9809
kishore$ docker run -d -v /Users/kishore/main_folder:/test_container k3_s3:latest
c9f9a7e09c54ee1c2cc966f15c963b4af320b5203b8c46689033c1ab8872a0ea
kishore$ docker run -i -t k3_s3:latest /bin/bash
root@0f17e2313a46:/test_container# ls -altotal 8drwx------ 2 root root 4096 Apr 29 05:15 .drwxr-xr-x 66 root root 4096 Apr 29 05:15 ..
root@0f17e2313a46:/test_container# exit exit
kishore$ docker -vDocker version 0.9.1, build 867b2a9
Questions, issues facing: 问题,面临的问题:
run -d -v
command? 如何在不真正使用run -d -v
命令的情况下做到这一点? 参考:
There are a couple ways you can do this. 您可以通过几种方法来执行此操作。 The simplest way to do so is to use the dockerfile ADD
command like so: 最简单的方法是使用dockerfile ADD
命令,如下所示:
ADD . /path/inside/docker/container
However, any changes made to this directory on the host after building the dockerfile will not show up in the container. 但是,构建dockerfile之后,主机上对此目录所做的任何更改都不会显示在容器中。 This is because when building a container, docker compresses the directory into a .tar
and uploads that context into the container permanently. 这是因为在构建容器时,docker将目录压缩为.tar
并将该上下文永久上传到容器中。
The second way to do this is the way you attempted, which is to mount a volume. 执行此操作的第二种方法是您尝试的方法,即装入卷。 Due to trying to be as portable as possible you cannot map a host directory to a docker container directory within a dockerfile, because the host directory can change depending on which machine you are running on. 由于尝试尽可能地可移植,您无法将主机目录映射到dockerfile中的docker容器目录,因为主机目录可以根据运行的机器而更改。 To map a host directory to a docker container directory you need to use the -v
flag when using docker run like so: 要将主机目录映射到Docker容器目录,需要在使用docker run时使用-v
标志,如下所示:
docker run -v /host/directory:/container/directory -other -options image_name command_to_run
Is it possible that you use docker on OS X via boot2docker
or something similar. 是否有可能通过boot2docker
或类似方式在OS X上使用boot2docker
。
I've made the same experience - the command is correct but nothing (sensible) is mounted in the container, anyway. 我也有相同的经验-该命令是正确的,但是无论如何,容器中没有安装任何(明智的)命令。
As it turns out - it's already explained in the . 事实证明,它已经在中进行了解释。 When you type docker run -v /var/logs/on/host:/var/logs/in/container ...
then /var/logs/on/host
is actually mapped from the boot2docker
VM-image, not your Mac. 当您键入docker run -v /var/logs/on/host:/var/logs/in/container ...
/var/logs/on/host
实际上是从boot2docker
VM映像而不是Mac映射的。
You'll have to pipe the shared folder through your VM to your actual host (the Mac in my case). 您必须将共享文件夹通过您的VM通过管道传输到实际主机(在我的情况下为Mac)。
boot2docker together with VirtualBox Guest Additions boot2docker以及VirtualBox来宾添加
How to mount /Users into boot2docker 如何将/用户挂载到boot2docker
tl;dr Build your own custom boot2docker.iso with VirtualBox Guest Additions (see ) or download and save it to ~/.boot2docker/boot2docker.iso. tl; dr使用VirtualBox Guest Additions(请参阅 )构建自己的自定义boot2docker.iso或下载并保存到〜/ .boot2docker / boot2docker.iso。
you can use -v option from cli, this facility is not available via Dockerfile 您可以使用cli中的-v选项,该功能无法通过Dockerfile使用
docker run -t -i -v <host_dir>:<container_dir> ubuntu /bin/bash
where host_dir is the directory from host which you want to mount. 其中host_dir是要挂载的主机目录。 you don't need to worry about directory of container if it doesn't exist docker will create it. 您无需担心容器的目录(如果不存在),docker将创建它。
If you do any changes in host_dir from host machine (under root privilege) it will be visible to container and vice versa. 如果您从主机(在root特权下)对host_dir进行任何更改,它将对容器可见,反之亦然。
How do I link the main_folder to the test_container folder present inside the docker container? 如何将main_folder链接到docker容器中存在的test_container文件夹?
Your command below is correct, unless your on a mac using boot2docker(depending on future updates) in which case you may find the folder empty. 您的以下命令是正确的,除非在Mac上使用boot2docker(取决于将来的更新),在这种情况下,您可能会发现该文件夹为空。 See mattes answer for a tutorial on correcting this. 有关更正此问题的教程,请参见遮罩答案。
docker run -d -v /Users/kishore/main_folder:/test_container k3_s3:latest
I need to make this run automatically, how to do that without really using the run -d -v command. 我需要自动运行该程序,如何在不真正使用run -d -v命令的情况下执行此操作。
You can't really get away from using these commands, they are intrinsic to the way docker works. 您真的无法摆脱使用这些命令的麻烦,它们是docker工作方式所固有的。 You would be best off putting them into a shell script to save you writing them out repeatedly. 您最好将它们放入shell脚本中,以免您反复将它们写出来。
What happens if boot2docker crashes? 如果boot2docker崩溃怎么办? Where are the docker files stored? Docker文件存储在哪里?
If you manage to use the -v arg and reference your host machine then the files will be safe on your host. 如果您设法使用-v arg并引用您的主机,则文件将在您的主机上安全。
If you've used 'docker build -t myimage .' 如果您使用了'docker build -t myimage'。 with a Dockerfile then your files will be baked into the image. 使用Dockerfile,您的文件将被烘焙到映像中。
Your docker images, i believe, are stored in the boot2docker-vm. 我相信您的docker映像存储在boot2docker-vm中。 I found this out when my images disappeared when i delete the vm from VirtualBox. 当我从VirtualBox删除虚拟机时图像消失时,我发现了这一点。 (Note, i don't know how Virtualbox works, so the images might be still hidden somewhere else, just not visible to docker). (请注意,我不知道Virtualbox的工作方式,因此图像可能仍隐藏在其他地方,只是对docker不可见)。
转载地址:http://qicnb.baihongyu.com/