Docker核心技术(六):Docker Compose

本文介绍DockerCompose的基础概念,包括其定义、管理多个Docker容器的方式及安装方法,并概述了DockerCompose文件的版本和主要配置选项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、Docker Compose 简介

Docker Compose是什么?

  • Docker Compose是一个能一次性定义和管理多个Docker容器的工具。

  • 详细地说:

    • Compose中定义和启动的每一个容器都相当于一个服务(service)
    • Compose中能定义和启动多个服务,且它们之间通常具有协同关系
  • 管理方式:

    • 使用YAML文件来配置我们应用程序的服务。
    • 使用单个命令(docker-compose up),就可以创建并启动配置文件中配置的所有服务。

Docker Compose 工作原理

在这里插入图片描述

Docker Compose安装

  • Docker for Mac与Docker for Windows自带docker-compose

  • Linux下需要单独安装:

    • 第一步:sudo curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose- ( u n a m e − s ) − (uname -s)- (unames)(uname -m) -o /usr/local/bin/docker-compose
    • 第二步:sudo chmod +x /usr/local/bin/docker-compose
      终端中使用docker-compose --version查看安装的版本
      这里示例安装版本是1.21.2,很可能您看到这里时,已经出现更新的版本,因此建议换成最新版本。查看最新版本
  • 其他安装方法查看

Docker Compose CLI

  • 利用docker-compose --help查看或者查看官方文档

  • 对比后会发现:Docker Compose CLI的很多命令的功能和Docker Client CLI是相似的。最主要的区别就是前者能一次性运行管理多个容器,后者只能一次管理一个。

C:\Users\lenovo>docker-compose --version
docker-compose version 1.27.4, build 40524192

C:\Users\lenovo>docker-compose --help
Define and run multi-container applications with Docker.

Usage:
  docker-compose [-f <arg>...] [options] [--] [COMMAND] [ARGS...]
  docker-compose -h|--help

Options:
  -f, --file FILE             Specify an alternate compose file
                              (default: docker-compose.yml)
  -p, --project-name NAME     Specify an alternate project name
                              (default: directory name)
  -c, --context NAME          Specify a context name
  --verbose                   Show more output
  --log-level LEVEL           Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
  --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
  --project-directory PATH    Specify an alternate working directory
                              (default: the path of the Compose file)
  --compatibility             If set, Compose will attempt to convert keys
                              in v3 files to their non-Swarm equivalent (DEPRECATED)
  --env-file PATH             Specify an alternate environment file

Commands:
  build              Build or rebuild services
  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 version information and quit

C:\Users\lenovo>

二、了解 Docker Compose File

Docker Compose File版本

  • Docker Compose File 有多个版本,基本是向后兼容的,但也有极个别配置项高版本中没有。
  • 在docker-compose.yml一开始就需要利用version关键词标明当前file使用的版本

在这里插入图片描述

Docker Compose File TOP配置参数概览

Docker Compose File 顶级配置项:

  • version:指定Docker Compose File版本号
  • services:定义多个服务并配置启动参数
  • volumes:声明或创建在多个服务中共同使用的数据卷对象
  • networks:定义在多个服务中共同使用的网络对象
  • configs:声明将在本服务中要使用的一些配置文件
  • secrets:声明将在本服务中要使用的一些秘钥、密码文件
  • x-***:自定义配置。主要用于复用相同的配置。

更多详细配置

三、Docker Compose 应用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值