易学笔记-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版本

    [root@localhost ~]# 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

    [root@localhost ~]#

  3. 在根目录下创建目录:

    [root@localhost /]# mkdir compose

    [root@localhost compose]# pwd

    /compose

  4. 新建文件:

    [root@localhost 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的服务:

    [root@localhost compose]# pwd

    /compose

    [root@localhost compose]# docker-compose up -d

    Starting compose_App3_1

    Starting compose_App2_1

    Starting compose_App1_1

    [root@localhost compose]#

  7. 查看容器运行状态:

    [root@localhost 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

    [root@localhost 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:

    [root@docker compose]# docker-compose exec App1 bash

    root@dd01fa7315ae:/# 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文件

    [root@localhost conf]# pwd

    /opt/conf

    [root@localhost conf]# ls

    nginx.conf

    在挂载目录/mnt下查看nginx.conf文件

    [root@localhost conf]#

    [root@localhost compose]# docker-compose exec App1 bash

    root@bb96cce0d662:/# cd mnt

    root@bb96cce0d662:/mnt# ls

    nginx.conf

    root@bb96cce0d662:/mnt#

  11. 在compost目录下(一定要在yml所在的文件夹才行)新增app/web目录,并行新增文件app3.html

    [root@localhost web]# pwd

    /compose/app/web

    [root@localhost web]# ls

    app3.html

    [root@localhost 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

    [root@localhost 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>

    [root@localhost web]#

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

    001faa364639c54f0ce268387d19166e13b9f7ee85cfa34d13e5e55301bae161

    [root@localhost 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/

猜你喜欢

转载自blog.csdn.net/u011830122/article/details/84060742