认识
官网:None
文档:https://docs.docker.com/compose/
仓库:https://github.com/docker/compose
Compose,是个用于定义和运行多个容器的工具。Docker Compose is a tool for defining and running multi-container applications. It is the key to unlocking a streamlined and efficient development and deployment experience. 简而言之,Compose 是个容器编排工具。
组成
- libcompose https://github.com/docker-archive-public/docker.libcompose
- compose-go https://github.com/compose-spec/compose-go
[Proposal] Rewrite in go #94 https://github.com/docker/compose/issues/94
docker-compose
docker-compose.yaml
docker
性质
功能 | 特性 | 学习笔记
Buildx
Docker Compose 也支持 buildx 构建:
Use buildx build linux/arm64 in docker-compose file – Stack Overflow
Compose file version 2 reference | Docker Documentation
提供镜像构建功能 Build Images
https://docs.docker.com/reference/compose-file/build/
在 Docker Compose 中,可以使用 docker-compose build <service> 可以构建单独的镜像。
docker-compose build | Docker Documentation | https://docs.docker.com/compose/reference/build/
Compose Build Specification | https://docs.docker.com/reference/compose-file/build/
services:
frontend:
image: example/webapp
build: ./webapp
backend:
image: example/database
build:
context: backend
dockerfile: ../backend.Dockerfile
提供 Bash 补全 Bash Completion
Command-line completion | Docker Documentation
在 Bash 中,配置 Docker Compose 补全,可以简化输入工作、提高输入准确性;
1)确保已经安装 Bash Completion 功能;
2)安装自动补全:
mkdir -pv ~/.local/share/bash-completion/completions/
curl -L https://raw.githubusercontent.com/docker/compose/1.27.4/contrib/completion/bash/docker-compose \
-o ~/.local/share/bash-completion/completions/
构建
官方文档:https://docs.docker.com/compose/install/
插件形式,直接下载安装,推荐,……
https://docs.docker.com/compose/install/linux/#install-the-plugin-manually
DOCKER_CONFIG=${DOCKER_CONFIG:-$HOME/.docker}
mkdir -p $DOCKER_CONFIG/cli-plugins
curl -SL https://github.com/docker/compose/releases/download/v2.39.3/docker-compose-linux-x86_64 \
-o $DOCKER_CONFIG/cli-plugins/docker-compose
chmod +x $DOCKER_CONFIG/cli-plugins/docker-compose
docker compose version # Docker Compose version v2.39.3
# 09/21/2025 测试可用。
插件形式,通过仓库安装,APT,……
# --------------------------------------------------------- # Install the Compose plugin # --------------------------------------------------------- # https://docs.docker.com/compose/install/linux/ apt-get install docker-compose-plugin docker compose version
独立安装,其作为独立命令运行,废弃,……
文档:https://docs.docker.com/compose/install/standalone/
根据官方文档,… The Docker Compose standalone uses the -compose syntax instead of the current standard syntax compose. For example, you must type docker-compose up when using Docker Compose standalone, instead of docker compose up. Use it only for backward compatibility … 我们不再采用该方式。
独立安装,通过仓库安装,……
在 CentOS 7 及 Debian 10 中,仓库已经内置 Docker Compoes 包,可以直接安装:
yum install -y docker-compose.noarch apt-get install -y docker-compose
但是,从仓库安装的 Docker Compose 命令的版本较旧,经常会提示各种问题。因此,我们很少采用这种方法来安装;
安装 | x86 | arm | amd64
通过仓库安装(APT) | 安装 | arm64
# Ubuntu 18.04 ARM64 apt-get install docker-compose
通过源码编译 | 安装 | arm64
或使用 run.sh 脚本(类似的,也是在 Docker 中运行):
curl -L --fail https://raw.githubusercontent.com/linuxserver/docker-docker-compose/master/run.sh -o /usr/local/bin/docker-compose chmod +x /usr/local/bin/docker-compose
应用
场景 | 通过 Docker Compose 运行 | 使用 Docker Compose 部署应用
Web + Redis
首先,使用 YAML 文件定义一组要启动的容器,然后通过一个简单的命令来启动所有在配置文件中定义的服务;
(1)创建 docker-compose.yaml 文件。该文件的内容「类似于把 docker run 的参数写在文件中」;
web: image: jam01/compseapp command: python app.py ports: - "5000:5000" volumes: - .:/composeapp links: - redis redis: images: redis
也可以用于构建一个镜像:
web: build: /home/jams/composeapp
(2)运行 Compose 程序
docker-compose up # 使用 docker-compose up 来启动这些服务 # 必须在 docker-compose.yaml 目录中执行大多数的 Compose 命令; # 为了保证服务的唯一,Compose 将目录名字作为前缀,并使用数字作为后缀,组合起来作为服务名 # 使用-d 选项以守护进程的方式运行。不使用该选项是可以通过 Ctrl+C 来结束; docker-compose up -d # 和 docker 命令类似,Compose 也受 DOCKER_HOST 环境变量的影响;
传统 Java 应用
version: '3'
services:
web:
network_mode: host
volumes:
- ./:/data/
image: openjdk:8u242-jdk-buster
command: java -jar /data/application.jar
参考