本文翻譯自:How to mount a host directory in a Docker container
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 Dockerfile
FROM ubuntu:trusty
RUN apt-get update
RUN apt-get -y install git curl vim
CMD ["/bin/bash"]
WORKDIR /test_container
VOLUME ["/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 kB
Uploading context
Step 0 : FROM ubuntu:trusty
---> 99ec81b80c55
Step 1 : RUN apt-get update
---> Using cache
---> 1c7282005040
Step 2 : RUN apt-get -y install git curl vim
---> Using cache
---> aed48634e300
Step 3 : CMD ["/bin/bash"]
---> Running in d081b576878d
---> 65db8df48595
Step 4 : WORKDIR /test_container
---> Running in 5b8d2ccd719d
---> 250369b30e1f
Step 5 : VOLUME ["/test_container"]
---> Running in 72ca332d9809
---> 163deb2b1bc5
Successfully built 163deb2b1bc5
Removing intermediate container b8bfcb071441
Removing intermediate container d081b576878d
Removing intermediate container 5b8d2ccd719d
Removing 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
[email protected]:/test_container# ls -al
total 8
drwx------ 2 root root 4096 Apr 29 05:15 .
drwxr-xr-x 66 root root 4096 Apr 29 05:15 ..
[email protected]:/test_container# exit exit
kishore$ docker -v
Docker version 0.9.1, build 867b2a9
- I don't know how to check boot2docker version 我不知道如何檢查boot2docker版本
Questions, issues facing:
問題,面臨的問題:- How do I need to link the main_folder to the test_container folder present inside the docker container? 我該如何将main_folder連結到docker容器内的test_container檔案夾?
- I need to make this automatically. 我需要自動進行此操作。 How do I to do that without really using the
command? 如何在不真正使用run -d -v
指令的情況下做到這一點?run -d -v
- What happens if the boot2docker crashes? 如果boot2docker崩潰怎麼辦? Where are the Docker files stored (apart from Dockerfile)? Docker檔案存儲在何處(除了Dockerfile)?
#1樓
參考:https://stackoom.com/question/1aLaQ/如何在Docker容器中挂載主機目錄
#2樓
There are a couple ways you can do this.
您可以通過幾種方法來執行此操作。The simplest way to do so is to use the dockerfile
ADD
command like so:
最簡單的方法是使用dockerfileADD
指令,如下所示: 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
#3樓
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 docker documentation .
事實證明,它已經在docker文檔中進行了解釋。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)。#4樓
boot2docker together with VirtualBox Guest Additions
boot2docker以及VirtualBox來賓添加How to mount /Users into boot2docker
如何将/使用者挂載到boot2dockerhttps://medium.com/boot2docker-lightweight-linux-for-docker/boot2docker-together-with-virtualbox-guest-additions-da1e3ab2465c
https://medium.com/boot2docker-lightweight-linux-for-docker/boot2docker-together-with-virtualbox-guest-additions-da1e3ab2465ctl;dr Build your own custom boot2docker.iso with VirtualBox Guest Additions (see link ) or download http://static.dockerfiles.io/boot2docker-v1.0.1-virtualbox-guest-additions-v4.3.12.iso and save it to ~/.boot2docker/boot2docker.iso. tl; dr使用VirtualBox Guest Additions(請參閱連結 )建構自己的自定義boot2docker.iso或下載下傳http://static.dockerfiles.io/boot2docker-v1.0.1-virtualbox-guest-additions-v4.3.12.iso并儲存到〜/ .boot2docker / boot2docker.iso。
#5樓
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進行任何更改,它将對容器可見,反之亦然。#6樓
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不可見)。