How to mount a host directory in a Docker container

后端 未结 25 1293
庸人自扰
庸人自扰 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:01

    Had the same problem. Found this in the docker documentation:

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

    0 讨论(0)
  • 2020-11-22 13:02

    How do I link the main_folder to the test_container folder present inside the docker 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. See mattes answer for a tutorial on correcting this.

    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.

    You can't really get away from using these commands, they are intrinsic to the way docker works. You would be best off putting them into a shell script to save you writing them out repeatedly.

    What happens if boot2docker crashes? Where are the docker files stored?

    If you manage to use the -v arg and reference your host machine then the files will be safe on your host.

    If you've used 'docker build -t myimage .' with a Dockerfile then your files will be baked into the image.

    Your docker images, i believe, are stored in the boot2docker-vm. I found this out when my images disappeared when i delete the vm from VirtualBox. (Note, i don't know how Virtualbox works, so the images might be still hidden somewhere else, just not visible to docker).

    0 讨论(0)
  • 2020-11-22 13:06

    Using command-line :

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

    Using docker-compose.yaml :

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

    Assume :

    • IMAGE: k3_s3
    • TAG: latest
    • LOCAL_PORT: 8080
    • CONTAINER_PORT: 8080
    • LOCAL_PATH: /volume-to-mount
    • CONTAINER_PATH: /mnt

    Examples :

    1. First create /volume-to-mount. (Skip if exist)
    $ mkdir -p /volume-to-mount
    
    1. 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
    
    0 讨论(0)
  • 2020-11-22 13:10

    Jul 2015 update - boot2docker now supports direct mounting. You can use -v /var/logs/on/host:/var/logs/in/container directly from your Mac prompt, without double mounting

    0 讨论(0)
  • 2020-11-22 13:14

    Note that in Windows you'll have to provide the absolute path.

    • Host: Windows 10
    • Container: Tensorflow Notebook

    Below worked for me.

    docker run -t -i -v D:/projects/:/home/chankeypathak/work -p 8888:8888 jupyter/tensorflow-notebook /bin/bash
    
    0 讨论(0)
  • 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 
    
    0 讨论(0)
提交回复
热议问题