1. 程式人生 > >易學筆記-Docker-Compose相關

易學筆記-Docker-Compose相關

  1. 下載docker-compose:

    curl -L "https://github.com/docker/compose/releases/download/1.8.1/docker-compose-$(uname -s)-$(uname -m)" > /usr/local/bin/docker-compose  存放路徑:/usr/local/bin/docker-compose

  2. 檢視docker-compose版本

    [[email protected] ~]# docker-compose version

    docker-compose version 1.8.1, build 878cff1

    docker-py version: 1.10.3

    CPython version: 2.7.9

    OpenSSL version: OpenSSL 1.0.1e 11 Feb 2013

    [[email protected] ~]#

  3. 在根目錄下建立目錄:

    [[email protected] /]# mkdir compose

    [[email protected] compose]# pwd

    /compose

  4. 新建檔案:

    [[email protected] compose]# touch docker-compose.yml

  5. 往檔案docker-compose.yml新增內容:

    version: '2'

    services:

      App1:

        image: nginx

        ports:

          - "8080:80"

        networks:

          - "netName1"

        volumes:

          - /opt/conf/:/mnt

      App2:

        image: nginx

        ports:

          - "8081:80"

        networks:

          - "netName1"

        volumes:

          - /opt/conf/:/mnt

      App3:

        image: nginx

        ports:

          - "8082:80"

        networks:

          - "netName2"

    networks:

      netName1:

        driver: bridge

      netName2:

        driver: bridge

  6. 後臺服務啟動docker-compose.yml的服務:

    [[email protected] compose]# pwd

    /compose

    [[email protected] compose]# docker-compose up -d

    Starting compose_App3_1

    Starting compose_App2_1

    Starting compose_App1_1

    [[email protected] compose]#

  7. 檢視容器執行狀態:

    [[email protected] compose]# docker-compose ps

         Name              Command          State          Ports        

    --------------------------------------------------------------------

    compose_App1_1   nginx -g daemon off;   Up      0.0.0.0:8080->80/tcp

    compose_App2_1   nginx -g daemon off;   Up      0.0.0.0:8081->80/tcp

    compose_App3_1   nginx -g daemon off;   Up      0.0.0.0:8082->80/tcp

    [[email protected] compose]#

  8. docker-compose其它命令:

    [root@docker compose]# docker-compose restart #重啟所有容器

    [root@docker compose]# docker-compose restart App1  #重啟App1

    [root@docker compose]# docker-compose stop #停止所有容器

    [root@docker compose]# docker-compose stop App1  #停止App1

  9. 在APP1中ping APP2:

    [[email protected] compose]# docker-compose exec App1 bash

    [email protected]:/# ping App2

    PING App2 (172.18.0.3): 56 data bytes

    64 bytes from 172.18.0.3: icmp_seq=0 ttl=64 time=0.059 ms

    64 bytes from 172.18.0.3: icmp_seq=1 ttl=64 time=0.108 ms

    64 bytes from 172.18.0.3: icmp_seq=2 ttl=64 time=0.062 ms

    64 bytes from 172.18.0.3: icmp_seq=3 ttl=64 time=0.062 ms

  10. 檢視共享資料卷:

    /opt/conf下新建nginx.conf檔案

    [[email protected] conf]# pwd

    /opt/conf

    [[email protected] conf]# ls

    nginx.conf

    在掛載目錄/mnt下檢視nginx.conf檔案

    [[email protected] conf]#

    [[email protected] compose]# docker-compose exec App1 bash

    [email protected]:/# cd mnt

    [email protected]:/mnt# ls

    nginx.conf

    [email protected]:/mnt#

  11. 在compost目錄下(一定要在yml所在的資料夾才行)新增app/web目錄,並行新增檔案app3.html

    [[email protected] web]# pwd

    /compose/app/web

    [[email protected] web]# ls

    app3.html

    [[email protected] web]#

    app3.html檔案內容:

    <html>

        <head>

             <meta charset="utf-8">

             <title>Welcome to leonyan's home, this is app3</title>

         </head>

       <body>

       Welcome to leonyan's home, this is app3!

       </body>

    </html>

  12. 測試開啟app3.html

    [[email protected] web]# curl http://localhost:8082/app3.html

    <html>

        <head>

             <meta charset="utf-8">

             <title>Welcome to leonyan's home, this is app3</title>

         </head>

       <body>

       Welcome to leonyan's home, this is app3!

       </body>

    </html>

    [[email protected] web]#

  13. [[email protected] php_dockerfile]# docker run -d --name workpress -v /compose/app/web/:/var/www/html phpwithmysql2

    001faa364639c54f0ce268387d19166e13b9f7ee85cfa34d13e5e55301bae161

    [[email protected] php_dockerfile]# docker ps -a

    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES

    001faa364639        phpwithmysql2       "docker-php-entrypoin"   3 seconds ago       Up 2 seconds        80/tcp              workpress

    3b5c894283e3        mysql               "docker-entrypoint.sh"   About an hour ago   Up About an hour    3306/tcp            wordpressdb

  14. 埠對映展現網頁:
    1. 命令:

      docker run -d --name wordpress -p 192.168.65.130:45690:80 --link wordpressdb:mysql -v /compose/app/web/:/var/www/html phpwithmysql2

    2. 網頁展現:http://192.168.65.130:45690/