继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Docker Compose

Coley_5
关注TA
已关注
手记 86
粉丝 8548
获赞 6550

简介

Docker Compose 是 Docker 官方编排(Orchestration)项目之一,负责快速的部署分布式应用。

它允许用户通过一个单独的 docker-compose.yml 模板文件(YAML 格式)来定义一组相关联的应用容器为一个项目(project)。

Compose 中有两个重要的概念:

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

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

安装


curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
docker-compose -v
docker-compose version 1.22.0, build f46880fe

Compose命令说明

docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]
Options:
  -f, --file FILE             指定Compose模版文件 (默认: docker-compose.yml)
  -p, --project-name NAME     指定项目名称(默认当前目录名称)
  --verbose                   显示更多的调试信息
  --log-level LEVEL           设置log输出级别 (DEBUG, INFO, WARNING, ERROR, CRITICAL)
  --no-ansi                   不打印ANSI控制字符
  -v, --version               输出版本信息
  -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
  --project-directory PATH    Specify an alternate working directory
                              (default: the path of the Compose file)
  --compatibility             If set, Compose will attempt to convert deploy
                              keys in v3 files to their non-Swarm equivalent
=============================================================================
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

命令使用说明

  • up
    该命令十分强大,它将尝试自动完成包括构建镜像,(重新)创建服务,启动服务,并关联服务相关容器的一系列操作。
    链接的服务都将会被自动启动,除非已经处于运行状态。
    可以说,大部分时候都可以直接通过该命令来启动一个项目。
    默认情况, docker-compose up 启动的容器都在前台,控制台将会同时打印所有容器的输出信息,可以很方便进行调试。
    当通过 Ctrl-C 停止命令时,所有容器将会停止。
    如果使用 docker-compose up -d ,将会在后台启动并运行所有的容器。一般推荐生产环境下使用该选项。

    up [options] [--scale SERVICE=NUM...] [SERVICE...]
    Options:
    -d, --detach               在后台运行服务容器。
    --no-color                 Produce monochrome output.
    --quiet-pull               Pull without printing progress information
    --no-deps                  Don't start linked services.
    --force-recreate           Recreate containers even if their configuration
                               and image haven't changed.
    --always-recreate-deps     Recreate dependent containers.
                               Incompatible with --no-recreate.
    --no-recreate              If containers already exist, don't recreate
                               them. Incompatible with --force-recreate and -V.
    --no-build                 Don't build an image, even if it's missing.
    --no-start                 Don't start the services after creating them.
    --build                    Build images before starting containers.
    --abort-on-container-exit  Stops all containers if any container was
                               stopped. Incompatible with -d.
    -t, --timeout TIMEOUT      Use this timeout in seconds for container
                               shutdown when attached or when containers are
                               already running. (default: 10)
    -V, --renew-anon-volumes   Recreate anonymous volumes instead of retrieving
                               data from the previous containers.
    --remove-orphans           Remove containers for services not defined
                               in the Compose file.
    --exit-code-from SERVICE   Return the exit code of the selected service
                               container. Implies --abort-on-container-exit.
    --scale SERVICE=NUM        Scale SERVICE to NUM instances. Overrides the
                               `scale` setting in the Compose file if present.
  • build
    构建(重新构建)项目中的服务容器

    docker-compose build [options] [--build-arg key=val...] [SERVICE...]
    Options:
    --compress              开启gzip压缩.
    --force-rm              删除构建过程中的临时容器.
    --no-cache              构建过程中不使用缓存.
    --pull                  构建过程中拉取最新版本的镜像.
    -m, --memory MEM        设置构建容器的内存限制.
    --build-arg key=val     Set build-time variables for services.
  • config
    验证 Compose 文件格式是否正确,若正确则显示配置,若格式错误显示错误原因。

  • down
    此命令将会停止 up 命令所启动的容器,并移除网络

  • exec
    进入指定的容器。

  • images
    列出 Compose 文件中包含的镜像。

  • kill
    格式为 docker-compose kill [options] [SERVICE...] 。
    通过发送 SIGKILL 信号来强制停止服务容器。
    支持通过 -s 参数来指定发送的信号,例如通过如下指令发送 SIGINT 信号。
    docker-compose kill -s SIGINT
  • logs
    查看服务容器的输出

    docker-compose logs [options] [SERVICE...]
  • pause
    暂停一个服务容器。

    docker-compose pause [SERVICE...]  
  • port
    打印某个容器端口所映射的公共端口。

    docker-compose port [options] SERVICE PRIVATE_PORT  
  • ps
    列出项目中目前的所有容器

    docker-compose ps [options] [SERVICE...] 
  • pull
    拉取服务依赖的镜像

    docker-compose pull [options] [SERVICE...]
  • push
    推送服务依赖的镜像dao到Docker的镜像仓库

    docker-compose push[options] [SERVICE...]
  • restart
    重启项目中的服务

  • rm
    删除所有(停止状态的)服务容器。推荐先执行 docker-compose stop 命令来停止容器。

  • run
    在指定服务上执行一个命令。

  • scale
    设置指定服务运行的容器个数。

  • start
    启动已经存在的服务容器。

  • stop
    停止已经处于运行状态的容器,但不删除它。通过 docker-compose start 可以再次启动这些容器。

  • top
    查看各个服务容器内运行的进程。

  • unpause
    恢复处于暂停状态中的服务。

Compose模版文件
默认的模板文件名称为 docker-compose.yml ,格式为 YAML 格式。
e.g

services:
    web:
        build: .
        ports:
            - "5000:5000"
    redis:
        image: "redis:alpine"

注意每个服务都必须通过 image 指令指定镜像或 build 指令(需要 Dockerfile)等来自动构建生成镜像。
如果使用 build 指令,在 Dockerfile 中设置的选项(例如: CMD , EXPOSE , VOLUME , ENV等) 将会自动被获取,无需在 docker-compose.yml 中再次设置。
模版文件指令

  • build
    指定 Dockerfile 所在文件夹的路径(可以是绝对路径,或者相对 docker-compose.yml 文件的路径)。 Compose 将会利用它自动构建这个镜像,然后使用这个镜像。
    version: '3'
    services:
    webapp:
        build: ./dir

    可以使用 context 指令指定 Dockerfile 所在文件夹的路径。
    使用 dockerfile 指令指定 Dockerfile 文件名。
    使用 arg 指令指定构建镜像时的变量。

    version: '3'
    services:
    webapp:
        build:
            context: ./dir  #指定Dockerfile路径
            dockerfile: Dockerfile-alternate # 指定Dockerfile文件名称
        args:
            buildno: 1
  • cap_add, cap_drop
    指定容器的内核能力(capacity)分配。
    # 让容器拥有所有能力可以指定为:
    cap_add:
    - ALL
    # 去掉 NET_ADMIN 能力可以指定为:
    cap_drop:
    - NET_ADMIN
  • command
    覆盖容器启动后默认执行的命令。

    command: echo "hello world"
  • cgroup_parent
    指定父 cgroup 组,意味着将继承该组的资源限制。

  • container_name
    指定容器名称。默认将会使用 项目名称_服务名称_序号 这样的格式。

  • devices
    指定设备映射关系。

  • depends_on
    解决容器的依赖、启动先后的问题。以下例子中会先启动 redis db 再启动 web

    version: '3'
    services:
    web:
        build: .
        depends_on:
            - db
            - redis
    redis:
        image: redis
    db:
        image: postgres
  • dns
    自定义 DNS 服务器。可以是一个值,也可以是一个列表。

    dns: 8.8.8.8
    dns:
    - 8.8.8.8
    - 114.114.114.114
  • dns_search
    配置 DNS 搜索域。可以是一个值,也可以是一个列表。
    dns_search: example.com
    dns_search:
    - domain1.example.com
    - domain2.example.com
  • tmpfs
    挂载一个 tmpfs 文件系统到容器。

  • env_file
    从文件中获取环境变量,可以为单独的文件路径或列表。

  • environment
    设置环境变量。你可以使用数组或字典两种格式。

  • expose
    暴露端口,但不映射到宿主机,只被连接的服务访问。
    仅可以指定内部端口为参数

    expose:
    - "3000"
    - "8000"
  • extra_hosts
    类似 Docker 中的 --add-host 参数,指定额外的 host 名称映射信息。

    extra_hosts:
    - "googledns:8.8.8.8"
    - "dockerhub:52.1.157.61"
  • healthcheck
    通过命令检查容器是否健康运行。

  • image
    指定为镜像名称或镜像 ID。如果镜像在本地不存在, Compose 将会尝试拉取这个镜像。

  • labels
    为容器添加 Docker 元数据(metadata)信息

  • network_mode
    设置网络模式。使用和 docker run 的 --network 参数一样的值。

  • networks
    配置容器连接的网络。

  • pid
    跟主机系统共享进程命名空间。打开该选项的容器之间,以及容器和宿主机系统之间可以通过进程 ID 来相互访问和操作。

  • ports
    暴露端口信息。
    使用宿主端口:容器端口 (HOST:CONTAINER) 格式,或者仅仅指定容器的端口(宿主将会随机选择端口)都可以。

  • secrets
    存储敏感数据,例如 mysql 服务密码。

  • security_opt
    指定容器模板标签(label)机制的默认属性(用户、角色、类型、级别等)。例如配置标签的用户名和角色名。

  • stop_signal
    设置另一个信号来停止容器。在默认情况下使用的是 SIGTERM 停止容器。

  • sysctls
    配置容器内核参数。

    sysctls:
    net.core.somaxconn: 1024
    net.ipv4.tcp_syncookies: 0
    sysctls:
    - net.core.somaxconn=1024
    - net.ipv4.tcp_syncookies=0
  • ulimits
    指定容器的 ulimits 限制值。

  • volumes
    数据卷所挂载路径设置。可以设置宿主机路径 ( HOST:CONTAINER ) 或加上访问模式( HOST:CONTAINER:ro )。
    该指令中路径支持相对路径。

参看资料
https://yeasy.gitbooks.io/docker_practice/content/

打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP