Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
469 views
in Technique[技术] by (71.8m points)

docker - 如何在Docker容器中挂载主机目录(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

root@0f17e2313a46:/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 ..

root@0f17e2313a46:/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:

(问题,面临的问题:)

  1. How do I need to link the main_folder to the test_container folder present inside the docker container?

    (我该如何将main_folder链接到docker容器内的test_container文件夹?)

  2. I need to make this automatically.

    (我需要自动进行此操作。)

    How do I to do that without really using the run -d -v command?

    (如何在不真正使用run -d -v命令的情况下做到这一点?)

  3. What happens if the boot2docker crashes?

    (如果boot2docker崩溃怎么办?)

    Where are the Docker files stored (apart from Dockerfile)?

    (Docker文件存储在何处(除了Dockerfile)?)

  ask by Kishore Vaishnav translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...