站長(zhǎng)資訊網(wǎng)
        最全最豐富的資訊網(wǎng)站

        使用Docker創(chuàng)建Web服務(wù)詳解

        1、目的

        在已經(jīng)掌握docker安裝、docker倉(cāng)庫(kù)的基本使用、docker鏡像和容器的基本操作和相互轉(zhuǎn)化的基礎(chǔ)上,可嘗試通過(guò)docker搭建一個(gè)web服務(wù)器,便于分布式部署或快速移植web服務(wù)器。

        通過(guò)本文的學(xué)習(xí),可以了解學(xué)習(xí)docker容器與宿主機(jī)的文件和端口映射,進(jìn)一步熟練使用docker容器。

        2、修改容器,搭建簡(jiǎn)單的web服務(wù)

        安裝nginx

        # apt-get install nginx

        修改nginx配置文件

        # vi /etc/nginx/conf.d/web.conf

        # server的配置
        server {
            # 監(jiān)聽(tīng)端口
            listen 81;
            # 項(xiàng)目的初始化頁(yè)面
            location / {
              root  /home/visual/nginx_web/;
              index index.html;
            }
        }

        修改開(kāi)機(jī)啟動(dòng)項(xiàng)

        # vi /etc/rc.local

        ####!/bin/sh -e
        #
        # rc.local
        #
        # This script is executed at the end of each multiuser runlevel.
        # Make sure that the script will “exit 0” on success or any other
        # value on error.
        #
        # In order to enable or disable this script just change the execution
        # bits.
        #
        # By default this script does nothing.

        service ssh start
        service nginx start

        /bin/bash

        exit 0

        3、創(chuàng)建鏡像,便于再次啟動(dòng)容器

        通過(guò)commit操作創(chuàng)建docker鏡像文件,上篇文章已經(jīng)講過(guò),命令如下

        linuxidc@linuxidc:~/docker$ docker ps -a
        CONTAINER ID        IMAGE              COMMAND                  CREATED            STATUS                      PORTS              NAMES
        568e5204fff3        Ubuntu              “/bin/sh -c ‘while…”  40 hours ago        Exited (137) 38 hours ago                      kind_khorana
        00f561d97811        ubuntu              “/bin/echo hello w…”  40 hours ago        Exited (0) 40 hours ago                        nifty_mcnulty
        93a1b9d39683        ubuntu              “bash”                  40 hours ago        Exited (0) 5 seconds ago                        zealous_noether
        abdc084f9821        hello-world        “/hello”                41 hours ago        Exited (0) 18 hours ago                        sleepy_clarke
        linuxidc@linuxidc:~/docker$ docker commit 93a1b9d39683 learn/nginx:v2
        sha256:ab92edd21696770f1eb37e9229b6834094a8d3381e5b4e9edc620b7927004bb4
        linuxidc@linuxidc:~/docker$ docker images
        REPOSITORY                                      TAG                IMAGE ID            CREATED            SIZE
        learn/nginx                                      v2                  ab92edd21696        5 seconds ago      370MB
        learn/visual_init                                v1                  56a4eab7dc5b        37 hours ago        321MB
        registry.cn-beijing.aliyuncs.com/zhangsp/ai      visual_init        56a4eab7dc5b        37 hours ago        321MB
        ubuntu                                          latest              14f60031763d        5 days ago          120MB
        hello-world                                      latest              1815c82652c0        5 weeks ago        1.84kB

        4、啟動(dòng)新容器

        使用新創(chuàng)建的鏡像learn/nginx:v2,啟動(dòng)新容器

        # docker run -it –name nginx_test -h docker-nginx -p 8001:81 -p 8000:80 -v /home/linuxidc/docker/nginx_web/:/home/visual/nginx_web/ learn/nginx:v2 /bin/sh /etc/rc.local

        啟動(dòng)容器的參數(shù)介紹

            -it,交互方式啟動(dòng)
            –name nginx_test,指定新容器的名稱是nginx_test
            -h docker-nginx,指定新容器的主機(jī)名是docker-nginx
            -p 8001:81 -p 8000:80,指定宿主機(jī)與docker容器的端口映射,宿主機(jī)的8001對(duì)應(yīng)docker容器的81,宿主機(jī)的8000對(duì)應(yīng)docker容器的80
            -v /home/linuxidc/docker/nginx_web/:/home/visual/nginx_web/,指定宿主機(jī)與docker容器的文件映射,宿主機(jī)的/home/linuxidc/docker/nginx_web/ 對(duì)應(yīng)docker容器的 /home/visual/nginx_web/
            learn/nginx:v2,指定啟動(dòng)容器對(duì)應(yīng)的鏡像是learn/nginx:v2,可以是鏡像ID
            /bin/sh /etc/rc.local,指定容器啟動(dòng)后,執(zhí)行shell腳本是/etc/rc.local

        查看docker容器,容器nginx_test處于up狀態(tài),說(shuō)明啟動(dòng)正常

        linuxidc@linuxidc:~/docker/nginx_web$ docker ps -a
        CONTAINER ID        IMAGE              COMMAND                  CREATED            STATUS                        PORTS                                        NAMES
        cbbbe7a5d47a        learn/nginx:v2      “/bin/sh /etc/rc.l…”  25 minutes ago      Up 24 minutes                  0.0.0.0:8000->80/tcp, 0.0.0.0:8001->81/tcp  nginx_test
        966bd52b72da        ubuntu              “/bin/sh -c ‘while…”  42 hours ago        Exited (137) 40 hours ago                                                  stupefied_knuth
        00f561d97811        ubuntu              “/bin/echo hello w…”  42 hours ago        Exited (0) 42 hours ago                                                    nifty_mcnulty
        93a1b9d39683        ubuntu              “bash”                  43 hours ago        Exited (0) About an hour ago                                                zealous_noether
        abdc084f9821        hello-world        “/hello”                43 hours ago        Exited (0) 20 hours ago                                                    sleepy_clarke

        5、測(cè)試docker_nginx是否正常

        通過(guò)瀏覽器測(cè)試8001端口

        使用Docker創(chuàng)建Web服務(wù)詳解

        通過(guò)瀏覽器測(cè)試8000端口

        使用Docker創(chuàng)建Web服務(wù)詳解

        修改宿主機(jī)的文件,并測(cè)試8001端口

        修改宿主機(jī)的/home/linuxidc/docker/nginx_web/index.html文件

        linuxidc@linuxidc:~/docker/nginx_web$ vi index.html
        <!DOCTYPE html>
        <html>
        <head>
        <title>Welcome to nginx! I am in docker!</title>
        <style>
            body {
                width: 35em;
                margin: 0 auto;
                font-family: Tahoma, Verdana, Arial, sans-serif;
            }
        </style>
        </head>
        <body>
        <h1>Welcome to nginx! I am in Docker!</h1>
        <p>If you see this page, the nginx web server is successfully installed and
        working. Further configuration is required.</p>

        <p>For online documentation and support please refer to
        <a href=”http://nginx.org/”>nginx.org</a>.<br/>
        Commercial support is available at
        <a href=”http://nginx.com/”>nginx.com</a>.</p>

        <p><em>Thank you for using nginx.</em></p>
        </body>
        </html>

        通過(guò)瀏覽器測(cè)試8001端口,發(fā)現(xiàn)“Welcome to nginx! I am in docker!”,說(shuō)明內(nèi)容已經(jīng)修改,使用docker做為web服務(wù)器的功能已經(jīng)OK

        使用Docker創(chuàng)建Web服務(wù)詳解

         

        更多Docker相關(guān)教程見(jiàn)以下內(nèi)容: 

        Linux 下的 Docker 安裝與使用  http://m.0106606.com/Linux/2018-06/152996.htm
        CentOS 7安裝Docker應(yīng)用容器引擎 http://m.0106606.com/Linux/2018-06/152856.htm
        CentOS 7.3環(huán)境安裝Docker 18.03 http://m.0106606.com/Linux/2018-05/152356.htm
        使用Docker分分鐘啟動(dòng)常用應(yīng)用  http://m.0106606.com/Linux/2017-04/142649.htm
        CentOS 7使用Docker搭建GitLab服務(wù)器  http://m.0106606.com/Linux/2018-04/151725.htm
        30分鐘帶你了解Docker  http://m.0106606.com/Linux/2018-08/153346.htm

        Docker容器常見(jiàn)操作詳解  http://m.0106606.com/Linux/2018-08/153685.htm

        Docker發(fā)布應(yīng)用程序指南 http://m.0106606.com/Linux/2018-08/153405.htm

        贊(0)
        分享到: 更多 (0)
        網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)
        主站蜘蛛池模板: 欧美国产日韩精品| 久久国产免费观看精品3| 漂亮人妻被黑人久久精品| 亚洲精品动漫人成3d在线| 亚洲精品永久在线观看| 久久这里只有精品18| 久久精品国产亚洲Aⅴ蜜臀色欲| 久久国产热这里只有精品| 人妻VA精品VA欧美VA| 精品少妇人妻av无码久久| 99在线精品视频在线观看| 久久夜色精品国产亚洲| 无码国产精品一区二区免费vr | 日韩av无码久久精品免费| 国产啪亚洲国产精品无码| 日韩精品无码Av一区二区| 亚洲av无码乱码国产精品fc2| 精品国产欧美一区二区三区成人| 亚洲?V无码成人精品区日韩| 777国产盗摄偷窥精品0OOO| 久久久WWW成人免费精品| 亚洲精品福利视频| 亚洲精品人成无码中文毛片| 国产精品亚洲w码日韩中文| 亚洲国产精品免费视频| 99精品视频3| 久久噜噜久久久精品66| 99热这里只有精品国产66| 一本色道久久综合亚洲精品| 久久精品国产半推半就| heyzo高无码国产精品| 国产午夜精品免费一区二区三区| 亚洲国产精品乱码一区二区| 国产成人精品日本亚洲网址| 精品乱码一区二区三区四区| 日韩精品极品视频在线观看免费 | 在线成人精品国产区免费| 欧洲精品一区二区三区在线观看| 久久久久人妻一区精品| 日韩精品成人a在线观看| 亚洲国产小视频精品久久久三级 |