9
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

docker runでhost osのディレクトリをマウントする際に注意すること

Last updated at Posted at 2016-11-15

久しぶりにDockerに触ってハマったこと。

以下のようにrunコマンドの-vオプションを渡すことで、コンテナとホストOS側でデータ共有できます。

$ docker run -v /host/dir:/container/dir ...

/host/dirの部分を絶対パスで指定する必要があります。

docker run -v ./host_dir:/container/dir ... のような指定をするとエラーとなります。

$ docker run -v ./host_dir:/container_dir
docker: Error response from daemon: create ./host_dir: "./host_dir" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed.
See 'docker run --help'.

また、 docker run -v host_dir:/container/dir のような指定をするとエラーにはなりませんが、container側にディレクトリが作成されるものの、ファイルシェアが行われません。

$ mkdir host_dir

$ echo hoge > host_dir/test

$ docker run -v host_dir:/container_dir xxxxx cat /container_dir/test
cat: /container_dir/test: No such file or directory

ということで冒頭書いたように、ホストOS側のディレクトリは絶対パスで指定しましょう。

$ mkdir host_dir

$ echo hoge > host_dir/test

$ docker run -v `pwd`/host_dir:/container_dir -t -i xxxxx cat /container_dir/test
hoge

9
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?