66
63

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 | docker run の -v オプションでホストの任意のパスをコンテナの任意のパスにマウントする

Posted at

docker run の -v オプションでホストの任意のパスをコンテナの任意のパスにマウントします。

サンプル

ホスト側の構成は以下

$ tree
.
└── sample_volume
    ├── hage.txt (中身は hage )
    ├── hige.txt (中身は hige )
    └── hoge.txt (中身は hoge )

ホストのsample_volumeをコンテナにマウントします

$ docker run -v /home/tbpgr/work/test_docker_volume/sample_volume:/opt/webapp/sample_volume -d -p 5000:5000 training/webapp python app.py

マウントされたVolumeを確認します

$ docker run -v /home/tbpgr/work/test_docker_volume/sample_volume:/opt/webapp/sample_volume -d -p 5000:5000 training/webapp python app.py
9a3a272800abb7466d9365fb39704138dc85e6393cec41efaf3d6fb0b618ba71
$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
9a3a272800ab        training/webapp     "python app.py"     4 seconds ago       Up 4 seconds        0.0.0.0:5000->5000/tcp   goofy_sammet
$ docker exec -it goofy_sammet /bin/bash
root@9a3a272800ab:/opt/webapp# ls
Procfile  app.py  requirements.txt  sample_volume  tests.py
root@9a3a272800ab:/opt/webapp# cd sample_volume/
root@9a3a272800ab:/opt/webapp/sample_volume# ll
total 20
drwxrwxr-x 2 1000 1000 4096 Sep 30 02:20 ./
drwxr-xr-x 3 root root 4096 Sep 30 02:35 ../
-rw-rw-r-- 1 1000 1000    5 Sep 30 02:20 hage.txt
-rw-rw-r-- 1 1000 1000    5 Sep 30 02:20 hige.txt
-rw-rw-r-- 1 1000 1000    5 Sep 30 02:20 hoge.txt
root@9a3a272800ab:/opt/webapp/sample_volume# cat h*ge.txt
hage
hige
hoge

編集

ホスト側でhoge.txtの中身をchangeに変更します。
コンテナ内のファイルも変更されているか確認します。

$ docker exec -it goofy_sammet /bin/bash
root@9a3a272800ab:/opt/webapp# cat sample_volume/hoge.txt 
change

関連資料

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?