简体   繁体   English

Mac上的Docker Machine:在docker主机/ docker-machine上看不到挂载的卷?卷物理存储在哪里?

[英]Docker Machine on Mac: Cannot see mounted Volumes on docker host/docker-machine? Where are volumes physically stored?

Am on a Macbook Pro laptop and running docker-machine (0.5.0) and docker-compose (1.5.0) to get my containers going. 我在Macbook Pro笔记本电脑上运行docker-machine(0.5.0)docker-compose(1.5.0)来让我的容器运行。

This means I'm using docker-machine to create my virtualbox boot2docker driven HOST machines, which will run my docker daemon and host all my containers. 这意味着我正在使用docker-machine来创建我的虚拟机boot2docker驱动的HOST机器,它将运行我的docker守护程序并托管我的所有容器。

I think I'm missing something critical with the concept of HOSTS and VOLUME, as they refer to Docker and the documentation. 我想我缺少一些关于HOSTS和VOLUME概念的东西,因为它们指的是Docker和文档。

This is my docker-compose.yml file (web simply builds the php:5.6-apache image): 这是我的docker-compose.yml文件(web只是构建了php:5.6-apache图像):

web:
  restart: "always"
  build: ./docker-containers/web
  ports:
    - "8080:80"
  volumes:
    - ./src:/var/www/html
  links:
    - mysql:mysql

mysql:
  restart: "always"
  image: mysql:5.7
  volumes_from:
    - data
  ports:
    - "3306:3306"
  environment:
    - MYSQL_ROOT_PASSWORD=XXX

data:
  restart: "no"
  image: mysql:5.7
  volumes:
    - /var/lib/mysql
  command: "true" 

Docker Compose file documention for volumes is here: http://docs.docker.com/compose/compose-file/ Docker撰写卷的文件文档在这里: http//docs.docker.com/compose/compose-file/

It states for volumes - Mount paths as volumes, optionally specifying a path on the host machine (HOST:CONTAINER), or an access mode (HOST:CONTAINER:ro). 它表示卷 - 将路径挂载为卷,可选择指定主机上的路径(HOST:CONTAINER),或访问模式(HOST:CONTAINER:ro)。

HOST in this case refers to my VM created by docker-machine, correct? 在这种情况下主机是指我的由docker-machine创建的VM,对吗? Or my local macbook file system? 还是我的本地macbook文件系统? Mounting a path on my VM to a container? 将VM上的路径挂载到容器?

Under web I declare: 网上我声明:

volumes:
  - ./src:/var/www/html

and this is mapping my local macbook file system ./src folder on my macbook pro to my web container. 这是将我的macbook pro上的本地macbook文件系统./src文件夹映射到我的Web容器。 If my understanding is correct though, shouldn't it be mapping the ./src folder on my VM to /var/www/html within the web container?! 如果我的理解是正确的,不应该将我的VM上的./src文件夹映射到Web容器中的/ var / www / html吗?! In theory I think I should be required to COPY my local mac file system folder ./src to my VM first, and then I do this volume declaration. 从理论上讲,我认为我应该首先将我的本地mac文件系统文件夹./src复制到我的VM,然后我做这个卷声明。 It seems docker-compose is magically doing it all at once though? 似乎docker-compose一下子神奇地做了所有这一切? confused 困惑

Lastly, we can see that I'm creating a data-only container to persist my mysql data. 最后,我们可以看到我正在创建一个仅限数据的容器来保存我的mysql数据。 I've declared: 我宣布:

volumes:
   - /var/lib/mysql

Shouldn't this create a /var/lib/mysql folder on my HOST boot2docker VM and I could then navigate to this folder on the VM, yes/no? 这不应该在我的HOST boot2docker VM上创建一个/ var / lib / mysql文件夹,然后我可以导航到VM上的这个文件夹,是/否? When I use docker-machine to ssh into my machine, and then navigate to /var/lib , there is NO mysql folder at all?! 当我使用docker-machine ssh进入我的机器,然后导航到/ var / lib时 ,根本没有 mysql文件夹?! Why is it not being created? 为什么没有创建? Is there something wrong with my configuration? 我的配置有问题吗? :/ :/

Thanks in advance! 提前致谢! Any explanations as to what I'm doing wrong here would be greatly appreciated! 任何关于我在这里做错的解释都将不胜感激!

Ok there's a couple of points that need to be addressed here. 好的,这里有几点需要解决。

Lets start with what a docker volume is(Try to not think about your macbook or the vagrant machine at this point. Just be mindful of the fact that the dockers use a different filesystem, where ever it may reside at this point ): Maybe imagine it like this, in and of itself every volume in Docker is just a part of the internal file system docker uses. 让我们从docker卷开始(尝试不考虑你的macbook或vagrant机器。请注意Docker使用不同的文件系统,此时它可能驻留在哪里):也许想象一下就像这样,Docker中的每个卷本身就是Docker使用的内部文件系统的一部分。 The containers can use theses volumes, like they were "small harddrives" that can be mounted by them and also shared between them (or mounted by two of them at the same time, like mounting a super fast version of some ftp server to two clients or whatever :P ). 容器可以使用这些卷,就像它们是“小硬盘”一样,可以由它们安装并在它们之间共享(或者同时由两个它们安装,就像将一些ftp服务器的超快版本安装到两个客户端一样或者其他:P)。

In principal you can declare these volumes ( still not thinking about your computer/vagrant itself, just the dockers ;) ) via the Dockerfile's VOLUME instruction. 原则上你可以通过Dockerfile的VOLUME指令声明这些卷(仍然不考虑你的计算机/流浪者本身,只是码头工人;))。 Standard example, run one webserver container like so: 标准示例,运行一个webserver容器,如下所示:

FROM: nginx
VOLUME /www

Now everything that goes into /www can in theory be mounted and unmounted from a container and also mounted to multiple containers. 现在,进入/ www的所有内容理论上都可以从容器中安装和卸载,也可以安装到多个容器中。 Now Nginx alone is boring, so we want to have php run over the files that nginx stores to produce some more fun content. 现在单独使用Nginx很无聊,所以我们想让php运行nginx存储的文件以产生更多有趣的内容。 => We need to mount that volume into some php-fpm container. =>我们需要将该卷安装到某个php-fpm容器中。 Ergo in our compose file we'd do this 在我们的撰写文件中,我们会这样做

web:
  image: nginx
php:
  image: php-fpm
  volumes_from:
    - web

=> voila! 瞧瞧! every folder declared by a VOLUME directive in the nginx/web container will be visible in the php one. 由nginx / web容器中的VOLUME指令声明的每个文件夹都将在php中显示。 Important point to note here, whatever is in nginx's /www, will override whatever php has in /www. 这里要注意的重要一点,无论是在nginx的/ www中,都将覆盖/ www中的任何php。 If you put the :ro, php can't even write to that folder :) 如果你把:ro,php甚至无法写入该文件夹:)

Now moving close to your issue, there's a second way to declare volumes, that does not require them being declared in the Dockerfile. 现在接近你的问题,有第二种声明卷的方法,它不需要在Dockerfile中声明它们。 This can be done by mounting volumes from the host (in this case your vagrant/boo2docker thingy). 这可以通过从主机安装卷来完成(在这种情况下你的vagrant / boo2docker thingy)。 Let's discuss this as though we're running on a native Linux first. 让我们讨论这个问题,好像我们首先在本机Linux上运行一样。

If you were to put something like: 如果你要做类似的事情:

volumes:
 - /home/myuser/folder:/folder

in your docker-compose.yml, then this will mean that /home/myuser/folder will now be mounted into the docker. 在您的docker-compose.yml中,这意味着/ home / myuser /文件夹现在将被挂载到docker中。 It will override whatever the docker has in /folder and just like the /www also be accessible from the thing that declared it. 它将覆盖docker在/ folder中的任何内容,就像/ www也可以从声明它的东西访问它。 Now the Linux machine the docker daemon is running on. 现在运行docker守护程序的Linux机器。

So much for the theory :), in fact you probably just need the following advice to get your stuff going :): 理论上这么多:),实际上你可能只需要以下建议就可以得到你的东西:):

The way boot2docker/docker-machine/kitematic and all these things deal with the issue is simply, that they first of all just mount a volume in the vagrant machine to the docker containers, and them simply also mount this thing into your Mac file system, hoping it will all work out :P boot2docker / docker-machine / kitematic和所有这些事情处理问题的方式很简单,他们首先只是将流量计算机中的卷安装到docker容器中,它们也只是将这个东西挂载到你的Mac文件系统中,希望一切顺利:P

Now for the practical problem all of us using this (or just trying to help their coworkers into the world of sweet sweet Docker :P) on Mac are facing is permissions. 现在对于实际问题,我们所有人在Mac上使用这个(或者只是试图帮助他们的同事进入甜蜜的Docker:P世界)都面临着权限。 I mean think about it ( root or some other user handles files in the container,the user vagrant might handle files in the vagrant host and then your Mac user "skalfyfan" handles those files in Mac. They all have different user id's and whatnot => many problems ensue with that, and somewhat depending on what you're actually running in Docker. Mysql and Apache are especially painful, because they do not run as root within the container. This means, they often have trouble writing to the Mac file system. 我的意思是考虑一下(root或其他一些用户处理容器中的文件,用户流浪者可能会处理vagrant主机中的文件,然后你的Mac用户“skalfyfan”处理Mac中的那些文件。他们都有不同的用户ID和whatnot = >随之而来的很多问题,在某种程度上取决于你在Docker中实际运行的内容.Mysql和Apache特别痛苦,因为它们不能在容器中以root身份运行。这意味着,他们经常无法写入Mac文件系统。

Before trying the second approach below, simply try putting your container volumes under you Mac home directory. 在尝试下面的第二种方法之前,只需尝试将容器卷放在Mac主目录下。 This will resolve issues with MySQL in most cases as I have found over time. 在大多数情况下,这将解决我在一段时间内发现的问题。 Btw: No need to declare full paths to volumes ./folder is fine and read relative to the place your docker-compose.yml resides! 顺便说一句: 不需要声明卷的完整路径./folder很好并且相对于你的docker-compose.yml所在的位置读取!

Just put the compose-yml in your Mac users folder, that's all that matters. 只需将compose-yml放入Mac用户文件夹即可,这一切都很重要。 No chmod 777 -R :P will help you here, it just needs to be under your home folder :) 没有chmod 777 -R:P会帮助你,它只需要在你的主文件夹下:)

Still some apps ( Apache for example ) will still give you a hard time. 仍然有一些应用程序(例如Apache)仍然会给你带来困难。 The fact that the user id of whatever runs in the container differs from your Mac user id will make your life hell. 容器中运行的任何用户ID与Mac用户ID不同的事实将使您的生活变得地狱。 In order to get around this, you need to adjust the user id as well as the user group in a way that doesn't conflict with your Mac's permissions. 为了解决这个问题,您需要以不与Mac的权限冲突的方式调整用户ID和用户组。 The group you want on a Mac is staff, a UID that works would be for example 1000. Hence you could put this at the end of your Dockerfile: 在Mac上你想要的组是工作人员,工作的UID例如是1000.因此你可以将它放在你的Dockerfile的末尾:

RUN usermod -u 1000 www-data
RUN usermod -G staff www-data

or 要么

RUN usermod -u 1000 mysql
RUN usermod -G staff mysql

So as you have now learnt: 所以你现在学到了:

In theory I think I should be required to COPY my local mac file system folder ./src to my VM first, and then I do this volume declaration. 从理论上讲,我认为我应该首先将我的本地mac文件系统文件夹./src复制到我的VM,然后我做这个卷声明。 It seems docker-compose is magically doing it all at once though? 似乎docker-compose一下子神奇地做了所有这一切?

Right on, it does that :) 对,它确实:)

Lastly, we can see that I'm creating a data-only container to persist my mysql data. 最后,我们可以看到我正在创建一个仅限数据的容器来保存我的mysql数据。 I've declared: volumes: - /var/lib/mysql 我已声明:卷: - / var / lib / mysql

This one you got wrong :) As explained, if you don't give a host folder, then Docker will persist this path. 这个你错了:)正如所解释的,如果你没有给一个主机文件夹,那么Doc​​ker将坚持这条路径。 But only for this container and all will stay within the docker file system. 但只有这个容器才会保留在docker文件系统中。 Nothing is written to the host at all! 根本没有东西写给主人! This will always only happen if you give a host folder before the container folder! 只有在容器文件夹之前提供主机文件夹时才会发生这种情况!

Hope this helped :) 希望这有助于:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM