How to mount a host directory in a Docker container

后端 未结 25 1577
庸人自扰
庸人自扰 2020-11-22 12:43

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.

Where am I doing somethin

25条回答
  •  青春惊慌失措
    2020-11-22 13:15

    Here's an example with a Windows path:

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

    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:

    "/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 
    

提交回复
热议问题