設置容器的端口映射html
經過run命令的兩個選項來實現這個功能nginx
run【-P】【-p】web
-P(大寫的P將爲容器暴露的全部端口進行映射)(隨機映射)docker
-P,--publish-all=true|false默認爲falseubuntu
dockerrun-P -i-tubuntu/bin/bashvim
-p(小寫的p可以指定映射容器哪些端口)bash
-p,--publish=[]curl
指定端口有四種狀況tcp
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地址和端口已經改變了(當咱們從新開啓時會發生改變)