利用Docker volume修改Nginx Docker镜像里index.html,dockernginx


通过这个小例子我们可以进一步加深对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首页:

本文来自linuxboy合作伙伴“汪子熙”,了解相关信息可以关注微信

相关内容