zoukankan      html  css  js  c++  java
  • Docker Compose

    简介 

      Compose 项目是Docker官方的开源项目,负责实现对Docker容器集群的快速编排。

      Compose 默认模板(YAML格式)文件 docker-compose.yml 

      Compose 中的两个概念:

        服务(service) :一个应用的容器,实际上可以包括若干运行相同镜像的容器实例。

        项目(project) :由一组关联的应用容器组成一个完整业务单元,在docker-compose.yml文件中定义。

      

      Compose 的默认管理对象是项目,通过子命令对项目中的一组容器进行便捷地生命周期管理。

      Compose 项目由Python编写,实现了调用了Docker服务提供的API对容器进行管理。因此只要所操作的平台支持Docker API ,就可以在其上利用Compose来进行编排管理。

    安装

    # pip 安装
    [root@localhost ~]# pip install -U docker-compose
    # 二进制包 官方定义好的二进制包,下载下来即可使用
    [root@localhost ~]# curl -L https://github.com/docker/compose/releases/download/1.18.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
    [root@localhost ~]# chmod +x /usr/local/bin/docker-compose
    # 查看命令路径
    [root@localhost ~]# which docker-compose
    /usr/local/python3.5/bin/docker-compose
    # 查看版本信息
    [root@localhost ~]# docker-compose  version 
    docker-compose version 1.16.1, build 6d1ac219
    docker-py version: 2.5.1
    CPython version: 3.5.2
    OpenSSL version: OpenSSL 1.0.2k-fips  26 Jan 2017
    # pip卸载
    [root@localhost ~]# pip uninstall  docker-compose
    # 二进制卸载
    [root@localhost ~]# rm /usr/local/bin/docker-compose
    

    Compose 命令说明 

    [root@localhost ~]# docker-compose
    Define and run multi-container applications with Docker.
    
    Usage:
      docker-compose [-f ...] [options] [COMMAND] [ARGS...]
      docker-compose -h|--help
    
    Options:
      # -f, --file FILE 指定使用的Compose模板文件,默认为docker-compose.yml 
      -f, --file FILE             Specify an alternate compose file (default: docker-compose.yml)
      # -p, --project-name NAME 指定项目名称,默认将使用所在目录名称作为项目名称。
      -p, --project-name NAME     Specify an alternate project name (default: directory name)
      --verbose                   Show more output
      --no-ansi                   Do not print ANSI control characters
      -v, --version               Print version and exit
      -H, --host HOST             Daemon socket to connect to
    
      --tls                       Use TLS; implied by --tlsverify
      --tlscacert CA_PATH         Trust certs signed only by this CA
      --tlscert CLIENT_CERT_PATH  Path to TLS certificate file
      --tlskey TLS_KEY_PATH       Path to TLS key file
      --tlsverify                 Use TLS and verify the remote
      --skip-hostname-check       Don't check the daemon's hostname against the name specified
                                  in the client certificate (for example if your docker host
                                  is an IP address)
      --project-directory PATH    Specify an alternate working directory
                                  (default: the path of the Compose file)
    
    Commands:
      build              Build or rebuild services
      bundle             Generate a Docker bundle from the Compose file
      config             Validate and view the Compose file
      create             Create services
      down               Stop and remove containers, networks, images, and volumes
      events             Receive real time events from containers
      exec               Execute a command in a running container
      help               Get help on a command
      images             List images
      kill               Kill containers
      logs               View output from containers
      pause              Pause services
      port               Print the public port for a port binding
      ps                 List containers
      pull               Pull service images
      push               Push service images
      restart            Restart services
      rm                 Remove stopped containers
      run                Run a one-off command
      scale              Set number of containers for a service
      start              Start services
      stop               Stop services
      top                Display the running processes
      unpause            Unpause services
      up                 Create and start containers
      version            Show the Docker-Compose version information
    
    

    build

    # build
    [root@localhost ~]# docker-compose build --help 
    Build or rebuild services.
    
    Services are built once and then tagged as `project_service`,
    e.g. `composetest_db`. If you change a service's `Dockerfile` or the
    contents of its build directory, you can run `docker-compose build` to rebuild it.
    
    Usage: build [options] [--build-arg key=val...] [SERVICE...]
    
    Options:
        --force-rm              Always remove intermediate containers.
    	# --force-rm 		删除构建过程中的临时容器。
        --no-cache              Do not use cache when building the image.
    	# --no-cache		构建镜像过程中不适用缓存(这将加长构建过程)。
        --pull                  Always attempt to pull a newer version of the image.
    	# --pull			始终尝试通过拉取操作来获取更新版本的镜像。
        --build-arg key=val     Set build-time variables for one service.
    	# --build-arg  key=val  设置变量(Dockerfile 中定义的ARG变量)
    	# AGR 变量不像ENV变量始终存在镜像中。不过ARG变量会以类似的方式对构建缓存产生影响。
    	# 如果Dockerfile中定义的ARG变量的值与之前定义的变量值不一样,那么就有可能产生"cache miss"。
    	# 比如RUN指令使用ARG定义的变量时,ARG变量的值变了之后,就会导致缓存失效。
    

    kill

    # kill
    [root@localhost ~]# docker-compose kill --help 
    Force stop service containers.
    
    Usage: kill [options] [SERVICE...]
    
    Options:
        -s SIGNAL         SIGNAL to send to the container.
                          Default signal is SIGKILL.
    	# 通过发送SIGKILL 信号强制停止服务容器。
    	# 支持通过-s 参数来指定发送的信号,
    	# 例如: docker-compose  -f /opt/compose-conf/jenkins/jenkins.yml  kill -s SIGINT
    

    logs

    # logs 查看服务器的输出
    # 默认情况下将对不同的服务输出使用不同颜色来区分
    # 可使用 --no-color  关闭颜色输出
    [root@localhost ~]# docker-compose logs --help 
    View output from containers.
    
    Usage: logs [options] [SERVICE...]
    
    Options:
        --no-color          Produce monochrome output.
    	# --no-color 关闭颜色输出
        -f, --follow        Follow log output.
    	# -f 按照日志格式输出 
    	# docker-compose  -f /opt/compose-conf/jenkins/jenkins.yml logs -f
        -t, --timestamps    Show timestamps.
    	# -t  timestamps时间格式输出
    	# docker-compose  -f /opt/compose-conf/jenkins/jenkins.yml logs -ft
        --tail="all"        Number of lines to show from the end of the logs
                            for each container.
    	# 查看日志末尾显示行数
    	#  docker-compose  -f /opt/compose-conf/jenkins/jenkins.yml logs -ft --tail=10
    

    ps

    # ps 列出项目中所有容器
    # -q 返回容器的ID
    # docker-compose  -f /opt/compose-conf/jenkins/jenkins.yml ps -q
    

    config

    # config 
    # 显示配置文件信息
    # docker-compose  -f /opt/compose-conf/jenkins/jenkins.yml config
    # --services 显示服务名称
    # docker-compose  -f /opt/compose-conf/jenkins/jenkins.yml config --services
    # --volumes 显示挂载信息
    # docker-compose  -f /opt/compose-conf/jenkins/jenkins.yml config  --volumes
    

    pull

    # pull 拉取服务依赖的镜像
    # docker-compose  -f /opt/compose-conf/jenkins/jenkins.yml pull
    # --ignore-pull-failures 忽略拉取镜像过程中的错误
    # --parallel 并发拉取多个镜像
    # --quiet  不显示进度信息
    # docker-compose  -f /opt/compose-conf/jenkins/jenkins.yml pull --ignore-pull-failures --parallel
    

    restart

    # restart 重启项目中的服务
    # -t, --timeout TIMEOUT 指定重启前停止容器的超时(默认10s)
    

    rm

    # rm 删除所有停止状态的服务容器。
    # -f, --force  强制删除,包括非停止的容器。
    # -s, --stop   先停止容器,然后删除
    # -v   删除容器所挂载的数据卷
    # [root@localhost ~]# docker-compose  -f /opt/compose-conf/jenkins/jenkins.yml rm -sf
    

    run

    # run 在指定服务上执行一个命令
    # docker-compose  -f /opt/compose-conf/jenkins/jenkins.yml run jenkins ping www.baidu.com
    # 默认情况下,如果存在关联,则所有关联的服务将会自动被启动,除非这些服务已经在运行中。
    # 该命令类似于启动容器后运行指定的命令,相关卷、链接等都将会按照配置自动创建。
    # 给定命令将会覆盖原有的自动运行命令。
    # 不会自动创建端口,以避免冲突
    # --no-deps  不自动启动关联的容器
    # -d 在后台运行服务容器。
    # --name NAME 为容器指定一个名字。
    # --entrypoint CMD 覆盖默认的容器启动指令。
    # -e KEY=VAL 设置环境变量值,可多次使用选项来设置多个环境变量。
    # -u, --user="" 指定运行容器的用户或者 uid。
    # --rm 运行命令后自动删除容器,d 模式下将忽略
    # -p, --publish=[] 映射容器端口到本地主机
    # --service-ports 配置服务端口并映射到本地主机
    # -T 不分配伪tty,意味着依赖tty的指令将无法运行
    # -v, --volume=[]       挂载卷,默认[] 
    

    scale

    # scale 设置指定服务运行的容器个数
    docker-compose  -f /opt/compose-conf/jenkins/jenkins.yml scale jenkins=5
    # 一般情况下,当指定数目多于改服务器低昂去实际运行容器,将新创建并启动容器;反之,将停止容器。
    # -t , --timeout TIMEOUT 停止容器时候的超时(默认10s)
    

    start

    # start 
    # 启动已经存在的服务容器。
    # [root@localhost ~]# docker-compose  -f /opt/compose-conf/jenkins/jenkins.yml start
    

    stop

    # stop 停止已处于运行状态的容器,不删除。可以通过docker-compose start 再次启动
    # -t , --timeout TIMEOUT 停止容器时候的超时(默认10s)
    # [root@localhost ~]# docker-compose  -f /opt/compose-conf/jenkins/jenkins.yml stop
    # [root@localhost ~]# docker-compose  -f /opt/compose-conf/jenkins/jenkins.yml start
    

    pause , unpause

    # pause 暂停一个服务容器
    # [root@localhost ~]# docker-compose  -f /opt/compose-conf/jenkins/jenkins.yml pause
    
    # unpause 恢复处于暂停状态的服务
    # [root@localhost ~]# docker-compose  -f /opt/compose-conf/jenkins/jenkins.yml unpause
    

    up

    # up 
    # 尝试自动完成包括构建镜像,重新创建服务,启动服务,并关联服务相关容器的一系列操作。
    # 链接的服务将会被自动启动,除非已经处于运行状态。
    # -d 在后台运行服务容器。
    # --no-color 不使用颜色来区分不同的服务的控制台输出。
    # --no-deps 不启动服务所链接的容器
    # --force-recreate 强制重新创建容器,不能与--no-recreate 同时使用。
    # --no-recreate 如果容器已经存在,则不重新创建,不能与--force-recreate 同时使用。
    # --no-build 不自动构建缺失的服务镜像。
    # --abort-on-container-exit 如果任何容器停止,则停止所有容器,不能与-d 同时使用。
    # -t, --timeout TIMEOU 停止容器时候的超时(默认10s)
    # --remove-orphans 移除服务容器,不改变文件中的定义
    # --exit-code-from SERVICE 返回所选服务容器的退出码
    # --scale SERVICE=NUM 将SERVICE 放到num个实例。覆盖scale 在文件中设置
    # [root@localhost ~]# docker-compose  -f /opt/compose-conf/jenkins/jenkins.yml up -d
    
  • 相关阅读:
    内存碎片
    内核对象之异步IO请求完成时调用一个函数
    线程池之在内核对象触发时调用一个对象
    线程池之每隔一段时间调用一个函数
    线程池
    logging模块
    装饰器
    django 2.0 url匹配
    django models 文件夹
    转载
  • 原文地址:https://www.cnblogs.com/lmx1002/p/8399331.html
Copyright © 2011-2022 走看看