如何在docker机器中安装本地卷

时间:2021-09-10 21:37:26

I am trying to use docker-machine with docker-compose. The file docker-compose.yml has definitions as follows:

我正在尝试使用docker-machine与docker-compose。 docker-compose.yml文件的定义如下:

web:
  build: .
  command: ./run_web.sh
  volumes:
    - .:/app
  ports:
    - "8000:8000"
  links:
    - db:db
    - rabbitmq:rabbit
    - redis:redis

When running docker-compose up -d all goes well until trying to execute the command and an error is produced:

当运行docker-compose up -d一切顺利,直到尝试执行命令并产生错误:

Cannot start container b58e2dfa503b696417c1c3f49e2714086d4e9999bd71915a53502cb6ef43936d: [8] System error: exec: "./run_web.sh": stat ./run_web.sh: no such file or directory

无法启动容器b58e2dfa503b696417c1c3f49e2714086d4e9999bd71915a53502cb6ef43936d:[8]系统错误:exec:“。/ run_web.sh”:stat ./run_web.sh:没有这样的文件或目录

Local volumes are not mounted to the remote machine. Whats the recommended strategy to mount the local volumes with the webapps' code?

本地卷未安装到远程计算机。什么是使用webapps代码挂载本地卷的推荐策略?

10 个解决方案

#1


Also ran into this issue and it looks like local volumes are not mounted when using docker-machine. A hack solution is to

还遇到了这个问题,看起来使用docker-machine时没有挂载本地卷。黑客解决方案是

  1. get the current working directory of the docker-machine instance docker-machine ssh <name> pwd

    获取docker-machine实例docker-machine ssh pwd的当前工作目录

  2. use a command line tool like rsync to copy folder to remote system

    使用像rsync这样的命令行工具将文件夹复制到远程系统

    rsync -avzhe ssh --progress <name_of_folder> username@remote_ip:<result _of_pwd_from_1>.
    

The default pwd is /root so the command above would be rsync -avzhe ssh --progress <name_of_folder> username@remote_ip:/root

默认的pwd是/ root所以上面的命令是rsync -avzhe ssh --progress username @ remote_ip:/ root

NB: you would need to supply the password for the remote system. You can quickly create one by ssh into the remote system and creating a password.

注意:您需要提供远程系统的密码。您可以通过ssh快速创建一个到远程系统并创建密码。

  1. change the volume mount point in your docker-compose.yml file from .:/app to /root/<name_of_folder>:/app

    将docker-compose.yml文件中的卷装入点从。:/ app更改为/ root / :/ app

  2. run docker-compose up -d

    运行docker-compose up -d

NB when changes are made locally, don't forget to rerun rsync to push the changes to the remote system.

注意,在本地进行更改时,不要忘记重新运行rsync以将更改推送到远程系统。

Its not perfect but it works. An issue is ongoing https://github.com/docker/machine/issues/179

它不完美,但它的工作原理。一个问题正在进行中https://github.com/docker/machine/issues/179

Other project that attempt to solve this include docker-rsync

尝试解决此问题的其他项目包括docker-rsync

#2


Docker-machine automounts the users directory... But sometimes that just isn't enough.

Docker-machine自动挂载用户目录......但有时候这还不够。

I don't know about docker 1.6, but in 1.8 you CAN add an additional mount to docker-machine

我不知道docker 1.6,但在1.8中你可以为docker-machine添加一个额外的mount

Add Virtual Machine Mount Point (part 1)

CLI: (Only works when machine is stopped)

CLI :(仅在机器停止时有效)

VBoxManage sharedfolder add <machine name/id> --name <mount_name> --hostpath <host_dir> --automount

VBoxManage共享文件夹添加 --name --hostpath --automount

So an example in windows would be

所以Windows中的一个例子就是

/c/Program\ Files/Oracle/VirtualBox/VBoxManage.exe sharedfolder add default --name e --hostpath 'e:\' --automount

GUI: (does NOT require the machine be stopped)

GUI :(不要求机器停止)

  1. Start "Oracle VM VirtualBox Manager"
  2. 启动“Oracle VM VirtualBox Manager”

  3. Right-Click <machine name> (default)
  4. 右键单击 <机器名> (默认)

  5. Settings...
  6. Shared Folders
  7. The Folder+ Icon on the Right (Add Share)
  8. 右侧的文件夹+图标(添加共享)

  9. Folder Path: <host dir> (e:)
  10. 文件夹路径: <主机目录> (e :)

  11. Folder Name: <mount name> (e)
  12. 文件夹名称: <装载名称> (e)

  13. Check on "Auto-mount" and "Make Permanent" (Read only if you want...) (The auto-mount is sort of pointless currently...)
  14. 检查“自动安装”和“永久安装”(只读,如果你想......)(自动安装目前有点无意义......)

Mounting in boot2docker (part 2)

Manually mount in boot2docker:

手动挂载在boot2docker中:

  1. There are various ways to log in, use "Show" in "Oracle VM VirtualBox Manager", or ssh/putty into docker by IP address docker-machine ip default, etc...
  2. 有多种方式可以登录,在“Oracle VM VirtualBox Manager”中使用“Show”,或者通过IP地址docker-machine ip default等将ssh / putty用于docker ...

  3. sudo mkdir -p <local_dir>
  4. sudo mkdir -p

  5. sudo mount -t vboxsf -o defaults,uid=`id -u docker`,gid=`id -g docker` <mount_name> <local_dir>
  6. sudo mount -t vboxsf -o defaults,uid =`id -u docker`,gid =`id -g docker`

But this is only good until you restart the machine, and then the mount is lost...

但这只有在你重新启动机器,然后安装丢失之后才有用......

Adding an automount to boot2docker:

将自动挂载添加到boot2docker:

While logged into the machine

登录机器时

  1. Edit/create (as root) /mnt/sda1/var/lib/boot2docker/bootlocal.sh, sda1 may be different for you...
  2. 编辑/创建(以root用户身份)/mnt/sda1/var/lib/boot2docker/bootlocal.sh,sda1可能与您有所不同......

  3. Add

    mkdir -p <local_dir>
    mount -t vboxsf -o defaults,uid=`id -u docker`,gid=`id -g docker` <mount_name> <local_dir>
    
  4. 添加mkdir -p mount -t vboxsf -o defaults,uid =`id -u docker`,gid =`id -g docker`

With these changes, you should have a new mount point. This is one of the few files I could find that is called on boot and is persistent. Until there is a better solution, this should work.

通过这些更改,您应该有一个新的挂载点。这是我能找到的少数几个在启动时调用的文件之一,并且是持久的。在有更好的解决方案之前,这应该有效。


Old method: Less recommended, but left as an alternative

旧方法:少推荐,但作为替代方案

  • Edit (as root) /mnt/sda1/var/lib/boot2docker/profile, sda1 may be different for you...
  • 编辑(以root用户身份)/ mnt / sda1 / var / lib / boot2docker / profile,sda1可能与您有所不同......

  • Add

    add_mount() {
      if ! grep -q "try_mount_share $1 $2" /etc/rc.d/automount-shares ; then
        echo "try_mount_share $1 $2" >> /etc/rc.d/automount-shares
      fi
    }
    
    add_mount <local dir> <mount name>
    
  • 添加add_mount(){   如果! grep -q“try_mount_share $ 1 $ 2”/etc/rc.d/automount-shares;然后     echo“try_mount_share $ 1 $ 2”>> /etc/rc.d/automount-shares   科幻 } add_mount

As a last resort, you can take the slightly more tedious alternative, and you can just modify the boot image.

作为最后的手段,您可以采用稍微繁琐的替代方案,您只需修改启动映像即可。

  • git -c core.autocrlf=false clone https://github.com/boot2docker/boot2docker.git
  • git -c core.autocrlf = false clone https://github.com/boot2docker/boot2docker.git

  • cd boot2docker
  • git -c core.autocrlf=false checkout v1.8.1 #or your appropriate version
  • git -c core.autocrlf = false checkout v1.8.1#或您的相应版本

  • Edit rootfs/etc/rc.d/automount-shares
  • Add try_mount_share <local_dir> <mount_name> line right before fi at the end. For example

    在fi之前添加try_mount_share 行。例如

    try_mount_share /e e
    

    Just be sure not to set the to anything the os needs, like /bin, etc...

    请确保不要设置os需要的任何内容,例如/ bin等...

  • docker build -t boot2docker . #This will take about an hour the first time :(
  • docker build -t boot2docker。 #这将是第一次约一个小时:(

  • docker run --rm boot2docker > boot2docker.iso
  • docker run --rm boot2docker> boot2docker.iso

  • Backup the old boot2docker.iso and copy your new one in its place, in ~/.docker/machine/machines/
  • 备份旧的boot2docker.iso并将其复制到其位置~~ .docker / machine / machines /

This does work, it's just long and complicated

这确实有效,它只是漫长而复杂

docker version 1.8.1, docker-machine version 0.4.0

docker version 1.8.1,docker-machine version 0.4.0

#3


At the moment I can't really see any way to mount volumes on machines, so the approach by now would be to somehow copy or sync the files you need into the machine.

目前我无法真正看到在机器上安装卷的任何方法,因此现在的方法是以某种方式将您需要的文件复制或同步到机器中。

There are conversations on how to solve this issue on the docker-machine's github repo. Someone made a pull request implementing scp on docker-machine and it's already merged on master, so it's very likely that the next release will include it.

有关如何在docker-machine的github repo上解决这个问题的对话。有人在docker-machine上发出了一个执行scp的pull请求,它已经在master上合并了,所以下一个版本很可能会包含它。

Since it's not yet released, by now I would recommend that if you have your code hosted on github, just clone your repo before you run the app

由于它尚未发布,现在我建议如果您的代码托管在github上,只需在运行应用程序之前克隆您的repo

web:
  build: .
  command: git clone https://github.com/my/repo.git; ./repo/run_web.sh
  volumes:
    - .:/app
  ports:
    - "8000:8000"
  links:
    - db:db
    - rabbitmq:rabbit
    - redis:redis

Update: Looking further I found that the feature is already available in the latest binaries, when you get them you'll be able to copy your local project running a command like this:

更新:进一步观察我发现该功能已经在最新的二进制文件中可用,当你获得它们时,你将能够复制运行如下命令的本地项目:

docker-machine scp -r . dev:/home/docker/project

Being this the general form:

这是一般形式:

docker-machine scp [machine:][path] [machine:][path]

So you can copy files from, to and between machines.

因此,您可以从机器复制文件,在机器之间复制文件。

Cheers!1

#4


If you choose the rsync option with docker-machine, you can combine it with the docker-machine ssh <machinename> command like this:

如果您选择带有docker-machine的rsync选项,则可以将它与docker-machine ssh 命令组合使用,如下所示:

rsync -rvz --rsh='docker-machine ssh <machinename>' --progress <local_directory_to_sync_to> :<host_directory_to_sync_to>

rsync -rvz --rsh ='docker-machine ssh ' - progress :

It uses this command format of rsync, leaving HOST blank:

它使用rsync的这种命令格式,将HOST留空:

rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST

rsync [OPTION] ... SRC [SRC] ... [USER @] HOST:DEST

(http://linuxcommand.org/man_pages/rsync1.html)

#5


Since October 2017 there is a new command for docker-machine that does the trick, but make sure there is nothing in the directory before executing it, otherwise it might get lost:

自2017年10月以来,有一个针对docker-machine的新命令可以完成这项工作,但确保在执行之前目录中没有任何内容,否则它可能会丢失:

docker-machine mount <machine-name>:<guest-path> <host-path>

docker-machine mount :

Check the docs for more information: https://docs.docker.com/machine/reference/mount/

有关更多信息,请查看文档:https://docs.docker.com/machine/reference/mount/

PR with the change: https://github.com/docker/machine/pull/4018

公关改变:https://github.com/docker/machine/pull/4018

#6


I assume the run_web.sh file is in the same directory as your docker-compose.yml file. Then the command should be command: /app/run_web.sh.

我假设run_web.sh文件与docker-compose.yml文件位于同一目录中。然后命令应该是命令:/app/run_web.sh。

Unless the Dockerfile (that you are not disclosing) takes care of putting the run_web.sh file into the Docker image.

除非Dockerfile(您没有透露)负责将run_web.sh文件放入Docker镜像。

#7


After summarize posts here, attached updated script, to create additional host mount point and automount when Virtualbox restart. The working environment brief as below: - Windows 7 - docker-machine.exe version 0.7.0 - VirtualBox 5.0.22

在此处汇总帖子后,附加更新的脚本,以在Virtualbox重新启动时创建其他主机挂载点和自动挂载。工作环境简述如下: - Windows 7 - docker-machine.exe版本0.7.0 - VirtualBox 5.0.22

    #!env bash

    : ${NAME:=default}
    : ${SHARE:=c/Proj}
    : ${MOUNT:=/c/Proj}
    : ${VBOXMGR:=C:\Program Files\Oracle\VirtualBox\VBoxManage.exe}
    SCRIPT=/mnt/sda1/var/lib/boot2docker/bootlocal.sh

    ## set -x
    docker-machine stop $NAME
    "$VBOXMGR" sharedfolder add $NAME --name c/Proj --hostpath 'c:\' --automount 2>/dev/null || :
    docker-machine start $NAME
    docker-machine env $NAME

    docker-machine ssh $NAME 'echo "mkdir -p $MOUNT" | sudo tee $SCRIPT'
    docker-machine ssh $NAME 'echo "sudo mount -t vboxsf -o rw,user $SHARE $MOUNT" |  sudo tee -a $SCRIPT'
    docker-machine ssh $NAME 'sudo chmod +x /mnt/sda1/var/lib/boot2docker/bootlocal.sh'
    docker-machine ssh $NAME 'sudo /mnt/sda1/var/lib/boot2docker/bootlocal.sh'
    #docker-machine ssh $NAME 'ls $MOUNT'

#8


Finally figured out how to upgrade Windows Docker Toolbox to v1.12.5 and keep my volumes working by adding a shared folder in Oracle VM VirtualBox manager and disabling path conversion. If you have Windows 10+ then you're best to use the newer Docker for Windows.

最后想出了如何将Windows Docker Toolbox升级到v1.12.5并通过在Oracle VM VirtualBox管理器中添加共享文件夹并禁用路径转换来保持我的卷工作。如果你有Windows 10+,那么你最好使用较新的Docker for Windows。

1st the upgrade Pain:

第一次升级痛苦:

  1. Uninstall VirtualBox first.
    • Yep that may break stuff in other tools like Android Studio. Thanks Docker :(
    • 是的,这可能会破坏其他工具,如Android Studio。谢谢Docker :(

  2. 首先卸载VirtualBox。是的,这可能会破坏其他工具,如Android Studio。谢谢Docker :(

  3. Install new version of Docker Toolbox.
  4. 安装新版本的Docker Toolbox。

Redis Database Example: redis: image: redis:alpine container_name: redis ports: - "6379" volumes: - "/var/db/redis:/data:rw"

Redis数据库示例:redis:image:redis:alpine container_name:redis ports: - “6379”卷: - “/ var / db / redis:/ data:rw”

In Docker Quickstart Terminal ....

在Docker快速启动终端....

  1. run docker-machine stop default - Ensure VM is haulted
  2. run docker-machine stop default - 确保VM被绑定

In Oracle VM VirtualBox Manager ...

在Oracle VM VirtualBox Manager中......

  1. Added a shared folder in default VM via or command line
    • D:\Projects\MyProject\db => /var/db
    • D:\ Projects \ MyProject \ db => / var / db

  2. 通过或命令行D:\ Projects \ MyProject \ db => / var / db在默认VM中添加了共享文件夹

In docker-compose.yml...

  1. Mapped redis volume as: "/var/db/redis:/data:rw"
  2. 将redis卷映射为:“/ var / db / redis:/ data:rw”

In Docker Quickstart Terminal ....

在Docker快速启动终端....

  1. Set COMPOSE_CONVERT_WINDOWS_PATHS=0 (for Toolbox version >= 1.9.0)
  2. 设置COMPOSE_CONVERT_WINDOWS_PATHS = 0(对于Toolbox版本> = 1.9.0)

  3. run docker-machine start default to restart the VM.
  4. 运行docker-machine start默认重启VM。

  5. cd D:\Projects\MyProject\
  6. docker-compose up should work now.
  7. docker-compose现在应该工作了。

Now creates redis database in D:\Projects\MyProject\db\redis\dump.rdb

现在在D:\ Projects \ MyProject \ db \ redis \ dump.rdb中创建redis数据库

Why avoid relative host paths?

为什么要避免相对主机路径

I avoided relative host paths for Windows Toolbox as they may introduce invalid '\' chars. It's not as nice as using paths relative to docker-compose.yml but at least my fellow developers can easily do it even if their project folder is elsewhere without having to hack the docker-compose.yml file (bad for SCM).

我避免了Windows Toolbox的相对主机路径,因为它们可能会引入无效的'\'字符。它不如使用相对于docker-compose.yml的路径那么好,但至少我的开发人员可以很容易地做到这一点,即使他们的项目文件夹在别处,而不必破解docker-compose.yml文件(对SCM不好)。

Original Issue

FYI ... Here is the original error I got when I used nice clean relative paths that used to work just fine for older versions. My volume mapping used to be just "./db/redis:/data:rw"

仅供参考...这是我使用漂亮干净的相对路径时得到的原始错误,这些路径过去常用于旧版本。我的卷映射曾经只是“./db/redis:/data:rw”

ERROR: for redis Cannot create container for service redis: Invalid bind mount spec "D:\\Projects\\MyProject\\db\\redis:/data:rw": Invalid volume specification: 'D:\Projects\MyProject\db\redis:/data

错误:for redis无法为服务redis创建容器:无效的绑定装入规范“D:\\ Projects \\ MyProject \\ db \\ redis:/ data:rw”:无效的卷规范:'D:\ Projects \ MyProject \ db \ redis的:/数据

This breaks for two reasons ..

这打破了两个原因..

  1. It can't access D: drive
  2. 它无法访问D:驱动器

  3. Volume paths can't include \ characters
    • docker-compose adds them and then blames you for it !!
    • docker-compose添加它们然后责怪你!

    • Use COMPOSE_CONVERT_WINDOWS_PATHS=0 to stop this nonsense.
    • 使用COMPOSE_CONVERT_WINDOWS_PATHS = 0来停止这种废话。

  4. 卷路径不能包含\ characters docker-compose添加它们然后责备你!使用COMPOSE_CONVERT_WINDOWS_PATHS = 0来停止这种废话。

I recommend documenting your additional VM shared folder mapping in your docker-compose.yml file as you may need to uninstall VirtualBox again and reset the shared folder and anyway your fellow devs will love you for it.

我建议您在docker-compose.yml文件中记录您的其他VM共享文件夹映射,因为您可能需要再次卸载VirtualBox并重置共享文件夹,无论如何,您的伙伴开发人员都会喜欢它。

#9


I am using docker-machine 0.12.2 with the virtualbox drive on my local machine. I found that there is a directory /hosthome/$(user name) from where you have access to local files.

我在我的本地机器上使用docker-machine 0.12.2和virtualbox驱动器。我发现有一个目录/ hosthome / $(用户名),您可以从中访问本地文件。

#10


Just thought I'd mention I've been using 18.03.1-ce-win65 (17513) on Windows 10 and I noticed that if you've previously shared a drive and cached the credentials, once you change your password docker will start having the volumes mounted within containers as blank.

刚想到我提到我在Windows 10上一直使用18.03.1-ce-win65(17513)而且我注意到如果你之前共享过一个驱动器并缓存了凭据,一旦你更改密码,docker将开始拥有容器中安装的卷为空白。

It gives no indication that what is actually happening is that it is now failing to access the shared with the old cached credentials. The solution in this scenario is to reset the credentials either through the UI (Settings->Shared drives) or to disable then renable drive sharing and enter the new password.

它没有表明实际发生的事情是它现在无法使用旧的缓存凭据访问共享。此方案中的解决方案是通过UI(设置 - >共享驱动器)重置凭据,或者禁用然后可重命名的驱动器共享并输入新密码。

It would be useful if docker-compose gave an error in these situations.

如果docker-compose在这些情况下出错,那将会很有用。

#1


Also ran into this issue and it looks like local volumes are not mounted when using docker-machine. A hack solution is to

还遇到了这个问题,看起来使用docker-machine时没有挂载本地卷。黑客解决方案是

  1. get the current working directory of the docker-machine instance docker-machine ssh <name> pwd

    获取docker-machine实例docker-machine ssh pwd的当前工作目录

  2. use a command line tool like rsync to copy folder to remote system

    使用像rsync这样的命令行工具将文件夹复制到远程系统

    rsync -avzhe ssh --progress <name_of_folder> username@remote_ip:<result _of_pwd_from_1>.
    

The default pwd is /root so the command above would be rsync -avzhe ssh --progress <name_of_folder> username@remote_ip:/root

默认的pwd是/ root所以上面的命令是rsync -avzhe ssh --progress username @ remote_ip:/ root

NB: you would need to supply the password for the remote system. You can quickly create one by ssh into the remote system and creating a password.

注意:您需要提供远程系统的密码。您可以通过ssh快速创建一个到远程系统并创建密码。

  1. change the volume mount point in your docker-compose.yml file from .:/app to /root/<name_of_folder>:/app

    将docker-compose.yml文件中的卷装入点从。:/ app更改为/ root / :/ app

  2. run docker-compose up -d

    运行docker-compose up -d

NB when changes are made locally, don't forget to rerun rsync to push the changes to the remote system.

注意,在本地进行更改时,不要忘记重新运行rsync以将更改推送到远程系统。

Its not perfect but it works. An issue is ongoing https://github.com/docker/machine/issues/179

它不完美,但它的工作原理。一个问题正在进行中https://github.com/docker/machine/issues/179

Other project that attempt to solve this include docker-rsync

尝试解决此问题的其他项目包括docker-rsync

#2


Docker-machine automounts the users directory... But sometimes that just isn't enough.

Docker-machine自动挂载用户目录......但有时候这还不够。

I don't know about docker 1.6, but in 1.8 you CAN add an additional mount to docker-machine

我不知道docker 1.6,但在1.8中你可以为docker-machine添加一个额外的mount

Add Virtual Machine Mount Point (part 1)

CLI: (Only works when machine is stopped)

CLI :(仅在机器停止时有效)

VBoxManage sharedfolder add <machine name/id> --name <mount_name> --hostpath <host_dir> --automount

VBoxManage共享文件夹添加 --name --hostpath --automount

So an example in windows would be

所以Windows中的一个例子就是

/c/Program\ Files/Oracle/VirtualBox/VBoxManage.exe sharedfolder add default --name e --hostpath 'e:\' --automount

GUI: (does NOT require the machine be stopped)

GUI :(不要求机器停止)

  1. Start "Oracle VM VirtualBox Manager"
  2. 启动“Oracle VM VirtualBox Manager”

  3. Right-Click <machine name> (default)
  4. 右键单击 <机器名> (默认)

  5. Settings...
  6. Shared Folders
  7. The Folder+ Icon on the Right (Add Share)
  8. 右侧的文件夹+图标(添加共享)

  9. Folder Path: <host dir> (e:)
  10. 文件夹路径: <主机目录> (e :)

  11. Folder Name: <mount name> (e)
  12. 文件夹名称: <装载名称> (e)

  13. Check on "Auto-mount" and "Make Permanent" (Read only if you want...) (The auto-mount is sort of pointless currently...)
  14. 检查“自动安装”和“永久安装”(只读,如果你想......)(自动安装目前有点无意义......)

Mounting in boot2docker (part 2)

Manually mount in boot2docker:

手动挂载在boot2docker中:

  1. There are various ways to log in, use "Show" in "Oracle VM VirtualBox Manager", or ssh/putty into docker by IP address docker-machine ip default, etc...
  2. 有多种方式可以登录,在“Oracle VM VirtualBox Manager”中使用“Show”,或者通过IP地址docker-machine ip default等将ssh / putty用于docker ...

  3. sudo mkdir -p <local_dir>
  4. sudo mkdir -p

  5. sudo mount -t vboxsf -o defaults,uid=`id -u docker`,gid=`id -g docker` <mount_name> <local_dir>
  6. sudo mount -t vboxsf -o defaults,uid =`id -u docker`,gid =`id -g docker`

But this is only good until you restart the machine, and then the mount is lost...

但这只有在你重新启动机器,然后安装丢失之后才有用......

Adding an automount to boot2docker:

将自动挂载添加到boot2docker:

While logged into the machine

登录机器时

  1. Edit/create (as root) /mnt/sda1/var/lib/boot2docker/bootlocal.sh, sda1 may be different for you...
  2. 编辑/创建(以root用户身份)/mnt/sda1/var/lib/boot2docker/bootlocal.sh,sda1可能与您有所不同......

  3. Add

    mkdir -p <local_dir>
    mount -t vboxsf -o defaults,uid=`id -u docker`,gid=`id -g docker` <mount_name> <local_dir>
    
  4. 添加mkdir -p mount -t vboxsf -o defaults,uid =`id -u docker`,gid =`id -g docker`

With these changes, you should have a new mount point. This is one of the few files I could find that is called on boot and is persistent. Until there is a better solution, this should work.

通过这些更改,您应该有一个新的挂载点。这是我能找到的少数几个在启动时调用的文件之一,并且是持久的。在有更好的解决方案之前,这应该有效。


Old method: Less recommended, but left as an alternative

旧方法:少推荐,但作为替代方案

  • Edit (as root) /mnt/sda1/var/lib/boot2docker/profile, sda1 may be different for you...
  • 编辑(以root用户身份)/ mnt / sda1 / var / lib / boot2docker / profile,sda1可能与您有所不同......

  • Add

    add_mount() {
      if ! grep -q "try_mount_share $1 $2" /etc/rc.d/automount-shares ; then
        echo "try_mount_share $1 $2" >> /etc/rc.d/automount-shares
      fi
    }
    
    add_mount <local dir> <mount name>
    
  • 添加add_mount(){   如果! grep -q“try_mount_share $ 1 $ 2”/etc/rc.d/automount-shares;然后     echo“try_mount_share $ 1 $ 2”>> /etc/rc.d/automount-shares   科幻 } add_mount

As a last resort, you can take the slightly more tedious alternative, and you can just modify the boot image.

作为最后的手段,您可以采用稍微繁琐的替代方案,您只需修改启动映像即可。

  • git -c core.autocrlf=false clone https://github.com/boot2docker/boot2docker.git
  • git -c core.autocrlf = false clone https://github.com/boot2docker/boot2docker.git

  • cd boot2docker
  • git -c core.autocrlf=false checkout v1.8.1 #or your appropriate version
  • git -c core.autocrlf = false checkout v1.8.1#或您的相应版本

  • Edit rootfs/etc/rc.d/automount-shares
  • Add try_mount_share <local_dir> <mount_name> line right before fi at the end. For example

    在fi之前添加try_mount_share 行。例如

    try_mount_share /e e
    

    Just be sure not to set the to anything the os needs, like /bin, etc...

    请确保不要设置os需要的任何内容,例如/ bin等...

  • docker build -t boot2docker . #This will take about an hour the first time :(
  • docker build -t boot2docker。 #这将是第一次约一个小时:(

  • docker run --rm boot2docker > boot2docker.iso
  • docker run --rm boot2docker> boot2docker.iso

  • Backup the old boot2docker.iso and copy your new one in its place, in ~/.docker/machine/machines/
  • 备份旧的boot2docker.iso并将其复制到其位置~~ .docker / machine / machines /

This does work, it's just long and complicated

这确实有效,它只是漫长而复杂

docker version 1.8.1, docker-machine version 0.4.0

docker version 1.8.1,docker-machine version 0.4.0

#3


At the moment I can't really see any way to mount volumes on machines, so the approach by now would be to somehow copy or sync the files you need into the machine.

目前我无法真正看到在机器上安装卷的任何方法,因此现在的方法是以某种方式将您需要的文件复制或同步到机器中。

There are conversations on how to solve this issue on the docker-machine's github repo. Someone made a pull request implementing scp on docker-machine and it's already merged on master, so it's very likely that the next release will include it.

有关如何在docker-machine的github repo上解决这个问题的对话。有人在docker-machine上发出了一个执行scp的pull请求,它已经在master上合并了,所以下一个版本很可能会包含它。

Since it's not yet released, by now I would recommend that if you have your code hosted on github, just clone your repo before you run the app

由于它尚未发布,现在我建议如果您的代码托管在github上,只需在运行应用程序之前克隆您的repo

web:
  build: .
  command: git clone https://github.com/my/repo.git; ./repo/run_web.sh
  volumes:
    - .:/app
  ports:
    - "8000:8000"
  links:
    - db:db
    - rabbitmq:rabbit
    - redis:redis

Update: Looking further I found that the feature is already available in the latest binaries, when you get them you'll be able to copy your local project running a command like this:

更新:进一步观察我发现该功能已经在最新的二进制文件中可用,当你获得它们时,你将能够复制运行如下命令的本地项目:

docker-machine scp -r . dev:/home/docker/project

Being this the general form:

这是一般形式:

docker-machine scp [machine:][path] [machine:][path]

So you can copy files from, to and between machines.

因此,您可以从机器复制文件,在机器之间复制文件。

Cheers!1

#4


If you choose the rsync option with docker-machine, you can combine it with the docker-machine ssh <machinename> command like this:

如果您选择带有docker-machine的rsync选项,则可以将它与docker-machine ssh 命令组合使用,如下所示:

rsync -rvz --rsh='docker-machine ssh <machinename>' --progress <local_directory_to_sync_to> :<host_directory_to_sync_to>

rsync -rvz --rsh ='docker-machine ssh ' - progress :

It uses this command format of rsync, leaving HOST blank:

它使用rsync的这种命令格式,将HOST留空:

rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST

rsync [OPTION] ... SRC [SRC] ... [USER @] HOST:DEST

(http://linuxcommand.org/man_pages/rsync1.html)

#5


Since October 2017 there is a new command for docker-machine that does the trick, but make sure there is nothing in the directory before executing it, otherwise it might get lost:

自2017年10月以来,有一个针对docker-machine的新命令可以完成这项工作,但确保在执行之前目录中没有任何内容,否则它可能会丢失:

docker-machine mount <machine-name>:<guest-path> <host-path>

docker-machine mount :

Check the docs for more information: https://docs.docker.com/machine/reference/mount/

有关更多信息,请查看文档:https://docs.docker.com/machine/reference/mount/

PR with the change: https://github.com/docker/machine/pull/4018

公关改变:https://github.com/docker/machine/pull/4018

#6


I assume the run_web.sh file is in the same directory as your docker-compose.yml file. Then the command should be command: /app/run_web.sh.

我假设run_web.sh文件与docker-compose.yml文件位于同一目录中。然后命令应该是命令:/app/run_web.sh。

Unless the Dockerfile (that you are not disclosing) takes care of putting the run_web.sh file into the Docker image.

除非Dockerfile(您没有透露)负责将run_web.sh文件放入Docker镜像。

#7


After summarize posts here, attached updated script, to create additional host mount point and automount when Virtualbox restart. The working environment brief as below: - Windows 7 - docker-machine.exe version 0.7.0 - VirtualBox 5.0.22

在此处汇总帖子后,附加更新的脚本,以在Virtualbox重新启动时创建其他主机挂载点和自动挂载。工作环境简述如下: - Windows 7 - docker-machine.exe版本0.7.0 - VirtualBox 5.0.22

    #!env bash

    : ${NAME:=default}
    : ${SHARE:=c/Proj}
    : ${MOUNT:=/c/Proj}
    : ${VBOXMGR:=C:\Program Files\Oracle\VirtualBox\VBoxManage.exe}
    SCRIPT=/mnt/sda1/var/lib/boot2docker/bootlocal.sh

    ## set -x
    docker-machine stop $NAME
    "$VBOXMGR" sharedfolder add $NAME --name c/Proj --hostpath 'c:\' --automount 2>/dev/null || :
    docker-machine start $NAME
    docker-machine env $NAME

    docker-machine ssh $NAME 'echo "mkdir -p $MOUNT" | sudo tee $SCRIPT'
    docker-machine ssh $NAME 'echo "sudo mount -t vboxsf -o rw,user $SHARE $MOUNT" |  sudo tee -a $SCRIPT'
    docker-machine ssh $NAME 'sudo chmod +x /mnt/sda1/var/lib/boot2docker/bootlocal.sh'
    docker-machine ssh $NAME 'sudo /mnt/sda1/var/lib/boot2docker/bootlocal.sh'
    #docker-machine ssh $NAME 'ls $MOUNT'

#8


Finally figured out how to upgrade Windows Docker Toolbox to v1.12.5 and keep my volumes working by adding a shared folder in Oracle VM VirtualBox manager and disabling path conversion. If you have Windows 10+ then you're best to use the newer Docker for Windows.

最后想出了如何将Windows Docker Toolbox升级到v1.12.5并通过在Oracle VM VirtualBox管理器中添加共享文件夹并禁用路径转换来保持我的卷工作。如果你有Windows 10+,那么你最好使用较新的Docker for Windows。

1st the upgrade Pain:

第一次升级痛苦:

  1. Uninstall VirtualBox first.
    • Yep that may break stuff in other tools like Android Studio. Thanks Docker :(
    • 是的,这可能会破坏其他工具,如Android Studio。谢谢Docker :(

  2. 首先卸载VirtualBox。是的,这可能会破坏其他工具,如Android Studio。谢谢Docker :(

  3. Install new version of Docker Toolbox.
  4. 安装新版本的Docker Toolbox。

Redis Database Example: redis: image: redis:alpine container_name: redis ports: - "6379" volumes: - "/var/db/redis:/data:rw"

Redis数据库示例:redis:image:redis:alpine container_name:redis ports: - “6379”卷: - “/ var / db / redis:/ data:rw”

In Docker Quickstart Terminal ....

在Docker快速启动终端....

  1. run docker-machine stop default - Ensure VM is haulted
  2. run docker-machine stop default - 确保VM被绑定

In Oracle VM VirtualBox Manager ...

在Oracle VM VirtualBox Manager中......

  1. Added a shared folder in default VM via or command line
    • D:\Projects\MyProject\db => /var/db
    • D:\ Projects \ MyProject \ db => / var / db

  2. 通过或命令行D:\ Projects \ MyProject \ db => / var / db在默认VM中添加了共享文件夹

In docker-compose.yml...

  1. Mapped redis volume as: "/var/db/redis:/data:rw"
  2. 将redis卷映射为:“/ var / db / redis:/ data:rw”

In Docker Quickstart Terminal ....

在Docker快速启动终端....

  1. Set COMPOSE_CONVERT_WINDOWS_PATHS=0 (for Toolbox version >= 1.9.0)
  2. 设置COMPOSE_CONVERT_WINDOWS_PATHS = 0(对于Toolbox版本> = 1.9.0)

  3. run docker-machine start default to restart the VM.
  4. 运行docker-machine start默认重启VM。

  5. cd D:\Projects\MyProject\
  6. docker-compose up should work now.
  7. docker-compose现在应该工作了。

Now creates redis database in D:\Projects\MyProject\db\redis\dump.rdb

现在在D:\ Projects \ MyProject \ db \ redis \ dump.rdb中创建redis数据库

Why avoid relative host paths?

为什么要避免相对主机路径

I avoided relative host paths for Windows Toolbox as they may introduce invalid '\' chars. It's not as nice as using paths relative to docker-compose.yml but at least my fellow developers can easily do it even if their project folder is elsewhere without having to hack the docker-compose.yml file (bad for SCM).

我避免了Windows Toolbox的相对主机路径,因为它们可能会引入无效的'\'字符。它不如使用相对于docker-compose.yml的路径那么好,但至少我的开发人员可以很容易地做到这一点,即使他们的项目文件夹在别处,而不必破解docker-compose.yml文件(对SCM不好)。

Original Issue

FYI ... Here is the original error I got when I used nice clean relative paths that used to work just fine for older versions. My volume mapping used to be just "./db/redis:/data:rw"

仅供参考...这是我使用漂亮干净的相对路径时得到的原始错误,这些路径过去常用于旧版本。我的卷映射曾经只是“./db/redis:/data:rw”

ERROR: for redis Cannot create container for service redis: Invalid bind mount spec "D:\\Projects\\MyProject\\db\\redis:/data:rw": Invalid volume specification: 'D:\Projects\MyProject\db\redis:/data

错误:for redis无法为服务redis创建容器:无效的绑定装入规范“D:\\ Projects \\ MyProject \\ db \\ redis:/ data:rw”:无效的卷规范:'D:\ Projects \ MyProject \ db \ redis的:/数据

This breaks for two reasons ..

这打破了两个原因..

  1. It can't access D: drive
  2. 它无法访问D:驱动器

  3. Volume paths can't include \ characters
    • docker-compose adds them and then blames you for it !!
    • docker-compose添加它们然后责怪你!

    • Use COMPOSE_CONVERT_WINDOWS_PATHS=0 to stop this nonsense.
    • 使用COMPOSE_CONVERT_WINDOWS_PATHS = 0来停止这种废话。

  4. 卷路径不能包含\ characters docker-compose添加它们然后责备你!使用COMPOSE_CONVERT_WINDOWS_PATHS = 0来停止这种废话。

I recommend documenting your additional VM shared folder mapping in your docker-compose.yml file as you may need to uninstall VirtualBox again and reset the shared folder and anyway your fellow devs will love you for it.

我建议您在docker-compose.yml文件中记录您的其他VM共享文件夹映射,因为您可能需要再次卸载VirtualBox并重置共享文件夹,无论如何,您的伙伴开发人员都会喜欢它。

#9


I am using docker-machine 0.12.2 with the virtualbox drive on my local machine. I found that there is a directory /hosthome/$(user name) from where you have access to local files.

我在我的本地机器上使用docker-machine 0.12.2和virtualbox驱动器。我发现有一个目录/ hosthome / $(用户名),您可以从中访问本地文件。

#10


Just thought I'd mention I've been using 18.03.1-ce-win65 (17513) on Windows 10 and I noticed that if you've previously shared a drive and cached the credentials, once you change your password docker will start having the volumes mounted within containers as blank.

刚想到我提到我在Windows 10上一直使用18.03.1-ce-win65(17513)而且我注意到如果你之前共享过一个驱动器并缓存了凭据,一旦你更改密码,docker将开始拥有容器中安装的卷为空白。

It gives no indication that what is actually happening is that it is now failing to access the shared with the old cached credentials. The solution in this scenario is to reset the credentials either through the UI (Settings->Shared drives) or to disable then renable drive sharing and enter the new password.

它没有表明实际发生的事情是它现在无法使用旧的缓存凭据访问共享。此方案中的解决方案是通过UI(设置 - >共享驱动器)重置凭据,或者禁用然后可重命名的驱动器共享并输入新密码。

It would be useful if docker-compose gave an error in these situations.

如果docker-compose在这些情况下出错,那将会很有用。