简体   繁体   English

如何在 Docker 容器中挂载主机目录

[英]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 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 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 除外)?

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 , eg,:要将主机目录映射到 docker 容器目录,您需要在使用docker run时使用-v标志,例如:

# 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

The user of this question was using Docker version 0.9.1, build 867b2a9 , I will give you an answer for docker version >= 17.06.这个问题的用户使用的是Docker version 0.9.1, build 867b2a9 ,我会给你一个 docker version >= 17.06 的答案。

What you want, keep local directory synchronized within container directory, is accomplished by mounting the volume with type bind .你想要的,在容器目录中保持本地目录同步,是通过使用bind类型挂载卷来完成的。 This will bind the source (your system) and the target (at the docker container) directories.这将绑定源(您的系统)和目标(在 docker 容器中)目录。 It's almost the same as mounting a directory on linux.这几乎与在linux上挂载目录相同。

According to Docker documentation , the appropriate command to mount is now mount instead of -v . 根据 Docker 文档,挂载的适当命令现在是mount而不是-v Here's its documentation:这是它的文档:

  • --mount : Consists of multiple key-value pairs, separated by commas. --mount :由多个键值对组成,以逗号分隔。 Each key/value pair takes the form of a <key>=<value> tuple.每个键/值对都采用<key>=<value>元组的形式。 The --mount syntax is more verbose than -v or --volume , but the order of the keys is not significant, and the value of the flag is easier to understand. --mount语法比-v--volume更冗长,但键的顺序并不重要,标志的值更容易理解。

  • The type of the mount, which can be bind , volume , or tmpfs .挂载type ,可以是bindvolumetmpfs (We are going to use bind ) (我们将使用bind

  • The source of the mount.坐骑的source For bind mounts, this is the path to the file or directory on the Docker daemon host.对于绑定挂载,这是 Docker 守护程序主机上文件或目录的路径。 May be specified as source or src .可以指定为sourcesrc

  • The destination takes as its value the path where the file or directory will be mounted in the container. destination将文件或目录安装在容器中的路径作为其值。 May be specified as destination , dst , or target .可以指定为destinationdsttarget

So, to mount the the current directory (source) with /test_container (target) we are going to use:因此,要使用/test_container (目标)挂载当前目录(源),我们将使用:

    docker run -it --mount src="$(pwd)",target=/test_container,type=bind k3_s3

If these mount parameters have spaces you must put quotes around them.如果这些挂载参数有空格,您必须在它们周围加上引号。 When I know they don't, I would use `pwd` instead:当我知道他们不知道时,我会使用`pwd`代替:

    docker run -it --mount src=`pwd`,target=/test_container,type=bind k3_s3

You will also have to deal with file permission, see this article .您还必须处理文件权限,请参阅这篇文章

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 进行任何更改,它将对容器可见,反之亦然。

2 successive mounts: I guess many posts here might be using two boot2docker, the reason you don't see anything is that you are mounting a directory from boot2docker, not from your host. 2 次连续挂载:我猜这里的许多帖子可能使用了两个 boot2docker,您看不到任何内容的原因是您正在从 boot2docker 挂载一个目录,而不是从您的主机上挂载。

You basically need 2 successive mounts:你基本上需要2个连续的坐骑:

the first one to mount a directory from your host to your system第一个将目录从主机挂载到系统的

the second to mount the new directory from boot2docker to your container like this:第二个将新目录从 boot2docker 挂载到你的容器,如下所示:

  • 1) Mount local system on boot2docker 1)在boot2docker上挂载本地系统

    sudo mount -t vboxsf hostfolder /boot2dockerfolder
  • 2) Mount boot2docker file on linux container 2)在linux容器上挂载boot2docker文件

    docker run -v /boot2dockerfolder:/root/containerfolder -i -t imagename

Then when you ls inside the containerfolder you will see the content of your hostfolder .然后,当您lscontainerfolder时,您将看到hostfolder的内容。

Is it possible that you use docker on OS X via boot2docker or something similar.您是否可以通过boot2docker或类似的方式在 OS X 上使用 docker。

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虚拟机映像映射的,而不是您的 Mac。

You'll have to pipe the shared folder through your VM to your actual host (the Mac in my case).您必须通过 VM 将共享文件夹通过管道传输到您的实际主机(在我的情况下为 Mac)。

I'm just experimenting with getting my SailsJS app running inside a Docker container to keep my physical machine clean.我只是在尝试让我的 SailsJS 应用程序在 Docker 容器内运行,以保持我的物理机器清洁。

I'm using the following command to mount my SailsJS/NodeJS application under /app:我正在使用以下命令在 /app 下安装 SailsJS/NodeJS 应用程序:

cd my_source_code_folder
docker run -it -p 1337:1337 -v $(pwd):/app my_docker/image_with_nodejs_etc

[UPDATE] As of ~June 2017, Docker for Mac takes care of all the annoying parts of this where you have to mess with VirtualBox. [更新]截至 2017 年 6 月, Docker for Mac负责处理所有烦人的部分,您必须使用 VirtualBox。 It lets you map basically everything on your local host using the /private prefix.它允许您使用/private前缀基本上映射本地主机上的所有内容。 More info here .更多信息在这里 [/UPDATE] [/更新]

All the current answers talk about Boot2docker.当前所有答案都在谈论 Boot2docker。 Since that's now deprecated in favor of docker-machine, this works for docker-machine:由于现在不赞成使用 docker-machine,因此这适用于 docker-machine:

First, ssh into the docker-machine vm and create the folder we'll be mapping to:首先,通过 ssh 进入 docker-machine vm 并创建我们将映射到的文件夹:

docker-machine ssh $MACHINE_NAME "sudo mkdir -p \"$VOL_DIR\""

Now share the folder to VirtualBox:现在将文件夹共享到 VirtualBox:

WORKDIR=$(basename "$VOL_DIR")
vboxmanage sharedfolder add "$MACHINE_NAME" --name "$WORKDIR" --hostpath "$VOL_DIR" --transient

Finally, ssh into the docker-machine again and mount the folder we just shared:最后,再次 ssh 进入 docker-machine 并挂载我们刚刚共享的文件夹:

docker-machine ssh $MACHINE_NAME "sudo mount -t vboxsf -o uid=\"$U\",gid=\"$G\" \"$WORKDIR\" \"$VOL_DIR\""

Note: for UID and GID you can basically use whatever integers as long as they're not already taken.注意:对于 UID 和 GID,您基本上可以使用任何整数,只要它们还没有被占用。

This is tested as of docker-machine 0.4.1 and docker 1.8.3 on OS X El Capitan.这是从 OS X El Capitan 上的 docker-machine 0.4.1 和 docker 1.8.3 开始测试的。

docker run -v /host/directory:/container/directory -t IMAGE-NAME /bin/bash

docker run -v /root/shareData:/home/shareData -t kylemanna/openvpn /bin/bash

In my system I've corrected the answer from nhjk, it works flawless when you add the -t flag.在我的系统中,我已经更正了 nhjk 的答案,当您添加-t标志时它可以完美运行。

在 Mac OS 上,要将文件夹/Users/<name>/projects/挂载到您的 mac 上容器的根目录:

docker run -it -v /Users/<name>/projects/:/projects <container_name> bash

ls /projects

Using command-line :使用command-line

docker run -it --name <WHATEVER> -p <LOCAL_PORT>:<CONTAINER_PORT> -v <LOCAL_PATH>:<CONTAINER_PATH> -d <IMAGE>:<TAG>

Using docker-compose.yaml :使用docker-compose.yaml

version: '2'
  services:
    cms:
       image: <IMAGE>:<TAG>
       ports:
       - <LOCAL_PORT>:<CONTAINER_PORT>
       volumes:
       - <LOCAL_PATH>:<CONTAINER_PATH>

Assume :认为 :

  • IMAGE: k3_s3图片:k3_s3
  • TAG: latest标签: 最新
  • LOCAL_PORT: 8080本地端口:8080
  • CONTAINER_PORT: 8080容器端口:8080
  • LOCAL_PATH: /volume-to-mount LOCAL_PATH: /volume-to-mount
  • CONTAINER_PATH: /mnt CONTAINER_PATH: /mnt

Examples :例子 :

  1. First create /volume-to-mount.首先创建 /volume-to-mount。 (Skip if exist) (如果有则跳过)
$ mkdir -p /volume-to-mount
  1. docker-compose -f docker-compose.yaml up -d docker-compose -f docker-compose.yaml up -d
version: '2'
  services:
    cms:
       image: ghost-cms:latest
       ports:
       - 8080:8080
       volumes:
       - /volume-to-mount:/mnt
  1. Verify your container :验证您的容器:
docker exec -it CONTAINER_ID ls -la /mnt

对于那些想要在当前目录中挂载文件夹的人:

docker run -d --name some-container -v ${PWD}/folder:/var/folder ubuntu

If the host is windows 10 then instead of forward slash, use backward slash -如果主机是 Windows 10,则不要使用正斜杠,而是使用反斜杠 -

docker run -it -p 12001:80 -v c:\Users\C\Desktop\dockerStorage:/root/sketches

Make sure the host drive is shared (C in this case).确保主驱动器是共享的(在本例中为 C)。 In my case I got a prompt asking for share permission after running the command above.就我而言,在运行上面的命令后,我收到了询问共享权限的提示。

For Windows 10 users, it is important to have the mount point inside the C:/Users/ directory.对于Windows 10用户,将挂载点放在C:/Users/目录中很重要。 I tried for hours to get this to work.我尝试了几个小时来让它工作。 This post helped but it was not obvious at first as the solution for Windows 10 is a comment to an accepted answer.这篇文章有所帮助,但起初并不明显,因为 Windows 10 的解决方案是对已接受答案的评论。 This is how I did it:我是这样做的:

docker run -it -p 12001:80 -v //c/Users/C/Desktop/dockerStorage:/root/sketches \
<your-image-here> /bin/bash

Then to test it, you can do echo TEST > hostTest.txt inside your image.然后要对其进行测试,您可以在图像中执行echo TEST > hostTest.txt You should be able to see this new file in the local host folder at C:/Users/C/Desktop/dockerStorage/ .您应该能够在C:/Users/C/Desktop/dockerStorage/的本地主机文件夹中看到这个新文件。

Jul 2015 update - boot2docker now supports direct mounting. 2015 年 7 月更新 - boot2docker 现在支持直接挂载。 You can use -v /var/logs/on/host:/var/logs/in/container directly from your Mac prompt, without double mounting您可以直接在 Mac 提示符下使用-v /var/logs/on/host:/var/logs/in/container ,无需双重挂载

As of Docker 18-CE, you can use docker run -v /src/path:/container/path to do 2-way binding of a host folder.从 Docker 18-CE 开始,您可以使用docker run -v /src/path:/container/path对主机文件夹进行双向绑定。

There is a major catch here though if you're working with Windows 10/WSL and have Docker-CE for Windows as your host and then docker-ce client tools in WSL.但是,如果您使用的是 Windows 10/WSL,并且将 Docker-CE for Windows 作为您的主机,然后在 WSL 中使用 docker-ce 客户端工具,那么这里有一个主要问题。 WSL knows about the entire / filesystem while your Windows host only knows about your drives. WSL 知道整个 / 文件系统,而您的 Windows 主机只知道您的驱动器。 Inside WSL, you can use /mnt/c/projectpath, but if you try to docker run -v ${PWD}:/projectpath , you will find in the host that /projectpath/ is empty because on the host /mnt means nothing.在 WSL 中,您可以使用 /mnt/c/projectpath,但是如果您尝试 docker docker run -v ${PWD}:/projectpath ,您会在主机中发现 /projectpath/ 为空,因为在主机上 /mnt 没有任何意义.

If you work from /c/projectpath though and THEN do docker run -v ${PWD}:/projectpath and you WILL find that in the container, /projectpath will reflect /c/projectpath in realtime.如果您从 /c/projectpath 工作,然后执行 docker docker run -v ${PWD}:/projectpath ,您将在容器中找到它,/projectpath 将实时反映 /c/projectpath 。 There are no errors or any other ways to detect this issue other than seeing empty mounts inside your guest.除了在您的客人中看到空挂载之外,没有任何错误或任何其他方法可以检测此问题。

You must also be sure to "share the drive" in the Docker for Windows settings.您还必须确保在 Docker for Windows 设置中“共享驱动器”。

I've been having the same issue.我一直有同样的问题。 My command line looked like this:我的命令行如下所示:

docker run --rm -i --name $NAME -v `pwd`:/sources:z $NAME

The problem was with 'pwd'.问题出在“密码”上。 So I changed that to $(pwd):所以我把它改成了 $(pwd):

docker run --rm -i --name $NAME -v $(pwd):/sources:z $NAME

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.您下面的命令是正确的,除非您在使用 boot2docker 的 Mac 上(取决于将来的更新),在这种情况下您可能会发现文件夹为空。 See mattes answer for a tutorial on correcting this.有关更正此问题的教程,请参阅 mattes answer。

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 参数并引用您的主机,那么文件在您的主机上将是安全的。

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 中删除 vm 时,我的图像消失了,我发现了这一点。 (Note, i don't know how Virtualbox works, so the images might be still hidden somewhere else, just not visible to docker). (注意,我不知道 Virtualbox 是如何工作的,所以图像可能仍然隐藏在其他地方,只是对 docker 不可见)。

Had the same problem.有同样的问题。 Found this in the docker documentation :docker 文档中找到了这个:

Note: The host directory is, by its nature, host-dependent.注意:主机目录本质上是依赖于主机的。 For this reason, you can't mount a host directory from Dockerfile, the VOLUME instruction does not support passing a host-dir, because built images should be portable.出于这个原因,您不能从 Dockerfile 挂载主机目录,VOLUME 指令不支持传递主机目录,因为构建的图像应该是可移植的。 A host directory wouldn't be available on all potential hosts.主机目录不会在所有潜在主机上都可用。

So, mounting a read/write host directory is only possible with the -v parameter in the docker run command, as the other answers point out correctly.因此,只能使用docker run命令中的-v参数安装读/写主机目录,因为其他答案正确指出。

I found that any directory laying under system directive like /var , /usr , /etc could not be mount under the container.我发现任何位于系统指令(如/var/usr/etc )下的目录都无法挂载在容器下。

The directive should be at user's space -v switch instructs docker daemon to mount local directory to the container, for example:该指令应该在用户空间-v开关指示 docker daemon 将本地目录挂载到容器,例如:

docker run -t -d -v /{local}/{path}:/{container}/{path} --name {container_name} {imagename}

Here's an example with a Windows path:这是一个带有 Windows 路径的示例:

docker run -P -it --name organizr --mount src="/c/Users/MyUserName/AppData/Roaming/DockerConfigs/Organizr",dst=/config,type=bind organizrtools/organizr-v2:latest

As a side note, during all of this hair pulling, having to wrestle with figuring out, and retyping paths over and over and over again, I decided to whip up a small AutoHotkey script to convert a Windows path to a "Docker Windows " formatted path.作为旁注,在所有这些头发拉扯过程中,不得不努力弄清楚,一遍又一遍地重新输入路径,我决定编写一个小的AutoHotkey脚本来将 Windows 路径转换为“Docker Windows ”格式小路。 This way all I have to do is copy any Windows path that I want to use as a mount point to the clipboard, press the "Apps Key" on the keyboard, and it'll format it into a path format that Docker appreciates.这样,我所要做的就是将任何我想用作挂载点的 Windows 路径复制到剪贴板,按键盘上的“应用程序键”,它会将其格式化为 Docker 欣赏的路径格式。

For example:例如:

Copy this to your clipboard:将此复制到剪贴板:

C:\Users\My PC\AppData\Roaming\DockerConfigs\Organizr

press the Apps Key while the cursor is where you want it on the command-line, and it'll paste this there:当光标位于命令行上您想要的位置时按Apps Key ,它会将其粘贴到那里:

"/c/Users/My PC/AppData/Roaming/DockerConfigs/Organizr"

Saves a lot to time for me.为我节省了很多时间。 Here it is for anyone else who may find it useful.在这里,它适用于任何可能觉得它有用的人。

; --------------------------------------------------------------------------------------------------------------
;
; Docker Utility: Convert a Windows Formatted Path to a Docker Formatter Path
;                   Useful for (example) when mounting Windows volumes via the command-line.
;
; By:       J. Scott Elblein
; Version:  1.0
; Date:     2/5/2019
;
; Usage:    Cut or Copy the Windows formatted path to the clipboard, press the AppsKey on your keyboard
;           (usually right next to the Windows Key), it'll format it into a 'docker path' and enter it
;           into the active window. Easy example usage would be to copy your intended volume path via
;           Explorer, place the cursor after the "-v" in your Docker command, press the Apps Key and
;           then it'll place the formatted path onto the line for you.
;
; TODO::    I may or may not add anything to this depending on needs. Some ideas are:
;           
;           - Add a tray menu with the ability to do some things, like just replace the unformatted path
;               on the clipboard with the formatted one rather than enter it automatically.
;           - Add 'smarter' handling so the it first confirms that the clipboard text is even a path in
;               the first place. (would need to be able to handle Win + Mac + Linux)
;           - Add command-line handling so the script doesn't need to always be in the tray, you could
;               just pass the Windows path to the script, have it format it, then paste and close.
;               Also, could have it just check for a path on the clipboard upon script startup, if found
;               do it's job, then exit the script.
;           - Add an 'all-in-one' action, to copy the selected Windows path, and then output the result.
;           - Whatever else comes to mind.
;
; --------------------------------------------------------------------------------------------------------------

#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%

AppsKey::

    ; Create a new var, store the current clipboard contents (should be a Windows path)
    NewStr := Clipboard

    ; Rip out the first 2 chars (should be a drive letter and colon) & convert the letter to lowercase
    ; NOTE: I could probably replace the following 3 lines with a regexreplace, but atm I'm lazy and in a rush.
    tmpVar := SubStr(NewStr, 1, 2)
    StringLower, tmpVar, tmpVar

    ; Replace the uppercase drive letter and colon with the lowercase drive letter and colon
    NewStr := StrReplace(NewStr, SubStr(NewStr, 1, 2), tmpVar)

    ; Replace backslashes with forward slashes
    NewStr := StrReplace(NewStr,  "\", "/")

    ; Replace all colons with nothing
    NewStr := StrReplace(NewStr, ":", "")

    ; Remove the last char if it's a trailing forward slash
    NewStr :=  RegExReplace(NewStr, "/$")

    ; Append a leading forward slash if not already there
    if RegExMatch(NewStr, "^/") == 0
        NewStr :=  "/" . NewStr

    ; If there are any spaces in the path ... wrap in double quotes
    if RegExMatch(NewStr, " ") > 0
        NewStr :=  """" . NewStr . """"

    ; Send the result to the active window
    SendInput % NewStr 

To get this working in Windows 10 I had to open the Docker Settings window from the system tray and go to the Shared Drives section.为了让它在Windows 10中运行,我必须从系统托盘打开 Docker 设置窗口并转到共享驱动器部分。

I then checked the box next to C .然后我选中了C旁边的框。 Docker asked for my desktop credentials to gain authorisation to write to my Users folder. Docker 要求我提供桌面凭据以获得写入我的用户文件夹的授权。

Then I ran the docker container following examples above and also the example on that settings page, attaching to /data in the container.然后我按照上面的示例以及该设置页面上的示例运行 docker 容器,附加到容器中的/data

docker run -v c:/Users/<user.name>/Desktop/dockerStorage:/data -other -options

boot2docker together with VirtualBox Guest Additions boot2docker 和 VirtualBox 来宾添加
How to mount /Users into boot2docker如何将 /Users 挂载到 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。

Note that in Windows you'll have to provide the absolute path.请注意,在 Windows 中,您必须提供绝对路径。

Below worked for me.下面为我​​工作。

docker run -t -i -v D:/projects/:/home/chankeypathak/work -p 8888:8888 jupyter/tensorflow-notebook /bin/bash

i had same issues , i was trying to mount C:\Users\ folder on docker我有同样的问题,我试图在 docker 上挂载 C:\Users\ 文件夹
this is how i did it Docker Toolbox command line我就是这样做的 Docker Toolbox 命令行

 $ docker run -it --name <containername> -v /c/Users:/myVolData <imagename>

You can also do this with Portainer web application for a different visual experience.您也可以使用Portainer Web 应用程序执行此操作,以获得不同的视觉体验。

First pull the Portainer image:首先拉取 Portainer 镜像:

docker pull portainer/portainer

Then create a volume for Portainer:然后为 Portainer 创建一个卷:

docker volume create portainer_data

Also create a Portainer container:同时创建一个 Portainer 容器:

docker run -d -p 8000:8000 -p 9000:9000 --name=portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer

You will be able to access the web app with your browser at this URL: " http://localhost:9000 ".您将能够使用浏览器通过以下 URL 访问 Web 应用程序:“ http://localhost:9000 ”。 At the first login, you will be prompted to set your Portainer admin credentials.首次登录时,系统会提示您设置 Portainer 管理员凭据。

In the web app, follow these menus and buttons: (Container > Add container > Fill settings > Deploy Container)在 Web 应用程序中,按照以下菜单和按钮操作:(容器 > 添加容器 >填充设置> 部署容器)

I had trouble to create a "mount" volume with Portainer and I realized I had to click "bind" when creating my container's volume.我在使用 Portainer 创建“挂载”卷时遇到了麻烦,我意识到在创建容器卷时我必须单击“绑定”。 Below is an illustration of the volume binding settings that worked for my container creation with a mounted volume binded to the host.下面是卷绑定设置的图示,该设置适用于我的容器创建,其中已安装卷绑定到主机。

卷绑定设置

PS: I'm using Docker 19.035 and Portainer 1.23.1 PS:我使用的是 Docker 19.035 和 Portainer 1.23.1

I had the same requirement to mount host directory from container and I used volume mount command.我有从容器挂载主机目录的相同要求,我使用了卷挂载命令。 But during testing noticed that it's creating files inside container too but after some digging found that they are just symbolic links and actual file system used form host machine.但是在测试过程中注意到它也在容器内创建文件但是经过一些挖掘发现它们只是符号链接和实际使用的文件系统来自主机。

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

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