如何在 Docker 容器中挂载主机目录 [英] How to mount a host directory in a Docker container

查看:58
本文介绍了如何在 Docker 容器中挂载主机目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将主机目录挂载到 Docker 容器中,以便在主机上完成的任何更新都反映到 Docker 容器中.

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.

我哪里做错了.这是我所做的:

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个目录,5个文件kishore$密码/用户/kishore/tdock

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:latestc9f9a7e09c54ee1c2cc966f15c963b4af320b5203b8c46689033c1ab8872a0ea

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退出

root@0f17e2313a46:/test_container# exit exit

kishore$ docker -v
Docker version 0.9.1, build 867b2a9

  • 我不知道如何查看 boot2docker 版本

问题、面临的问题:

  1. 如何将 main_folder 链接到 docker 容器内的 test_container 文件夹?
  2. 我需要自动制作.如何在不真正使用 run -d -v 命令的情况下做到这一点?
  3. 如果 boot2docker 崩溃会怎样?Docker 文件存储在哪里(除了 Dockerfile)?
  1. How do I need to link the main_folder to the test_container folder present inside the docker container?
  2. I need to make this automatically. How do I to do that without really using the run -d -v command?
  3. What happens if the boot2docker crashes? Where are the Docker files stored (apart from Dockerfile)?

推荐答案

有几种方法可以做到这一点.最简单的方法是使用 dockerfile ADD 命令,如下所示:

There are a couple ways you can do this. The simplest way to do so is to use the dockerfile ADD command like so:

ADD . /path/inside/docker/container

但是,在构建 dockerfile 后对主机上的此目录所做的任何更改都不会显示在容器中.这是因为在构建容器时,docker 会将目录压缩到 .tar 并将该 context 永久上传到容器中.

However, any changes made to this directory on the host after building the dockerfile will not show up in the container. This is because when building a container, docker compresses the directory into a .tar and uploads that context into the container permanently.

第二种方法是您尝试的方法,即安装卷.由于尝试尽可能可移植,您无法将主机目录映射到 dockerfile 中的 docker 容器目录,因为主机目录可能会根据您运行的机器而改变.要将主机目录映射到 docker 容器目录,您需要在使用 docker run 时使用 -v 标志,例如:

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. To map a host directory to a docker container directory you need to use the -v flag when using docker run, e.g.,:

# Run a container using the `alpine` image, mount the `/tmp`
# directory from your host into the `/container/directory`
# directory in your container, and run the `ls` command to
# show the contents of that directory.
docker run 
    -v /tmp:/container/directory 
    alpine 
    ls /container/directory

这篇关于如何在 Docker 容器中挂载主机目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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