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

来源:互联网 发布:陕西天互数据 编辑:程序博客网 时间:2024/06/10 14:40

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/bashhostPort:containPort    docker run -p 8080:80 -i -t ubuntu /bin/baship::containerPort    docker run -p 0.0.0.0:80 -i -t ubuntu /bin/baship: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/bashroot@3b1e8b137e4e:/# 

安装nginx

root@3b1e8b137e4e:/# apt-get install -y nginxReading package lists... DoneBuilding dependency tree       Reading state information... DoneE: Unable to locate package nginxroot@3b1e8b137e4e:/# apt-get updateroot@3b1e8b137e4e:/# apt-get install -y nginx

安装vim

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

创建静态页面

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

修改nginx配置文件

root@3b1e8b137e4e:/var/www/html# whereis nginxnginx: /usr/sbin/nginx /etc/nginx /usr/share/nginxroot@3b1e8b137e4e:/usr/share/nginx# cd /etc/nginxroot@3b1e8b137e4e:/etc/nginx# lsconf.d        fastcgi_params  koi-win     nginx.conf    scgi_params      sites-enabled  uwsgi_paramsfastcgi.conf  koi-utf         mime.types  proxy_params  sites-available  snippets       win-utfroot@3b1e8b137e4e:/etc/nginx# cd sites-enabled/root@3b1e8b137e4e:/etc/nginx/sites-enabled# lsdefaultroot@3b1e8b137e4e:/etc/nginx/sites-enabled# vim defaultroot@3b1e8b137e4e:/etc/nginx/sites-enabled# 

运行nginx

root@3b1e8b137e4e:/etc/nginx/sites-enabled# nginx root@3b1e8b137e4e:/etc/nginx/sites-enabled# ps -efUID         PID   PPID  C STIME TTY          TIME CMDroot          1      0  0 04:41 ?        00:00:00 /bin/bashroot        823      1  0 05:01 ?        00:00:00 nginx: master process nginxwww-data    824    823  0 05:01 ?        00:00:00 nginx: worker processwww-data    825    823  0 05:01 ?        00:00:00 nginx: worker processwww-data    826    823  0 05:01 ?        00:00:00 nginx: worker processwww-data    827    823  0 05:01 ?        00:00:00 nginx: worker processroot        828      1  0 05:01 ?        00:00:00 ps -efroot@3b1e8b137e4e:/etc/nginx/sites-enabled# 

验证网站访问

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                   NAMES3b1e8b137e4e        ubuntu              "/bin/bash"         21 minutes ago      Up 21 minutes       0.0.0.0:32768->80/tcp   webba87d27bc76b        ubuntu              "/bin/bash"         About an hour ago   Up About an hour                            romantic_franklineggyer@ubuntu:/usr/local$ eggyer@ubuntu:/usr/local$ docker port web80/tcp -> 0.0.0.0:32768eggyer@ubuntu:/usr/local$ docker port web80/tcp -> 0.0.0.0:32768eggyer@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>
0 0