• DOCKER 学习笔记4 认识DockerCompose 多容器编排


    前言

    通过上一节的学习,学会了如何在Linux 环境下搭建Docker并且部署Springboot 项目,并且成功的跑了起来,当然,在生产环境中,不只是需要一个后端的Web 项目,还需要比如 Nginx 作为反向代理。数据库也需要单独部署在一个容器里面,要是我们像之前学过的那样一个个部署,那岂不是很麻烦。

    所以,我们需要一套东西来帮助我们实现这个功能,那就是今天要学习的Docker Compose 容器编排技术。

    Docker Compose

    Docker Compose 用于定义和运行多容器Docker应用程序的工具,通过YAML文件来定义应用程序里面的服务。而后,通过一个命令,即可启动这些容器所产生的服务。

    基本步骤:

    1. 定义 Dockerfile 自定义镜像
    2. 编辑 docker-compose.yml 进行容器服务的编排
    3. docker-compose up 创建和启动容器服务

    安装 Compose

    Github 安装

    从github 拉取最新版本的Compose 本地编译安装。推荐方式,错误少,可直接安装。

    github https://github.com/docker/compose/releases

    当前Github 最新版本为 1.25.4

    ## 下载最新版的Docker Compose 二进制包
    curl -L https://github.com/docker/compose/releases/download/1.25.4/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
    
    ## 赋予可执行权限
    chmod +x /usr/local/bin/docker-compose
    
    ## 检查版本信息
    docker-compose -v
    
    

    image.png

    pip 安装

    这是种比较保守的安装方法,相较于上面的安装方法,建议还是采用上面的安装方法。

    ## 安装epel 额外的源
    yum -y install epel-release
    
    ## 安装python-pip
    yum -y install python-pip
    
    ## 更新pip 到最新版本
    pip install -U pip
    
    ## install
    pip install docker-componse
    
    ## 检查安装情况
    docker-compose -v
    
    

    测试使用

    确保我们已经正常的安装了Docker Compose,我们将采用官网的DEMO 进行演示,在此,你不需要安装Python 以及Redis

    1. 创建目录

    
    mkdir -p composetest
    
    cd composetest
    
    

    2. 创建一个pyton web

    
    ## 创建app.py
    vi app.py
    
    ## 复制以下内容到文件内保存退出
    
    import time
    
    import redis
    from flask import Flask
    
    app = Flask(__name__)
    cache = redis.Redis(host='redis', port=6379)
    
    
    def get_hit_count():
        retries = 5
        while True:
            try:
                return cache.incr('hits')
            except redis.exceptions.ConnectionError as exc:
                if retries == 0:
                    raise exc
                retries -= 1
                time.sleep(0.5)
    
    
    @app.route('/')
    def hello():
        count = get_hit_count()
        return 'Hello World! I have been seen {} times.
    '.format(count)
    
    
    ## 创建一个文件用于保存命令
    vi requirements.txt
    
    ## 保存以下内容
    flask
    redis
    

    自定义镜像

    
    ## 创建新的Dockerfile 
    vi Dockerfile
    
    ## 加入以下内容
    
    FROM python:3.7-alpine
    WORKDIR /code
    ENV FLASK_APP app.py
    ENV FLASK_RUN_HOST 0.0.0.0
    RUN apk add --no-cache gcc musl-dev linux-headers
    COPY requirements.txt requirements.txt
    RUN pip install -r requirements.txt
    COPY . .
    CMD ["flask", "run"]
    
    

    这告诉Docker:

    从Python 3.7映像开始构建映像。
    将工作目录设置为。/code
    设置命令使用的环境变量。flask
    安装gcc,以便诸如MarkupSafe和SQLAlchemy之类的Python包可以编译加速。
    复制并安装Python依赖项。requirements.txt
    将项目中的当前目录复制到映像中的工作目录。..
    将容器的默认命令设置为。flask run

    撰写容器编排

    
    ## 创建docker-compose.yml 文件
    vi docker-compose.yml
    
    ## 加入以下内容
    
    version: '3'
    services:
      web:
        build: .
        ports:
          - "5000:5000"
      redis:
        image: "redis:alpine"
    

    网络服务

    该web服务使用从Dockerfile当前目录中构建的映像。然后,它将容器和主机绑定到暴露的端口5000。此示例服务使用Flask Web服务器的默认端口5000。

    Redis服务

    该redis服务使用 从Docker Hub注册表中提取的公共Redis映像。

    运行服务

    docker-compose up

    Starting composetest_redis_1 ... done
    Recreating composetest_web_1 ... done
    Attaching to composetest_redis_1, composetest_web_1
    redis_1  | 1:C 09 Feb 2020 02:14:31.462 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
    redis_1  | 1:C 09 Feb 2020 02:14:31.462 # Redis version=5.0.7, bits=64, commit=00000000, modified=0, pid=1, just started
    redis_1  | 1:C 09 Feb 2020 02:14:31.462 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
    redis_1  | 1:M 09 Feb 2020 02:14:31.463 * Running mode=standalone, port=6379.
    redis_1  | 1:M 09 Feb 2020 02:14:31.463 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
    redis_1  | 1:M 09 Feb 2020 02:14:31.463 # Server initialized
    redis_1  | 1:M 09 Feb 2020 02:14:31.463 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
    redis_1  | 1:M 09 Feb 2020 02:14:31.463 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
    redis_1  | 1:M 09 Feb 2020 02:14:31.463 * DB loaded from disk: 0.000 seconds
    redis_1  | 1:M 09 Feb 2020 02:14:31.463 * Ready to accept connections
    web_1    |  * Serving Flask app "app.py"
    web_1    |  * Environment: production
    web_1    |    WARNING: This is a development server. Do not use it in a production deployment.
    web_1    |    Use a production WSGI server instead.
    web_1    |  * Debug mode: off
    web_1    |  * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
    
    

    服务成功的运行在5000端口下,访问尝试,而后刷新,观察计数器的变化!
    image.png

    常用命令

    docker-compose up -d 后台运行服务

    [root@mrclinux composetest]# docker-compose up -d
    Starting composetest_web_1   ... done
    Starting composetest_redis_1 ... done
    

    docker-compose ps 查看当前的运行容器

    [root@mrclinux composetest]# docker-compose ps
           Name                      Command               State           Ports         
    -------------------------------------------------------------------------------------
    composetest_redis_1   docker-entrypoint.sh redis ...   Up      6379/tcp              
    composetest_web_1     flask run                        Up      0.0.0.0:8080->5000/tcp
    
    

    docker-compose images 查看已存在的镜像信息

    [root@mrclinux composetest]# docker-compose images
         Container          Repository       Tag       Image Id       Size  
    ------------------------------------------------------------------------
    composetest_redis_1   docker.io/redis   alpine   b68707e68547   29.78 MB
    composetest_web_1     composetest_web   latest   0a812986cca6   221.8 MB
    
    

    docker-compose stop 用来停止容器,不删除它,可以再次使用start启用

    [root@mrclinux composetest]# docker-compose stop
    Stopping composetest_web_1   ... done
    Stopping composetest_redis_1 ... done
    
    

    docker-compose start 使用start 启动存在的容器服务

    [root@mrclinux composetest]# docker-compose start
    Starting web   ... done
    Starting redis ... done
    

    docker-compose down 停止服务并且移除容器

    
    [root@mrclinux composetest]# docker-compose down
    Stopping composetest_web_1   ... done
    Stopping composetest_redis_1 ... done
    Removing composetest_web_1   ... done
    Removing composetest_redis_1 ... done
    Removing network composetest_default
    
    

    docker-compose up 创建以及启动服务

    参考

    https://docs.docker.com/compose/

    https://www.cnblogs.com/ityouknow/p/8648467.html

  • 相关阅读:
    遇到的StageFright问题 一 音视频因ALSA不同步
    stagefright
    细数开源历史上的九个重大事件
    AOP concepts (from spring.net document)
    javascript ECMA262概述
    Spring.net配置相关
    NUnit2.5 测试类几个方法
    Bill Gates 哈佛大学演讲 Never Surrend to Complexity
    iis6.0支持GZIP的详细设置方法
    Hessian是一个轻量级的remoting onhttp工具
  • 原文地址:https://www.cnblogs.com/ChromeT/p/12286484.html
Copyright © 2020-2023  润新知