5.Ubuntu16.04Docker部署nginx实现静态网站

Ubuntu16.04Docker部署nginx实现静态网站

容器端口映射

如何访问容器的80端口?

run[-P][-p]

-P --publish-all=true|false 默认为false
使用大写P将对docker容器所有端口进行映射
    docker run -P -i -t ubuntu /bin/bash
-p ==publish=[]
containerPor 
    docker run -p 80 -i -t ubuntu /bin/bash

hostPort:containPort
    docker run -p 8080:80 -i -t ubuntu /bin/bash
ip::containerPort
    docker run -p 0.0.0.0:80 -i -t ubuntu /bin/bash
ip:hostPort:containerPort
    docker run -p 0.0.0.0:8080:80 -i -t ubuntu /bin/bash

nginx部署

创建映射80端口的交互式容器

eggyer@ubuntu:/usr/local$ docker run -p 80 --name web -i -t ubuntu /bin/bash
root@3b1e8b137e4e:/# 

安装nginx

root@3b1e8b137e4e:/# apt-get install -y nginx
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package nginx

root@3b1e8b137e4e:/# apt-get update

root@3b1e8b137e4e:/# apt-get install -y nginx

安装vim

root@3b1e8b137e4e:/# apt-get install -y vim

创建静态页面

root@3b1e8b137e4e:/# mkdir -p /var/www/html
root@3b1e8b137e4e:/# cd /var/www/html
root@3b1e8b137e4e:/var/www/html# vim index.html

修改nginx配置文件

root@3b1e8b137e4e:/var/www/html# whereis nginx
nginx: /usr/sbin/nginx /etc/nginx /usr/share/nginx

root@3b1e8b137e4e:/usr/share/nginx# cd /etc/nginx
root@3b1e8b137e4e:/etc/nginx# ls
conf.d        fastcgi_params  koi-win     nginx.conf    scgi_params      sites-enabled  uwsgi_params
fastcgi.conf  koi-utf         mime.types  proxy_params  sites-available  snippets       win-utf
root@3b1e8b137e4e:/etc/nginx# cd sites-enabled/
root@3b1e8b137e4e:/etc/nginx/sites-enabled# ls
default
root@3b1e8b137e4e:/etc/nginx/sites-enabled# vim default
root@3b1e8b137e4e:/etc/nginx/sites-enabled# 

运行nginx

root@3b1e8b137e4e:/etc/nginx/sites-enabled# nginx 
root@3b1e8b137e4e:/etc/nginx/sites-enabled# ps -ef
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 04:41 ?        00:00:00 /bin/bash
root        823      1  0 05:01 ?        00:00:00 nginx: master process nginx
www-data    824    823  0 05:01 ?        00:00:00 nginx: worker process
www-data    825    823  0 05:01 ?        00:00:00 nginx: worker process
www-data    826    823  0 05:01 ?        00:00:00 nginx: worker process
www-data    827    823  0 05:01 ?        00:00:00 nginx: worker process
root        828      1  0 05:01 ?        00:00:00 ps -ef
root@3b1e8b137e4e:/etc/nginx/sites-enabled# 

验证网站访问

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                   NAMES
3b1e8b137e4e        ubuntu              "/bin/bash"         21 minutes ago      Up 21 minutes       0.0.0.0:32768->80/tcp   web
ba87d27bc76b        ubuntu              "/bin/bash"         About an hour ago   Up About an hour                            romantic_franklin
eggyer@ubuntu:/usr/local$ 

eggyer@ubuntu:/usr/local$ docker port web
80/tcp -> 0.0.0.0:32768

eggyer@ubuntu:/usr/local$ docker port web
80/tcp -> 0.0.0.0:32768
eggyer@ubuntu:/usr/local$ curl http://127.0.0.1:32768
<html>
    <head>
        Nginx TO Docker
    </head>
    <body>
        <h1>hello,tangtang</h1>
    </body>
</html>


eggyer@ubuntu:/usr/local$ docker inspect web

        "IPAddress": "172.17.0.3",

eggyer@ubuntu:/usr/local$ curl http://172.17.0.3
<html>
    <head>
        Nginx TO Docker
    </head>
    <body>
        <h1>hello,tangtang</h1>
    </body>
</html>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值