Mac上的Docker Machine:在docker主机/ docker-machine上看不到装载的卷?物理存储在哪里? [英] Docker Machine on Mac: Cannot see mounted Volumes on docker host/docker-machine? Where are volumes physically stored?

查看:129
本文介绍了Mac上的Docker Machine:在docker主机/ docker-machine上看不到装载的卷?物理存储在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Macbook Pro笔记本电脑上运行,并运行docker-machine(0.5.0)和 docker-compose(1.5.0),以使我的容器运行。 p>

这意味着我使用docker-machine来创建我的virtualbox boot2docker驱动的HOST机器,它将运行我的docker守护程序并托管所有的容器。



我认为我错过了关于HOSTS和VOLUME概念的重要内容,因为它们引用了Docker和文档。



这是我的docker-compose.yml文件(web只是构建php:5.6-apache图像):

  web:
restart:always
build:./docker-containers/web
ports:
- 8080:80
卷:
- ./src:/var/www/html
链接:
- mysql:mysql

mysql:
重新启动:always
image:mysql:5.7
volumes_from:
- data
ports:
- 3306:3306
environment:
- MYSQL_ROOT_PASSWORD = XXX

数据:
重新启动:否
图像:mysql:5.7
卷:
- / var / lib / mysql
命令:true

Docker卷的撰写文件记录在这里: http://docs.docker.com/compose/compose-file/



它规定卷 - 将路径作为卷安装,可选地指定主机(HOST:CONTAINER)上的路径或访问模式(HOST:CONTAINER: ro)。



HOST在这种情况下是指我的VM由docker-machine创建的,是否正确?还是我的本地macbook文件系统?将VM上的路径放在容器上?



网页下,我声明:

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

这是将我的macbook pro上的本地macbook文件系统 ./ src 文件夹映射到我的Web容器。如果我的理解是正确的,不应该将我的 VM 上的 ./ src 文件夹映射到 / var / www / html 网络容器?在理论上我认为我应该要求我的本地mac文件系统文件夹 ./ src 首先到我的VM,然后我做这个卷声明。似乎docker-compose是神奇地做这一切的吗?



最后,我们可以看到,我正在创建一个仅数据的容器来保存我的mysql数据。我已经声明:

 卷:
- / var / lib / mysql

不应该在我的HOST boot2docker VM上创建一个 / var / lib / mysql 文件夹,然后我可以导航到VM上的此文件夹,是/否?当我将docker机器ssh使用到我的机器中,然后导航到 / var / lib 时,根本就没有 NO mysql文件夹?为什么没有被创建?我的配置有问题吗? :/



提前感谢!关于我在这里做错什么的任何解释将不胜感激!

解决方案

确定有几点需要在这里要解决。



让我们从一个Docker卷开始(在这一点上尝试不要考虑你的macbook或流氓机器,只要注意这一事实Docker使用不同的文件系统,此时可能会驻留在此处):
可能想像这样,Docker中的每个卷本身就是Docker使用的内部文件系统的一部分。
容器可以使用这些卷,就像它们是可以由它们安装的小硬盘一样,也可以在它们之间共享(或者由它们中的两个安装在同一时间,如安装一个超快速版本的一些ftp服务器到两个客户端或其他任何东西:P)。



原则上,您可以声明这些卷(仍然没有考虑到您的电脑/流氓本身,只是码头工人))通过Dockerfile的VOLUME指令。
标准示例,运行一个这样的webserver容器:

  FROM:nginx 
VOLUME / www

现在,所有进入/ www的东西在理论上都可以从容器中安装和卸载,并且也安装到多个容器。
现在Nginx单纯是无聊的,所以我们想让php运行在nginx存储的文件,以产生一些更有趣的内容。 =>我们需要将该卷加载到一些php-fpm容器中。
在我们的撰写文件中我们会这样做Ergo $ /

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

=>瞧!在nginx / web容器中由VOLUME指令声明的每个文件夹将在php文件中可见。重要的是要注意这里,无论在nginx / www中,将覆盖/ www中的任何php。
如果你放在:ro,php甚至不能写到该文件夹​​:)



现在接近你的问题,有第二种方式声明卷,不需要在Docker文件中声明它们。这可以通过从主机装载卷(在这种情况下是你的流氓/ boo2docker thingy)来完成。让我们来讨论一下,就好像我们首先运行在一个本机的Linux上。



如果你要这样做:

 卷:
- / home / myuser / folder:/ folder


您的docker-compose.yml中的

,这将意味着/ home / myuser /文件夹现在将被挂载到docker中。它将覆盖docker在/文件夹中的任何东西,就像/ www也可以从声明它的东西进行访问。现在的Linux机器docker守护程序正在运行。



这么多的理论:),其实你可能只需要以下建议,让你的东西去:) :



boot2docker / docker-machine / kitematic和所有这些事情处理这个问题的方法是简单的,他们首先只是在流氓机器中安装一个卷docker容器,他们也只是把这个东西挂载到你的Mac文件系统中,希望它们都可以实现:P



现在我们使用这个(或者只是试图帮助他们的同事进入甜蜜的Docker的世界:P)在Mac上面临的是权限。我的意思是想想(root或一些其他用户处理容器中的文件,用户vagrant可能处理流氓主机中的文件,然后您的Mac用户skalfyfan处理Mac中的这些文件,它们都有不同的用户标识和whatnot = >许多问题随之而来,有些取决于您在Docker中实际运行的内容,Mysql和Apache特别痛苦,因为它们不以root身份运行在容器中,这意味着它们往往无法写入Mac文件系统。



在尝试下面的第二个方法之前,只需尝试将容器卷放在Mac主目录下,这将解决MySQL在大多数情况下的问题,因为我发现时间
Btw:不需要声明卷的完整路径./folder相对于您的docker-compose.yml所驻留的地方很好阅读



只需将compose-yml放在Mac用户文件夹中,这就是重要的。没有chmod 777 -R:P会帮助你e,它只需要在你的家庭文件夹下:)



仍然有一些应用程序(例如Apache)仍然会给你一个困难的时刻。事实上,任何运行在容器中的用户标识符与您的Mac用户标识不同将使您的生活陷入困境。为了解决这个问题,您需要调整用户标识和用户组,方式与Mac的权限不冲突。在Mac上想要的组是工作人员,可以使用一个UID,例如1000.
因此,您可以将其放在Dockerfile的末尾:

 运行usermod -u 1000 www-data 
运行usermod -G工作人员www-data

 运行usermod -u 1000 mysql 
运行usermod -G员工mysql

所以你现在学到了:


理论上我认为我应该要求我的本地mac文件
系统文件夹./src首先到我的VM,然后我做这个卷
声明。似乎docker-compose是一个奇迹般的做,一切
虽然?


就这样做:)


最后,我们可以看到,我正在创建一个仅数据的容器来保存
我的mysql数据。我已经声明:
卷:
- / var / lib / mysql


:)如上所述,如果不给主机文件夹,那么Doc​​ker将会保留这个路径。但只有这个容器,所有的都将保留在docker文件系统内。根本没有写信给主机!这将永远只会发生,如果你给容器文件夹之前的主机文件夹!



希望这有帮助:)


Am on a Macbook Pro laptop and running docker-machine (0.5.0) and docker-compose (1.5.0) to get my containers going.

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.

I think I'm missing something critical with the concept of HOSTS and VOLUME, as they refer to Docker and the documentation.

This is my docker-compose.yml file (web simply builds the php:5.6-apache image):

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/

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 in this case refers to my VM created by docker-machine, correct? Or my local macbook file system? Mounting a path on my VM to a container?

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. 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?! 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. It seems docker-compose is magically doing it all at once though? confused

Lastly, we can see that I'm creating a data-only container to persist my mysql data. 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? When I use docker-machine to ssh into my machine, and then navigate to /var/lib, there is NO mysql folder at all?! 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. 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 ).

In principal you can declare these volumes ( still not thinking about your computer/vagrant itself, just the dockers ;) ) via the Dockerfile's VOLUME instruction. Standard example, run one webserver container like so:

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. Now Nginx alone is boring, so we want to have php run over the files that nginx stores to produce some more fun content. => We need to mount that volume into some php-fpm container. 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. Important point to note here, whatever is in nginx's /www, will override whatever php has in /www. If you put the :ro, php can't even write to that folder :)

Now moving close to your issue, there's a second way to declare volumes, that does not require them being declared in the Dockerfile. This can be done by mounting volumes from the host (in this case your vagrant/boo2docker thingy). Let's discuss this as though we're running on a native Linux first.

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. It will override whatever the docker has in /folder and just like the /www also be accessible from the thing that declared it. Now the Linux machine the docker daemon is running on.

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

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. 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.

Before trying the second approach below, simply try putting your container volumes under you Mac home directory. 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!

Just put the compose-yml in your Mac users folder, that's all that matters. No chmod 777 -R :P will help you here, it just needs to be under your home folder :)

Still some apps ( Apache for example ) will still give you a hard time. The fact that the user id of whatever runs in the container differs from your Mac user id will make your life hell. 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. 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:

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. It seems docker-compose is magically doing it all at once though?

Right on, it does that :)

Lastly, we can see that I'm creating a data-only container to persist my mysql data. I've declared: volumes: - /var/lib/mysql

This one you got wrong :) As explained, if you don't give a host folder, then Docker will persist this path. But only for this container and all will stay within the docker file system. 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 :)

这篇关于Mac上的Docker Machine:在docker主机/ docker-machine上看不到装载的卷?物理存储在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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