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>
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐