蓝晶灵 发表于 2018-5-26 11:35:18

在Docker容器中部署静态网页的方法教程

  步骤:
1.创建映射端口的交互式容器
docker run -p 80 --name web -i -t daocloud.io/ubuntu /bin/bash
2.安装Nginx
apt-get install -y nginx   不行需要更新一下apt-get update
3.安装文本编辑器vim
apt-get install -y vim
4.创建静态页面

      mkdir -p /var/www/html
cd /var/www/html
vim index.html
  使用i切换到插入模式
  在index.html中写入以下内容:

<html>
<head>
<title>Nginx in Docker</title>
</head>
<body>Hello Docker</body>
</html>
  保存退出
  5.修改Nginx配置文件
vim /etc/nginx/sites-enabled/default
这样打开Nginx的配置文件之后会看见:

server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;   
root /var/www/html;
index index.html index.htm;
    # Make site accessible from http://localhost/
server_name localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
  这个时候修改root的内容,修改成咱们html文件所在的位置.保存退出.
  切换到根目录:
  cd /
  6.运行Nginx
   view plain copy
nginx
  可以使用ps -ef查看一下nginx是否运行了起来.
  使用Ctrl+p+q可以将容器放在后台运行.
  使用docker ps可以查看容器的运行效果.
  也可以使用docker port web查看容器的端口映射:
  80/tcp -> 0.0.0.0:32768
  7.验证网站访问
   view plain copy
curl http://127.0.0.1:32768
  也可以在浏览器中访问这个页面:
   view plain copy
http://127.0.0.1:32768
  使用docker inspect web查看容器的ip地址:
   view plain copy
&quot;NetworkSettings&quot;: {
&quot;Bridge&quot;: &quot;docker0&quot;,
&quot;Gateway&quot;: &quot;172.17.42.1&quot;,
&quot;GlobalIPv6Address&quot;: &quot;&quot;,
&quot;GlobalIPv6PrefixLen&quot;: 0,
&quot;IPAddress&quot;: &quot;172.17.0.1&quot;,
&quot;IPPrefixLen&quot;: 16,
&quot;IPv6Gateway&quot;: &quot;&quot;,
&quot;LinkLocalIPv6Address&quot;: &quot;fe80::42:acff:fe11:1&quot;,
&quot;LinkLocalIPv6PrefixLen&quot;: 64,
&quot;MacAddress&quot;: &quot;02:42:ac:11:00:01&quot;,
&quot;PortMapping&quot;: null,
&quot;Ports&quot;: {
&quot;80/tcp&quot;: [
{
&quot;HostIp&quot;: &quot;0.0.0.0&quot;,
&quot;HostPort&quot;: &quot;32768&quot;
}
]
}
},
  可以看到&quot;IPAddress&quot;: &quot;172.17.0.1&quot;,这是容器的IP地址.
  使用
   view plain copy
curl http://127.17.0.1
  就可以直接查看了.
  也可以在浏览器中使用这个容器的IP地址.
  最后需要说明一点,如果使用命令将容器停止:
   view plain copy
docker stop web
  然后开启容器:
   view plain copy
docker start -i web
  这个时候使用:
   view plain copy
ps -ef
  发现nginx是没有启动的.
  使用Curl+p+q将容器放入后台.
  使用:
   view plain copy
docker exec web nginx
  启动nginx服务.
  使用:
   view plain copy
curl http://172.17.0.1
  发现不行了.
  这个时候我们使用:
   view plain copy
docker inspect web
  看到以下输出结果:
   view plain copy
&quot;NetworkSettings&quot;: {
&quot;Bridge&quot;: &quot;docker0&quot;,
&quot;Gateway&quot;: &quot;172.17.42.1&quot;,
&quot;GlobalIPv6Address&quot;: &quot;&quot;,
&quot;GlobalIPv6PrefixLen&quot;: 0,
&quot;IPAddress&quot;: &quot;172.17.0.2&quot;,
&quot;IPPrefixLen&quot;: 16,
&quot;IPv6Gateway&quot;: &quot;&quot;,
&quot;LinkLocalIPv6Address&quot;: &quot;fe80::42:acff:fe11:2&quot;,
&quot;LinkLocalIPv6PrefixLen&quot;: 64,
&quot;MacAddress&quot;: &quot;02:42:ac:11:00:02&quot;,
&quot;PortMapping&quot;: null,
&quot;Ports&quot;: {
&quot;80/tcp&quot;: [
{
&quot;HostIp&quot;: &quot;0.0.0.0&quot;,
&quot;HostPort&quot;: &quot;32769&quot;
}
]
}
},
  发现容器的IP地和端口号都发生了变化,这个时候我们能知道,容器在重启之后的IP地址和端口号都会发生变化.
页: [1]
查看完整版本: 在Docker容器中部署静态网页的方法教程