• docker服务编排--docker-compose


    根据哔哩哔哩视频【docker入门2】实战~如何组织一个多容器项目docker-compose进行学习

    利用网站https://labs.play-with-docker.com/进行测试

    第一步,安装nginx镜像

    # 拉取nginx镜像
    docker pull nginx
    # 运行nginx容器
    docker run -d -p80:80 --name mynginx nginx 
    #容器ID
    87702c4c192061036d731df8f533a82f20b972284b650a850adec3aba9580342
    # 查看运行中的容器
    docker ps
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
    87702c4c1920        nginx               "nginx -g 'daemon of…"   24 seconds ago      Up 23 seconds       0.0.0.0:80->80/tcp   mynginx 
    # 在容器中进行交互操作
    docker exec -it mynginx /bin/bash
    # 查看容器IP地址
    cat /etc/hosts
    
    127.0.0.1	localhost
    ::1	localhost ip6-localhost ip6-loopback
    fe00::0	ip6-localnet
    ff00::0	ip6-mcastprefix
    ff02::1	ip6-allnodes
    ff02::2	ip6-allrouters
    172.17.0.2	87702c4c1920
    

    第二步:安装最小化Linux镜像alpine

    # 拉取alpine镜像
    docker pull alpine
    # 安装curl
    apk add curl
    # curl nginx容器
    curl 172.17.0.2
    
    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    <style>
        body {
             35em;
            margin: 0 auto;
            font-family: Tahoma, Verdana, Arial, sans-serif;
        }
    </style>
    </head>
    <body>
    <h1>Welcome to nginx!</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>
    

    另外一种方式 --link方式

    #后台运行alpine,并将mynginx容器绑定域名myng
    docker run -dit --link mynginx:myng alpine   
    825d4b3d221238bda2f29411fcae6d72fafb97d5002986660aa8c3b7cc501d99
    # 安装curl
    apk add curl
    
    fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/main/x86_64/APKINDEX.tar.gz
    fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/community/x86_64/APKINDEX.tar.gz
    (1/4) Installing ca-certificates (20191127-r1)
    (2/4) Installing nghttp2-libs (1.40.0-r0)
    (3/4) Installing libcurl (7.67.0-r0)
    (4/4) Installing curl (7.67.0-r0)
    Executing busybox-1.31.1-r9.trigger
    Executing ca-certificates-20191127-r1.trigger
    OK: 7 MiB in 18 packages
    # curl nginx镜像
    curl myng
    
    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    <style>
        body {
             35em;
            margin: 0 auto;
            font-family: Tahoma, Verdana, Arial, sans-serif;
        }
    </style>
    </head>
    <body>
    <h1>Welcome to nginx!</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>
    
    # 原理,增加了172.17.0.2      myng d2d78de2ad2f
    cat /etc/hosts
    127.0.0.1       localhost
    ::1     localhost ip6-localhost ip6-loopback
    fe00::0 ip6-localnet
    ff00::0 ip6-mcastprefix
    ff02::1 ip6-allnodes
    ff02::2 ip6-allrouters
    172.17.0.2      myng d2d78de2ad2f
    172.17.0.3      9688a42fec94
    

    docker-compose方式

    /Users/doubledumbao/docker-demo/compose-demo目录下,新建html目录,添加index.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>hello world nginx</title>
    </head>
    <body>
        <h1>hello world nginx!!!</h1>
    </body>
    </html>
    

    /Users/doubledumbao/docker-demo/compose-demo/html目录下,添加test.php

    <!DOCTYPE html>
    <html>
    <body>
    
    <?php
    echo "Hello World PHP!!!";
    ?>
    
    </body>
    </html>
    

    /Users/doubledumbao/docker-demo/compose-demo/html目录下,添加mysql.php

    <?php
    $servername = "mysql";
    $username = "root";
    $password = "123456";
     
    // 创建连接
    $conn = new mysqli($servername, $username, $password);
     
    // 检测连接
    if ($conn->connect_error) {
        die("连接失败: " . $conn->connect_error);
    } 
    echo "connected success !!!!!";
    ?>
    

    /Users/doubledumbao/docker-demo/compose-demo目录下,新建conf目录

    #user  nobody;
    worker_processes  1;
    
    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    
    #pid        logs/nginx.pid;
    
    
    events {
        worker_connections  1024;
    }
    
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
        #                  '$status $body_bytes_sent "$http_referer" '
        #                  '"$http_user_agent" "$http_x_forwarded_for"';
    
        #access_log  logs/access.log  main;
    
        sendfile        on;
        #tcp_nopush     on;
    
        #keepalive_timeout  0;
        keepalive_timeout  65;
    
        #gzip  on;
    
        server {
            listen       80;
            server_name  localhost;
    
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
    
            location / {
                root   /usr/share/nginx/html;
                index  index.html index.htm;
            }
    
            #error_page  404              /404.html;
    
            # redirect server error pages to the static page /50x.html
            #
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
    
            # proxy the PHP scripts to Apache listening on 127.0.0.1:80
            #
            # location ~ .php$ {
            #    proxy_pass   http://127.0.0.1;
            # }
    
            # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
            #
            location ~ .php$ {
               fastcgi_pass   php:9000;
               fastcgi_index  index.php;
               fastcgi_param  SCRIPT_FILENAME  /var/www/html/$fastcgi_script_name;
               include        fastcgi_params;
            }
    
            # deny access to .htaccess files, if Apache's document root
            # concurs with nginx's one
            #
            #location ~ /.ht {
            #    deny  all;
            #}
        }
    
    
        # another virtual host using mix of IP-, name-, and port-based configuration
        #
        #server {
        #    listen       8000;
        #    listen       somename:8080;
        #    server_name  somename  alias  another.alias;
    
        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}
    
    
        # HTTPS server
        #
        #server {
        #    listen       443 ssl;
        #    server_name  localhost;
    
        #    ssl_certificate      cert.pem;
        #    ssl_certificate_key  cert.key;
    
        #    ssl_session_cache    shared:SSL:1m;
        #    ssl_session_timeout  5m;
    
        #    ssl_ciphers  HIGH:!aNULL:!MD5;
        #    ssl_prefer_server_ciphers  on;
    
        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}
    
    }
    

    /Users/doubledumbao/docker-demo/compose-demo目录下,添加docker-compose.yml

    version: '3'
    services: 
        nginx:
            image: nginx:alpine
            ports: 
            - 80:80
            volumes: 
            - /Users/doubledumbao/docker-demo/compose-demo/html:/usr/share/nginx/html
            - /Users/doubledumbao/docker-demo/compose-demo/conf/nginx.conf:/etc/nginx/nginx.conf
        php:
            image: devilbox/php-fpm:5.2-work-0.89
            volumes: 
            - /Users/doubledumbao/docker-demo/compose-demo/html:/var/www/html
        mysql:
            image: mysql:5.6
            environment: 
            - MYSQL_ROOT_PASSWORD=123456
    
    

    启动docker-compose

    docker-compose up
    

    打开浏览器,查看效果

    # 查看nginx配置效果
    http://localhost
    # 查看php配置效果
    http://localhost/test.php
    # 查看mysql配置效果
    http://localhost/mysql.php
    
  • 相关阅读:
    记第一次为开源代码报漏洞
    入职第三周——总结前两周学习内容
    入职一星期之感想
    毕业季之礼
    基于mint-ui的移动应用开发案例二(项目搭建)
    基于mint-ui的移动应用开发案例一(简介)
    工作笔记一——杂项
    微信小程序实战小小应用——豆瓣电影
    React学习之坑(二)- 基础入门
    React学习笔记(一)- 环境搭建
  • 原文地址:https://www.cnblogs.com/zhaoran8775/p/12852915.html
Copyright © 2020-2023  润新知