通過這個小例子我們可以進一步加深對Docker volume概念的理解和使用方法。
我們都知道運行基於Docker的Nginx鏡像後,訪問localhost能看到Nginx默認的首頁,這個首頁的位置是Nginx鏡像內的/usr/share/nginx/html目錄下面。
假設我們有個需求,修改Nginx默認的首頁成下面的內容:
<html>
<head>
<title>Custom Website from my container</title>
</head>
<body>
<h1>This is Jerry's custom website.</h1>
<p>This website is served from my <a href="http://www.docker.com" target="_blank">SAP Docker</a> container.</p>
</body>
</html>
下面是詳細方法。
命令行-v將主機目錄nginx-html掛載到Nginx容器內的/usr/share/nginx/html目錄內。
docker run -d -p 1081:80 -v `pwd`/nginx-html:/usr/share/nginx/html --name jerry-custom nginx
使用vi將主機目錄nginx-html下面的index.html修改成自定義內容:
通過交互式的方式進入到docker容器內部:
docker exec -it jerry-custom /bin/sh
發現Docker容器裏的index.html也自動被修改了,內容和主機目錄nginx-html下面的一致。
localhost:1081即可看到修改過後的自定義Nginx首頁:
要獲取更多Jerry的原創文章,請關注公衆號"汪子熙":