このドキュメントで説明されているソフトウェアはサポートされていないか、拡張サポートが提供されています。
現在サポートされているリリースにアップグレードすることをお薦めします。

4.8 データ・ボリューム・コンテナの作成と使用方法

1つのディレクトリ引数をdocker run-vオプションに指定すると、Dockerはコンテナ内にディレクトリを作成してdata volumeとしてマークし、これを他のコンテナがマウントできます。 DockerfileのVOLUME命令を使用して、このデータ・ボリュームをイメージに作成することも可能です。 このようなデータ・ボリュームが含まれるコンテナをデータ・ボリューム・コンテナと呼びます。 データ・ボリュームにファイルを移入後、docker run--volumes-fromオプションを使用すると、他のコンテナがこのボリュームをマウントしてそのデータにアクセスできるようになります。

注意

docker rmを使用してデータ・ボリュームに関連付けられているコンテナを削除する場合は、-vオプションを指定してこれらのボリュームを削除します。 関連付けが解除されたボリュームは、ディスク・スペースの無駄になり、削除が困難になります。

次の例では、HTTPサーバー・コンテナがWebコンテンツのソースとして使用可能なデータ・ボリューム・コンテナを作成します。

データ・ボリューム・コンテナのイメージを作成し、そのイメージからデータ・ボリューム・コンテナのインスタンスを作成する手順:

  1. 次のようにして、データ・ボリューム・コンテナ・イメージのDockerfileの生成先ディレクトリを作成します。

    # mkdir -p /var/docker_projects/mymod/dvc
  2. 新しいディレクトリ内に、データ・ボリューム・コンテナのイメージを定義するDockerfileを作成します。

    # Dockerfile that modifies oraclelinux:6 to create a data volume container
    FROM oraclelinux:6
    MAINTAINER A N Other <another@example.com>
    RUN mkdir -p /var/www/html
    RUN echo "This is the content for file1.html" > /var/www/html/file1.html
    RUN echo "This is the content for file2.html" > /var/www/html/file2.html
    RUN echo "This is the content for index.html" > /var/www/html/index.html
    VOLUME /var/www/html
    ENTRYPOINT /usr/bin/tail -f /dev/null

    RUN命令では、3つの簡易ファイルが格納された/var/www/htmlディレクトリを作成します。

    VOLUME命令では、docker run--volumes-fromオプションを使用して、他のコンテナがマウント可能なボリュームとしてディレクトリを利用できるようにします。

    ENTRYPOINT命令では、イメージから作成されたコンテナが常に実行するコマンドを指定します。 コンテナが終了しないように、ユーザーがdocker stop dvc1などのコマンドを使用してコンテナを停止するまで、/usr/bin/tail -f /dev/nullコマンドはブロックされます。

  3. docker buildコマンドを使用してイメージを作成します。

    [root@host ~]# docker build --tag="mymod/dvc:v1" \
        /var/docker_projects/mymod/dvc/
    Uploading context  2.56 kB
    Uploading context 
    Step 0 : FROM oraclelinux:6
     ---> 3e4b5e722ab9
    Step 1 : MAINTAINER A N Other <another@example.com>
     ---> Using cache
     ---> debe47cef9b8
    Step 2 : RUN mkdir -p /var/www/html
     ---> Running in fa94df7dd3af
     ---> 503132e87939
    Removing intermediate container fa94df7dd3af
    Step 3 : RUN echo "This is the content for file1.html" > /var/www/html/file1.html
     ---> Running in f98a14371672
     ---> e63ba0d36d88
    Removing intermediate container f98a14371672
    Step 4 : RUN echo "This is the content for file2.html" > /var/www/html/file2.html
     ---> Running in d0dca96ad53c
     ---> 27f2e2b3d207
    Removing intermediate container d0dca96ad53c
    Step 5 : RUN echo "This is the content for index.html" > /var/www/html/index.html
     ---> Running in fe39aa35b577
     ---> 89f3cb1db1c3
    Removing intermediate container fe39aa35b577
    Step 6 : VOLUME /var/www/html
     ---> Using cache
     ---> 91d394fd412e
    Step 7 : ENTRYPOINT /usr/bin/tail -f /dev/null
     ---> Running in 91b872b93b35
     ---> c6e914249bfd
    Removing intermediate container 91b872b93b35
    Successfully built 91d394fd412e
  4. データ・ボリューム・コンテナのインスタンス(例: dvc1)を作成します。

    [root@host ~]# docker run -d --name dvc1 mymod/dvc:v1 tail -f /dev/null
    1c8973e3c24e4f195e2b90ba5cb44af930121897c0e697407a8f83270589c6f1

他のコンテナがデータ・ボリューム(/var/www/html)をdvc1からマウントできることをテストするため、HTTPサーバーを実行してそのデータ・ボリュームをdvc1からマウントするwebsvrというコンテナを作成します。

[root@host ~]# docker run -d --volumes-from dvc1 --name websvr -P mymod/httpd:v2
008ce3de1cbf98ce50f6e3f3cf7618d248ce9dcfca8c29c1d04d179118d4c1b3

ホストで使用する適切なポートを確認後、curlを使用して、websvrがイメージに設定されている3つのファイルすべてのコンテンツを正常に提供していることをテストします。

[root@host ~]# docker port websvr 80
0.0.0.0:49154
[root@host ~]# curl http://localhost:49154
This is the content for index.html
[root@host ~]# curl http://localhost:49154/file1.html
This is the content for file1.html
[root@host ~]# curl http://localhost:49154/file2.html
This is the content for file2.html