Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Docker容器中装载主机目录_Docker_Mount_Boot2docker - Fatal编程技术网

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

如何在Docker容器中装载主机目录,docker,mount,boot2docker,Docker,Mount,Boot2docker,我正在尝试将主机目录装载到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$dock

我正在尝试将主机目录装载到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$docker run-d-v/Users/kishore/main_文件夹:/test_容器k3_s3:最新
c9f9a7e09c54ee1c2cc966f15c963b4af320b5203b8c46689033c1ab8872a0ea
root@0f17e2313a46:/test_容器#退出 退出

  • 我不知道如何检查boot2docker版本
问题、面临的问题:

  • 我需要如何将主\u文件夹链接到docker容器中的test\u容器文件夹
  • 我需要自动做这个。如何在不真正使用
    run-d-v
    命令的情况下执行该操作
  • 如果boot2docker崩溃怎么办?Docker文件存储在哪里(Dockerfile除外)

  • 有几种方法可以做到这一点。最简单的方法是使用dockerfile
    ADD
    命令,如下所示:

    docker run -d -v /Users/kishore/main_folder:/test_container k3_s3:latest
    
    但是,在生成dockerfile后对主机上的此目录所做的任何更改都不会显示在容器中。这是因为在构建容器时,docker将目录压缩到
    .tar
    中,并将该上下文永久上传到容器中

    第二种方法是您尝试的方法,即装入卷。由于尝试尽可能的可移植,您无法将主机目录映射到dockerfile中的docker容器目录,因为主机目录可能会根据您运行的机器而更改。要将主机目录映射到docker容器目录,您需要在使用
    docker run
    时使用
    -v
    标志,例如:

    sudo mount -t vboxsf hostfolder /boot2dockerfolder
    

    您是否可以通过
    boot2docker
    或类似的方式在OS X上使用docker

    我也有过同样的经历——命令是正确的,但无论如何,容器中没有安装任何(合理的)东西

    事实证明,这一点已经在本文中解释过了。当您键入
    docker run-v/var/logs/on/host:/var/logs/in/container…
    时,
    /var/logs/on/host
    实际上是从
    boot2docker
    虚拟机映像映射的,而不是从Mac映射的


    您必须通过虚拟机将共享文件夹传输到实际主机(在我的情况下是Mac)。

    boot2docker与VirtualBox来宾添加一起使用。
    如何将/用户装载到boot2docker中

    tl;dr使用VirtualBox来宾构建您自己的自定义boot2docker.iso 添加(请参阅)或下载 并将其保存到~/.boot2docker/boot2docker.iso


    您可以在cli中使用-v选项,此功能无法通过Dockerfile使用

    docker-run-t-i-v:ubuntu/bin/bash

    其中,host_dir是要装载的主机的目录。 若容器的目录不存在,你们不必担心,docker会创建它


    如果您从主机(在root权限下)对host_dir进行任何更改,它将对容器可见,反之亦然

    如何将主\u文件夹链接到docker容器中的test\u容器文件夹?

    下面的命令是正确的,除非您在mac上使用boot2docker(取决于将来的更新),在这种情况下,您可能会发现文件夹为空。有关更正此问题的教程,请参见mattes answer

    docker run -v /boot2dockerfolder:/root/containerfolder -i -t imagename
    
    我需要自动运行,如何在没有真正 使用run-d-v命令。

    您无法真正摆脱使用这些命令,它们是docker工作方式的固有特性。最好将它们放入shell脚本中,以避免重复编写它们

    如果boot2docker崩溃怎么办?docker文件存储在哪里?

    如果您设法使用-v参数并引用您的主机,那么这些文件在您的主机上是安全的

    如果您使用了带有docker文件的“docker build-t myimage”,那么您的文件将被烘焙到映像中

    我相信,您的docker映像存储在boot2docker vm中。当我从VirtualBox中删除虚拟机时,我发现我的图像消失了。(请注意,我不知道Virtualbox是如何工作的,因此图像可能仍然隐藏在其他地方,只是docker看不到这些图像)。

    2连续装载: 我猜这里的许多帖子可能使用了两个boot2docker,你看不到任何东西的原因是你是从boot2docker而不是从你的主机挂载了一个目录

    您基本上需要连续安装两次:

    docker run -it -p 12001:80 -v c:\Users\C\Desktop\dockerStorage:/root/sketches
    
    第一个将目录从主机装载到系统的

    第二种方法是将新目录从boot2docker装载到容器中,如下所示:

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

      docker-machine ssh $MACHINE_NAME "sudo mkdir -p \"$VOL_DIR\""
      
    • 2) 在linux容器上装载
      boot2docker
      文件

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

    然后,当您在
    containerfolder
    ls
    时,您将看到您的
    hostfolder

    2015年7月更新-boot2docker现在支持直接装载。从2017年6月起,您可以直接从Mac提示符下使用
    -v/var/logs/on/host:/var/logs/in/container,无需重复安装[UPDATE],处理好所有您不得不与VirtualBox发生冲突的烦人部分。它允许您使用
    /private
    前缀基本上映射本地主机上的所有内容。更多信息[/UPDATE]

    目前所有的答案都是关于Boot2docker的。由于现在docker machine不赞成使用该选项,因此docker machine也适用于该选项:

    首先,将ssh连接到docker机器vm中,并创建我们要映射到的文件夹:

    docker-machine ssh $MACHINE_NAME "sudo mount -t vboxsf -o uid=\"$U\",gid=\"$G\" \"$WORKDIR\" \"$VOL_DIR\""
    
    现在将文件夹共享到VirtualBox:

    cd my_source_code_folder docker run -it -p 1337:1337 -v $(pwd):/app my_docker/image_with_nodejs_etc 最后,再次将ssh插入docker机器,并装载我们刚才共享的文件夹:

    docker run -v /host/directory:/container/directory -t IMAGE-NAME /bin/bash
    
    docker run -v /root/shareData:/home/shareData -t kylemanna/openvpn /bin/bash
    
    注意:对于UID和GID,基本上可以使用任何整数,只要它们尚未被接受

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

    docker-machine ssh $MACHINE_NAME "sudo mount -t vboxsf -o uid=\"$U\",gid=\"$G\" \"$WORKDIR\" \"$VOL_DIR\""
    
    cd my_source_code_folder docker run -it -p 1337:1337 -v $(pwd):/app my_docker/image_with_nodejs_etc
    docker run -v /host/directory:/container/directory -t IMAGE-NAME /bin/bash
    
    docker run -v /root/shareData:/home/shareData -t kylemanna/openvpn /bin/bash
    
    docker run -t -d -v /{local}/{path}:/{container}/{path} --name {container_name} {imagename}
    
        docker run -it --mount src="$(pwd)",target=/test_container,type=bind k3_s3
    
        docker run -it --mount src=`pwd`,target=/test_container,type=bind k3_s3
    
    docker run -t -i -v D:/projects/:/home/chankeypathak/work -p 8888:8888 jupyter/tensorflow-notebook /bin/bash
    
     $ docker run -it --name <containername> -v /c/Users:/myVolData <imagename>
    
    docker run -it -p 12001:80 -v //c/Users/C/Desktop/dockerStorage:/root/sketches \
    <your-image-here> /bin/bash
    
    docker run --rm -i --name $NAME -v `pwd`:/sources:z $NAME
    
    docker run --rm -i --name $NAME -v $(pwd):/sources:z $NAME
    
    docker run -P -it --name organizr --mount src="/c/Users/MyUserName/AppData/Roaming/DockerConfigs/Organizr",dst=/config,type=bind organizrtools/organizr-v2:latest
    
    ; --------------------------------------------------------------------------------------------------------------
    ;
    ; 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 
    
    docker run -it -p 12001:80 -v c:\Users\C\Desktop\dockerStorage:/root/sketches
    
    docker run -v c:/Users/<user.name>/Desktop/dockerStorage:/data -other -options
    
    docker pull portainer/portainer
    
    docker volume create portainer_data
    
    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
    
    docker run -it --name <WHATEVER> -p <LOCAL_PORT>:<CONTAINER_PORT> -v <LOCAL_PATH>:<CONTAINER_PATH> -d <IMAGE>:<TAG>
    
    version: '2'
      services:
        cms:
           image: <IMAGE>:<TAG>
           ports:
           - <LOCAL_PORT>:<CONTAINER_PORT>
           volumes:
           - <LOCAL_PATH>:<CONTAINER_PATH>
    
    $ mkdir -p /volume-to-mount
    
    version: '2'
      services:
        cms:
           image: ghost-cms:latest
           ports:
           - 8080:8080
           volumes:
           - /volume-to-mount:/mnt
    
    docker exec -it CONTAINER_ID ls -la /mnt