791

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

Questions, issues facing:

  1. How do I need to link the main_folder to the test_container folder present inside the docker container?
  2. I need to make this automatically. How do I to do that without really using the run -d -v command?
  3. What happens if the boot2docker crashes? Where are the Docker files stored (apart from Dockerfile)?
4

26 に答える 26

627

これを行うにはいくつかの方法があります。これを行う最も簡単な方法は、次のように dockerfileADDコマンドを使用することです。

ADD . /path/inside/docker/container

ただし、dockerfile のビルド後にホスト上のこのディレクトリに加えられた変更は、コンテナーには反映されません。これは、コンテナーを作成するときに、docker がディレクトリを に圧縮し、そのコンテキストをコンテナーに永続的に.tarアップロードするためです。

これを行う 2 番目の方法は、ボリュームをマウントする方法です。ホスト ディレクトリは、実行しているマシンによって変わる可能性があるため、可能な限り移植性を高めるために、ホスト ディレクトリを dockerfile 内の docker コンテナー ディレクトリにマップすることはできません。ホスト ディレクトリを docker コンテナ ディレクトリにマップするには、次のようにフラグを使用する必要があります-vdocker run

# 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
于 2014-05-04T11:12:53.920 に答える
90

2 回の連続マウント: ここでの多くの投稿は 2 つの boot2docker を使用している可能性があると思います。何も表示されないのは、ホストからではなく、boot2docker からディレクトリをマウントしているためです。

基本的に、2 つの連続したマウントが必要です。

ホストからシステムにディレクトリをマウントする最初のもの

次のように、boot2docker からコンテナに新しいディレクトリをマウントします。

  • 1) ローカル システムをマウントするboot2docker

    sudo mount -t vboxsf hostfolder /boot2dockerfolder
    
  • boot2docker2) Linux コンテナーにファイルをマウントする

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

次に、lsの中に入るcontainerfolderと、 の内容が表示されますhostfolder

于 2014-12-05T16:45:21.417 に答える
30

物理マシンをクリーンに保つために、SailsJS アプリを Docker コンテナー内で実行することを試しているところです。

次のコマンドを使用して、SailsJS/NodeJS アプリケーションを /app の下にマウントしています。

cd my_source_code_folder
docker run -it -p 1337:1337 -v $(pwd):/app my_docker/image_with_nodejs_etc
于 2016-07-28T12:35:37.260 に答える
21

/Users/<name>/projects/Mac OS で、Macのコンテナーのルートにフォルダーをマウントするには、次のようにします。

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

ls /projects

于 2019-12-09T15:24:16.747 に答える
11

2015 年 7 月の更新 - boot2docker が直接マウントをサポートするようになりました。-v /var/logs/on/host:/var/logs/in/container二重にマウントすることなく、Mac プロンプトから直接使用できます

于 2015-07-11T19:11:49.720 に答える
11

私は同じ問題を抱えています。私のコマンドラインは次のようになりました:

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

問題は「pwd」にありました。だから私はそれを $(pwd) に変更しました:

docker run --rm -i --name $NAME -v $(pwd):/sources:z $NAME
于 2018-11-12T10:19:58.467 に答える
8

docker コンテナー内にある test_container フォルダーに main_folder をリンクするにはどうすればよいですか?

以下のコマンドは正しいですが、Mac で boot2docker を使用している場合 (将来の更新によって異なります) を除きます。その場合、フォルダーが空である可能性があります。これを修正するためのチュートリアルについては、マットの回答を参照してください。

docker run -d -v /Users/kishore/main_folder:/test_container k3_s3:latest

これを自動的に実行する必要があります。実際に run -d -v コマンドを使用せずに実行する方法です。

これらのコマンドを使用することから逃れることはできません。これらは docker の動作に固有のものです。何度も書き出す手間を省くために、それらをシェル スクリプトに入れることをお勧めします。

boot2docker がクラッシュするとどうなりますか? docker ファイルはどこに保存されますか?

-v 引数を使用してホスト マシンを参照することができれば、ファイルはホスト上で安全になります。

「docker build -t myimage」を使用した場合。Dockerfile を使用すると、ファイルがイメージに焼き付けられます。

Docker イメージは、boot2docker-vm に保存されていると思います。VirtualBoxからvmを削除すると画像が消えたときにこれを見つけました。(注、Virtualboxの仕組みがわからないため、画像はまだ別の場所に隠されている可能性があり、dockerには表示されません)。

于 2014-09-04T15:52:43.553 に答える
6

Windows パスの例を次に示します。

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

補足として、この髪の毛を引っ張ったり、見つけたり、パスを何度も何度も再入力したりするのに苦労している間に、Windows パスを"Docker Windows " 形式のパスAutoHotkeyに変換する小さなスクリプトを作成することにしました。 . このように、マウント ポイントとして使用したい Windows パスをクリップボードにコピーし、キーボードの "Apps Key" を押すだけで、Docker が認識できるパス形式にフォーマットされます。

例えば:

これをクリップボードにコピーします。

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

カーソルがコマンドライン上の目的の場所にある間に を押すと、Apps Keyそこにこれが貼り付けられます。

"/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 
于 2019-02-10T18:44:17.693 に答える
3

私は同じ問題を抱えていました.DockerにC:\Users\フォルダーをマウントしようとしていました
.

 $ docker run -it --name <containername> -v /c/Users:/myVolData <imagename>
于 2018-10-14T09:25:27.147 に答える
2

コンテナーからホスト ディレクトリをマウントするという同じ要件があり、ボリューム マウント コマンドを使用しました。しかし、テスト中にコンテナ内にもファイルが作成されていることに気付きましたが、掘り下げた後、それらは単なるシンボリックリンクであり、ホストマシンから使用される実際のファイルシステムであることがわかりました。

于 2018-07-16T13:49:37.027 に答える