我正在尝试将主机目录装载到Docker容器中,以便在主机上完成的任何更新都反映到Docker集装箱中。

我哪里做错了什么。以下是我所做的:

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$树.├── Dockerfile文件└── 主文件夹├── tfile1.text├── tfile2.txt文件├── tfile3.txt文件└── tfile4.txt文件

1个目录,5个文件基肖尔$pwd/用户/kishore/tdockkishore$docker build--tag=k3_s3:最新。

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运行-d-v/Users/kishore/main_folder:/test_container k3_s3:最新c9f9a7e09c54ee1c2c966f15c963b4af320b5203b8c46689033c1ab8872a0漏洞$docker运行-i-t k3_s3:最新/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#退出exitkishore$docker-vDocker版本0.9.1,内部版本867b2a9

我不知道如何检查boot2docker版本

问题和面临的问题:

如何将main_folder链接到docker容器中的test_container文件夹?我需要自动完成。如何在不真正使用run-d-v命令的情况下做到这一点?如果boot2docker崩溃会发生什么?Docker文件存储在哪里(除了Dockerfile)?


当前回答

下面是一个Windows路径示例:

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

顺便说一句,在所有这些拉扯头发的过程中,我不得不一次又一次地计算和重新键入路径,我决定编写一个小的AutoHotkey脚本,将Windows路径转换为“Docker Windows”格式的路径。这样,我所要做的就是将任何我想用作装载点的Windows路径复制到剪贴板,按下键盘上的“应用程序键”,它就会将其格式化为Docker喜欢的路径格式。

例如:

将此复制到剪贴板:

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

当光标位于命令行上的所需位置时,按下Apps键,它会将其粘贴在那里:

“/c/Users/My PC/AppData/Roaming/DockerConfigs/Organizr”

为我节省了很多时间。这是给其他可能觉得有用的人的。

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

其他回答

对于Windows 10用户,将装载点放在C:\users/目录中很重要。我花了好几个小时才把它弄好。这篇文章有所帮助,但最初并不明显,因为Windows 10的解决方案是对公认答案的评论。我是这样做的:

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

然后要测试它,可以在图像中执行echo test>hostTest.txt。您应该能够在位于C:\Users/C/Desktop/dockerStorage/的本地主机文件夹中看到此新文件。

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

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

在我的系统中,我已经纠正了nhjk的答案,当你添加-t标志时,它就完美无瑕了。

从官方网站引用:

确保之前没有运行任何入门容器。从应用程序目录运行以下命令。

x86-64 Mac或Linux设备:码头运行-dp 3000:3000\-w/app-v“$(pwd):/app”\节点:12高山\sh-c“纱线安装和纱线运行开发”Windows(PowerShell):码头运行-dp 3000:3000`-w/app-v“$(pwd):/app”`节点:12高山`sh-c“纱线安装和纱线运行开发”Aple silicon Mac或其他ARM64设备:码头运行-dp 3000:3000\-w/app-v“$(pwd):/app”\节点:12高山\sh-c“apk add--无缓存python2g++make&&yarn install&&yarn run-dev”

解释:

dp 3000:3000-与之前相同。以分离(后台)模式运行并创建端口映射w/app-设置运行命令的“工作目录”或当前目录v“$(pwd):/app”-将当前目录从主机绑定到容器中的/app目录节点:12 alpine-要使用的图像。

注意,这是Dockerfile sh-c“yarn install&&yarn run dev”命令中应用程序的基本映像。

我们正在使用sh(alpine没有bash)启动一个shell,并运行yarn install来安装所有依赖项,然后运行yarn run dev。如果我们查看package.json,我们将看到dev脚本正在启动nodemon。

为了在Windows 10中实现这一点,我必须从系统托盘中打开Docker设置窗口,然后转到共享驱动器部分。

然后,我选中了C旁边的复选框。Docker要求我的桌面凭据以获得写入我的用户文件夹的授权。

然后,我按照上面的示例以及设置页面上的示例运行docker容器,将其附加到容器中的/data。

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

下面是一个Windows路径示例:

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

顺便说一句,在所有这些拉扯头发的过程中,我不得不一次又一次地计算和重新键入路径,我决定编写一个小的AutoHotkey脚本,将Windows路径转换为“Docker Windows”格式的路径。这样,我所要做的就是将任何我想用作装载点的Windows路径复制到剪贴板,按下键盘上的“应用程序键”,它就会将其格式化为Docker喜欢的路径格式。

例如:

将此复制到剪贴板:

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

当光标位于命令行上的所需位置时,按下Apps键,它会将其粘贴在那里:

“/c/Users/My PC/AppData/Roaming/DockerConfigs/Organizr”

为我节省了很多时间。这是给其他可能觉得有用的人的。

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