本文翻译自: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 DockerfileFROM 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)?

#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: 最简单的方法是使用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

#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 如何将/用户挂载到boot2docker

https://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-da1e3ab2465c

tl;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不可见)。

如何在Docker容器中挂载主机目录相关推荐

  1. 如何在Docker容器中运行GUI程序

    如何在Docker容器中运行GUI程序 各位,今天我们将学习如何在Docker之中运行GUI程序.我们可以轻易地在Docker容器中运行大多数GUI程序且不出错.Docker是一个开源项目,提供了一个 ...

  2. Docker容器中挂载NFS共享目录

    之前在https://blog.csdn.net/fengbingchun/article/details/110561129 介绍过使用Dockerfile构建ubuntu 16.04镜像,并在容器 ...

  3. 如何在Docker容器中运行Docker [3种方法]

    在本博客中,我将向您介绍在docker中运行docker所需的三种不同方法. Docker In Docker的用处 dockerIndocker的一个潜在用处是CI管道,在代码成功构建后,您需要在其 ...

  4. sigterm信号_详解如何在 docker 容器中捕获信号

    概述 玩过docker的朋友可能都使用过 docker stop 命令来停止正在运行的容器,有些会使用 docker kill 命令强行关闭容器或者把某个信号传递给容器中的进程.这些操作的本质都是通过 ...

  5. Beats:如何在 Docker 容器中运行 Filebeat

    今天在这篇博客中,我们将学习如何在容器环境中运行 Filebeat. 为了快速了解 Filebeat 是做什么用的: Filebeat用于转发和集中日志数据 它重量轻,小型化,使用的资源更少 它作为代 ...

  6. 面试官:如何在 Docker 容器中抓包?问倒一大片。。。。

    点击下方名片,设为星标! 回复"1024"获取2TB学习资源! 如何在容器中进行抓包?一篇文章搞懂其原理! 大家好,我是民工哥,今天就来同大家一起来学习一下,在容器中进行抓包的命令 ...

  7. docker容器找到挂载的目录,以及服务器对应物理地址

    docker inspect 容器id 出现一堆,找到下面的地方 ......."GraphDriver": {"Data": {"LowerDir& ...

  8. 如何在Docker容器中安装RabbitMQ

    1.Docker环境 视频教程:https://www.bilibili.com/video/BV1xv4y1S7kA/ 2.搜索镜像 https://hub.docker.com/网站搜索rabbi ...

  9. Docker容器中数据两种持久化存储方式:卷和挂载宿主目录

    镜像使用的是分层存储,容器也是如此.每一个容器运行时,是以镜像为基础层,在其上创建一个当前容器的存储层,我们可以称这个为容器运行时读写而准备的存储层为容器存储层.容器存储层的生存周期和容器一样,容器消 ...

最新文章

  1. windows下sqlite3的基本操作
  2. Linux 多线程应用中编写安全的信号处理函数
  3. ES6语法规则之解构
  4. python sort 多级排序_为什么在python中使用排序功能进行多级排序...
  5. 创建工程师文化的3个步骤 | IDCF
  6. count函数里加函数_PHP count()函数与示例
  7. 机械齿轮网站404单页源码
  8. 吴恩达深度学习4.2练习_Convolutional Neural Networks_Happy House Residual Networks
  9. 二分--求最小值的最大p1m2
  10. TCP和TCP/IP的区别
  11. pttools内置浏览器访问PT站无法登录
  12. 网络封包分析软件-WildPackets OmniPeek Workgroup Pro
  13. 输入一个大写字母,显示三角形
  14. java固件包_iOS13 各版本固件下载地址以及更新方法
  15. 花旗银行java面试_花旗金融—面经(已offer)
  16. LSP标识符(LSP ID)
  17. 升级windows11的方法
  18. uniapp使用第三方文字,本地文字
  19. 【wifi移植 1】 ap6210 wifi模块移植
  20. RocksDB数据库简介及使用分享

热门文章

  1. Flutter编译时生成代码之 code_builder
  2. Android 代码设置调试等待
  3. Java排序算法(1)
  4. 第十五周程序阅读-范型程序设计(5)
  5. HwServiceManager篇-Android10.0 HwBinder通信原理(五)
  6. 解读基本数据类型和内置方法(2)
  7. P1171 售货员的难题--搜索(剪枝)
  8. vuejs,angularjs,reactjs介绍
  9. 2018冬令营模拟测试赛(十八)
  10. 454. 4Sum II