首页 > 代码库 > 在容器中部署静态网站(三)
在容器中部署静态网站(三)
设置容器的端口映射
通过run命令的两个选项来实现这个功能
run【-P】【-p】
-P(大写的P将为容器暴露的所有端口进行映射)(随机映射)
-P,--publish-all=true|false默认为false
dockerrun-P -i-tubuntu/bin/bash
-p(小写的p能够指定映射容器哪些端口)
-p,--publish=[]
指定端口有四种情况
containerPort
docker run -p 80 -I -tubuntu /bin/bash
hostPort:containerPort
docker run -p8080:80 -I -t ubuntu /bin/bash
ip::containerPort
docker run -p0.0.0.0:80 -I -t ubuntu /bin/bash
ip:hostPort:containerPort
docker run -p0.0.0.0:8080:80 -I -t ubuntu /bin/bash
Nginx部署流程:
创建映射80端口的交互式容器
安装Nginx
安装文本编辑器vim
创建静态页面
修改Nginx配置文件
运行Nginx
验证网站访问
ubuntu@ubuntu-virtual-machine:~$docker run -p 80 --name web -i -t ubuntu /bin/bash
root@0cc8c10d217a:/# apt-get intsall -y nginx#安装nginx
root@0cc8c10d217a:/var/www/html#apt-get install -y vim#安装vim
root@0cc8c10d217a:/#mkdir -p /var/www/html
root@0cc8c10d217a:/# cd/var/www/html/
root@0cc8c10d217a:/var/www/html#
root@0cc8c10d217a:/var/www/html#cat index.html #编辑一个静态页面
<html>
<head>
<title>Nginx inDocker</title>
</head>
<body>
<h1>Hello,I‘m website in Docker!</h1>
</body>
</html>
查看下nginx安装在哪里
root@0cc8c10d217a:/var/www/html#whereis nginx
nginx: /usr/sbin/nginx/etc/nginx /usr/share/nginx /usr/share/man/man1/nginx.1.gz
vim /etc/nginx/sites-enabled/default#打开default文件
将root的值改为我们刚刚建立的静态网站的位置
root@0cc8c10d217a:/var/www/html#cat /etc/nginx/sites-enabled/default|grep root
root /var/www/html;
# root /usr/share/nginx/html;
# deny access to .htaccess files, ifApache‘s document root
# root html;
# root html;
root@0cc8c10d217a:/var/www/html#cd /
root@0cc8c10d217a:/#
root@0cc8c10d217a:~#ps -ef|grep nginx#查看进程
root 27163 13952 0 Apr22 ? 00:00:00 nginx: master process/usr/sbin/nginx
www-data 2716427163 0 Apr22 ? 00:00:05 nginx: worker process
www-data 2716527163 0 Apr22 ? 00:00:00 nginx: worker process
www-data 2716627163 0 Apr22 ? 00:00:05 nginx: worker process
www-data 2716727163 0 Apr22 ? 00:00:04 nginx: worker process
root 31545 31522 0 04:44 pts/0 00:00:00 grep --color=auto nginx
root@0cc8c10d217a:~#
可以用ps和port查看端口映射的情况
ubuntu@ubuntu-virtual-machine:~$docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0cc8c10d217a ubuntu:latest "/bin/bash" 2 days ago Up 2 days 0.0.0.0:32769->80/tcp web
ubuntu@ubuntu-virtual-machine:~$docker port web
80/tcp ->0.0.0.0:32769
ubuntu@ubuntu-virtual-machine:~$ docker top web#可以查看容器中进程运行的情况
UID PID PPID C STIME TTY TIME CMD
root 13952 7138 0 Apr22 pts/1 00:00:00 /bin/bash
ubuntu@ubuntu-virtual-machine:~$ curl http://127.0.0.1:32769#访问
<!DOCTYPE html>
<html>
<head>
<title>Welcome tonginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial,sans-serif;
}
</style>
</head>
<body>
<h1>Welcome tonginx!</h1>
<p>If you seethis page, the nginx web server is successfully installed and
working. Furtherconfiguration is required.</p>
<p>For onlinedocumentation and support please refer to
<ahref="http://nginx.org/">nginx.org</a>.<br/>
Commercial support isavailable at
<ahref="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thankyou for using nginx.</em></p>
</body>
</html>
也可以用容器的ip地址来访问
ubuntu@ubuntu-virtual-machine:~$docker inspect web|grep IPAddress
"IPAddress":"172.17.0.7",
ubuntu@ubuntu-virtual-machine:~$
ubuntu@ubuntu-virtual-machine:~$curl http://172.17.0.7
<!DOCTYPE html>
<html>
<head>
<title>Welcome tonginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial,sans-serif;
}
</style>
</head>
<body>
<h1>Welcome tonginx!</h1>
<p>If you seethis page, the nginx web server is successfully installed and
working. Furtherconfiguration is required.</p>
<p>For onlinedocumentation and support please refer to
<ahref="http://nginx.org/">nginx.org</a>.<br/>
Commercial support isavailable at
<ahref="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thankyou for using nginx.</em></p>
</body>
</html>
ubuntu@ubuntu-virtual-machine:~$ docker stop web#停掉容器
web
ubuntu@ubuntu-virtual-machine:~$
ubuntu@ubuntu-virtual-machine:~$ docker start -i web#启动容器
root@0cc8c10d217a:/# ps -ef#重新启动后并没有开启nginx
UID PID PPID C STIME TTY TIME CMD
root 1 0 16 17:17 ? 00:00:02/bin/bash
root 11 1 26 17:17 ? 00:00:00 ps-ef
先ctrl+pctrl+q推出,这个时候就可以用exec命令来启动nginx
ubuntu@ubuntu-virtual-machine:~$ docker exec web nginx#再次启动nginx
ubuntu@ubuntu-virtual-machine:~$docker top web
UID PID PPID C STIME TTY TIME CMD
root 32349 7138 1 01:17 pts/0 00:00:02 /bin/bash
root 32396 32349 0 01:19 ? 00:00:00 nginx: master process nginx
www-data 32397 32396 0 01:19 ? 00:00:00 nginx: worker process
这时候再用原来的IP地址来访问,就访问失败了
ubuntu@ubuntu-virtual-machine:~$curl http://172.17.0.7
curl: (7) Failed toconnect to 172.17.0.7 port 80: No route to host
ubuntu@ubuntu-virtual-machine:~$docker inspect web|grep -E "IPAddress|HostPort"
"HostPort":""
"IPAddress":"172.17.0.8",
"HostPort":"32770"
#发现容器的IP地址和端口已经改变了(当我们重新开启时会发生改变)
本文出自 “11364450” 博客,请务必保留此出处http://11374450.blog.51cto.com/11364450/1918703
在容器中部署静态网站(三)