在容器中部署靜態網站(三)

設置容器的端口映射

通過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:~#

可以用psport查看端口映射的情況

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地址和端口已經改變了(當我們重新開啓時會發生改變)


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章