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

4.7 Dockerコンテナからの外部ファイルへのアクセス

docker run-vオプションを使用すると、コンテナ内でファイルまたはファイル・システムを利用できるようになります。 次の例は、コンテナ内で実行中のHTTPサーバーが、ホスト上のWebページを利用できるようにする方法を示しています。

ホスト上でファイル/var/www/html/index.htmlを作成し、このファイルをマウントするHTTPサーバー・コンテナを実行します。

[root@host ~]# echo "This text was created in a file on the host" > /var/www/html/index.html
[root@host ~]# docker run -d --name newguest3 -P \
  -v /var/www/html/index.html:/var/www/html/index.html:ro mymod/httpd:v2
1197c308cdbae64daaa5422016108be76a085286281e5264e193f08a4cebea20

:ro修飾子は、コンテナがファイルまたはファイル・システムを読取り専用でマウントすることを指定します。 ファイルまたはファイル・システムを読取り/書込み可能でマウントするには、かわりに:rw修飾子を指定するか、修飾子全体を省略します。

HTTPサーバーがホスト上で実行されていないことを確認します。

[root@host ~]# curl http://localhost
curl: (7) couldn't connect to host
[root@host ~]# service httpd status
httpd is stopped

HTTPサーバーがホスト上で直接実行されていない場合でも、newguest3コンテナが提供している新しいWebページを表示できます。

[root@host ~]# docker inspect --format='{{ .NetworkSettings.Ports }}' newguest3
map[80/tcp:[map[HostIp:0.0.0.0 HostPort:49153]]]
[root@host ~]# curl http://localhost:49153
This text was created in a file on the host

ホスト上の/var/www/html/index.htmlファイルに加えた変更は、コンテナ内のマウント済ファイルに反映されます。

[root@host ~]# echo "Change the file on the host" > /var/www/html/index.html 
[root@host ~]# curl http://localhost:49153
Change the file on the host

ホスト上のファイルを削除しても、コンテナでは引き続き表示されます。

[root@host ~]# rm /var/www/html/index.html 
rm: remove regular file `/var/www/html/index.html'? y
[root@host ~]# ls -l /var/www/html/index.html
ls: cannot access /var/www/html/index.html: No such file or directory
[root@host ~]# curl http://localhost:49153
Change the file on the host

ホストからファイルまたはファイル・システムをマウントする方法を定義するためにDockerfileは使用できません。 Dockerアプリケーションは可搬性を目的としているので、元のホスト上に存在するファイルまたはファイル・システムを別のシステム上に用意することはありません。 外部ファイル・データに可搬性を持たせるには、そのデータをデータ・ボリューム・コンテナでカプセル化します。 4.8項「データ・ボリューム・コンテナの作成と使用方法」を参照してください。