zoukankan      html  css  js  c++  java
  • docker知识7---docker-compose介绍

    docker-compose
      多数的现代应用通过多个更小的服务互相协同来组成一个完整可用的应用。比如一个简单的示例应用可能由如下 4 个服务组成。
        Web前端。
        订单管理。
        品类管理。
        后台数据库。
      将以上服务组织在一起,就是一个可用的应用。部署和管理繁多的服务是困难的。而这正是 Docker Compose 要解决的问题。
    Docker Compose,能够在 Docker 节点上,以单引擎模式(Single-Engine Mode)进行多容器应用的部署和管理。
    Docker Compose 并不是通过脚本和各种冗长的 docker 命令来将应用组件组织起来,而是通过一个声明式的配置文件描述整个应用,从而使用一条命令完成部署。

     ##案例1:docker启动多容器

     ##案例2:docker-compose启动多容器 wordpress、mysql

    cat <<eof> docker-compose.yml
    version: '3'
    
    services:
    
      wordpress:
        image: wordpress
        ports:
          - 1080:80
        environment:
          WORDPRESS_DB_HOST: mysql
          WORDPRESS_DB_PASSWORD: root
        networks:
          - my-bridge
    
      mysql:
        image: mysql
        environment:
          MYSQL_ROOT_PASSWORD: root
          MYSQL_DATABASE: wordpress
        volumes:
          - mysql-data:/var/lib/mysql
        networks:
          - my-bridge
    
    volumes:
      mysql-data:
    
    networks:
      my-bridge:
        driver: bridge
    eof
    docker-compose up
    
    [root@controller bin]# docker-compose up
    Creating network "bin_my-bridge" with driver "bridge"
    Creating volume "bin_mysql-data" with default driver
    Creating bin_wordpress_1 ... done
    Creating bin_mysql_1     ... done
    Attaching to bin_mysql_1, bin_wordpress_1
    mysql_1      | 2021-08-02 12:27:22+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.26-1debian10 started.
    wordpress_1  | WordPress not found in /var/www/html - copying now...
    mysql_1      | 2021-08-02 12:27:23+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
    mysql_1      | 2021-08-02 12:27:23+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.26-1debian10 started.
    mysql_1      | 2021-08-02 12:27:23+00:00 [Note] [Entrypoint]: Initializing database files
    mysql_1      | 2021-08-02T12:27:23.374792Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.26) initializing of server in progress as process 40
    mysql_1      | 2021-08-02T12:27:23.390372Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
    mysql_1      | 2021-08-02T12:27:24.263737Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
    wordpress_1  | Complete! WordPress has been successfully copied to /var/www/html
    wordpress_1  | No 'wp-config.php' found in /var/www/html, but 'WORDPRESS_...' variables supplied; copying 'wp-config-docker.php' (WORDPRESS_DB_HOST WORDPRESS_DB_PASSWORD)
    mysql_1      | 2021-08-02T12:27:26.525255Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1 is enabled for channel mysql_main
    mysql_1      | 2021-08-02T12:27:26.525755Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1.1 is enabled for channel mysql_main
    mysql_1      | 2021-08-02T12:27:26.553145Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
    wordpress_1  | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.18.0.3. Set the 'ServerName' directive globally to suppress this message
    wordpress_1  | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.18.0.3. Set the 'ServerName' directive globally to suppress this message
    wordpress_1  | [Mon Aug 02 12:27:27.376843 2021] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.38 (Debian) PHP/7.4.22 configured -- resuming normal operations
    wordpress_1  | [Mon Aug 02 12:27:27.377214 2021] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'
    mysql_1      | 2021-08-02 12:27:30+00:00 [Note] [Entrypoint]: Database files initialized
    mysql_1      | 2021-08-02 12:27:30+00:00 [Note] [Entrypoint]: Starting temporary server
    mysql_1      | 2021-08-02T12:27:31.451164Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.26) starting as process 87
    mysql_1      | 2021-08-02T12:27:31.553054Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
    mysql_1      | 2021-08-02T12:27:32.667455Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
    mysql_1      | 2021-08-02T12:27:33.300315Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1 is enabled for channel mysql_main
    mysql_1      | 2021-08-02T12:27:33.300660Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1.1 is enabled for channel mysql_main
    mysql_1      | 2021-08-02T12:27:33.303590Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
    mysql_1      | 2021-08-02T12:27:33.304035Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
    mysql_1      | 2021-08-02T12:27:33.308182Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
    mysql_1      | 2021-08-02T12:27:33.350230Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.26'  socket: '/var/run/mysqld/mysqld.sock'  port: 0  MySQL Community Server - GPL.
    mysql_1      | 2021-08-02T12:27:33.350283Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: /var/run/mysqld/mysqlx.sock
    mysql_1      | 2021-08-02 12:27:33+00:00 [Note] [Entrypoint]: Temporary server started.
    mysql_1      | Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
    mysql_1      | Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
    mysql_1      | Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
    mysql_1      | Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.
    mysql_1      | 2021-08-02 12:27:37+00:00 [Note] [Entrypoint]: Creating database wordpress
    mysql_1      | 
    mysql_1      | 2021-08-02 12:27:37+00:00 [Note] [Entrypoint]: Stopping temporary server
    mysql_1      | 2021-08-02T12:27:37.672903Z 11 [System] [MY-013172] [Server] Received SHUTDOWN from user root. Shutting down mysqld (Version: 8.0.26).
    mysql_1      | 2021-08-02T12:27:38.662161Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.26)  MySQL Community Server - GPL.
    mysql_1      | 2021-08-02 12:27:38+00:00 [Note] [Entrypoint]: Temporary server stopped
    mysql_1      | 
    mysql_1      | 2021-08-02 12:27:38+00:00 [Note] [Entrypoint]: MySQL init process done. Ready for start up.
    mysql_1      | 
    mysql_1      | 2021-08-02T12:27:38.968138Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.26) starting as process 1
    mysql_1      | 2021-08-02T12:27:38.980098Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
    mysql_1      | 2021-08-02T12:27:39.247591Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
    mysql_1      | 2021-08-02T12:27:39.501385Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1 is enabled for channel mysql_main
    mysql_1      | 2021-08-02T12:27:39.501621Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1.1 is enabled for channel mysql_main
    mysql_1      | 2021-08-02T12:27:39.502807Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
    mysql_1      | 2021-08-02T12:27:39.503103Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
    mysql_1      | 2021-08-02T12:27:39.506041Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
    mysql_1      | 2021-08-02T12:27:39.550616Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.26'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.
    mysql_1      | 2021-08-02T12:27:39.550639Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
    

      

     ##案例3:docker-compose启动多容器flask、redis

    mkdir /root/compose-flask-redis/
    cd !$
    
    cat <<eof> app.py
    from flask import Flask
    from redis import Redis
    import os
    import socket
    
    app = Flask(__name__)
    redis = Redis(host=os.environ.get('REDIS_HOST', '127.0.0.1'), port=6379)
    
    
    @app.route('/')
    def hello():
        redis.incr('hits')
        return 'Hello Container World! I have been seen %s times and my hostname is %s.
    ' % (redis.get('hits'),socket.gethostname())
    
    
    if __name__ == "__main__":
        app.run(host="0.0.0.0", port=5000, debug=True)
    eof
    
    cat <<eof> Dockerfile
    FROM python:2.7
    LABEL maintaner="Peng Xiao xiaoquwl@gmail.com"
    COPY . /app
    WORKDIR /app
    RUN pip install flask redis
    EXPOSE 5000
    CMD [ "python", "app.py" ]
    eof
    
    cat <<eof> docker-compose.yml
    version: "3"
    
    services:
    
      redis:
        image: redis
    
      web:
        build:
          context: .
          dockerfile: Dockerfile
        ports:
          - 2080:5000
        environment:
          REDIS_HOST: redis
    eof
    
    docker-compose -f docker-compose.yml
    curl http://192.168.66.10:2080/ 
    

     案例4:docker-compose案例3:LB-SCALE

    mkdir /root/compose-LB-SCALE/
    cd !$
    
    cat <<eof> app.py
    from flask import Flask
    from redis import Redis
    import os
    import socket
    
    app = Flask(__name__)
    redis = Redis(host=os.environ.get('REDIS_HOST', '127.0.0.1'), port=6379)
    
    
    @app.route('/')
    def hello():
        redis.incr('hits')
        return 'Hello Container World! I have been seen %s times and my hostname is %s.
    ' % (redis.get('hits'),socket.gethostname())
    
    
    if __name__ == "__main__":
        app.run(host="0.0.0.0", port=80, debug=True)
    eof
    
    cat <<eof> Dockerfile
    FROM python:2.7
    LABEL maintaner="chalon"
    COPY . /app
    WORKDIR /app
    RUN pip install flask redis
    EXPOSE 80
    CMD [ "python", "app.py" ]
    eof
    
    cat <<eof> docker-compose.yml
    version: "3"
    
    services:
    
      redis:
        image: redis
    
      web:
        build:
          context: .
          dockerfile: Dockerfile
        environment:
          REDIS_HOST: redis
    
      lb:
        image: dockercloud/haproxy
        links:
          - web
        ports:
          - 1080:80
        volumes:
          - /var/run/docker.sock:/var/run/docker.sock 
    eof
    
    docker-compose -f docker-compose.yml up
    curl http://192.168.66.10:1080/ 

    docker-compose scale web=3 #设置web服务的数量为3个;
    curl http://192.168.66.10:1080/ #验证web服务;
    curl http://192.168.66.10:1080/ #验证web服务;
    curl http://192.168.66.10:1080/ #验证web服务;

     

    ##docker-compose案例4:voting-app
      应用架构:5个模块;voting app负责对外提供投票服务,其投票产生的数据由redis消息队列进行缓存,java worker从消息队列读取投票数据后并将其写入数据库,result app从数据库读取数据进行统计并输出结果。

    mkdir voting-pro
    cd voting-pro
    cat <<eof> docker-compose.yml  #字段未设置参数时表示使用默认参数,如front-tier:;
    version: "3"
    
    services:
      voting-app:
        build: ./voting-app/.
        volumes:
         - ./voting-app:/app
        ports:
          - "5000:80"
        links:
          - redis
        networks:
          - front-tier
          - back-tier
    
      result-app:
        build: ./result-app/.
        volumes:
          - ./result-app:/app
        ports:
          - "5001:80"
        links:
          - db
        networks:
          - front-tier
          - back-tier
    
      worker:
        build: ./worker
        links:
          - db
          - redis
        networks:
          - back-tier
    
      redis:
        image: redis
        ports: ["6379"]
        networks:
          - back-tier
    
      db:
        image: postgres:9.4
        volumes:
          - "db-data:/var/lib/postgresql/data"
        networks:
          - back-tier
    
    volumes:
      db-data:
    
    networks:
      front-tier:
      back-tier:
    eof
    
    ~~~上传程序源代码:voting app、woker、result app;
    docker pull java:7
    docker pull postgres:9.4
    docker pull redis
    docker pull python:2.7
    docker pull node:0.10
    docker-compose build  #先将dockerfile创建镜像
    docker images 
    docker-compose up     #启动多容器;
    

      

     

      

     

  • 相关阅读:
    Python进程池
    Python进程间通信
    python编程中的if __name__ == 'main与windows中使用多进程
    Python进程-实现
    python进程join()函数理解
    python 进程池Pool
    python多进程打印字符,加锁(Lock加锁)
    python进程、多进程
    正则表达式统计字符串中数字的个数
    python 处理xml
  • 原文地址:https://www.cnblogs.com/chalon/p/15089549.html
Copyright © 2011-2022 走看看