docker compose 主要是对一些列的容器程序集中的操作管理
确认一下compose 已安装,没有自行安装
docker compose version
Docker Compose version v2.10.2
命令差不多,build可以使用远程镜像也可以用本地的dockerfile
本地项目目录
├── cmd
│ ├── admin
│ │ ├── Dockerfile
│ │ ├── admin
│ │ └── config.toml
│ ├── binlog
│ │ ├── Dockerfile
│ │ ├── binlog
│ │ └── config.toml
│ ├── job
│ │ ├── Dockerfile
│ │ ├── config.toml
│ │ └── job
│ ├── rest
│ │ ├── Dockerfile
│ │ ├── config.toml
│ │ └── rest
│ └── worker
│ ├── Dockerfile
│ ├── config.toml
│ └── worker
└── docker-compose.yml
docker-compose.yml
version: "3.0"
services:
redis:
container_name: redis-compose
image: redis:latest
expose:
- 6379
networks:
- default
mysql:
container_name: mysql-compose
image: mysql:latest
# 外部映射端口
ports:
- 33060:3306
environment:
MYSQL_ROOT_PASSWORD: "123456"
# 运行命令
# command:
networks:
- default
admin:
container_name: admin
build:
context: ./cmd/admin
dockerfile: ./Dockerfile
# mac m1 指定运行平台
platform: "linux/amd64"
restart: always
ports:
- 81:8001
networks:
- default
links:
- redis
- mysql
# 在指定容器启动完成后启动
depends_on:
- redis
- mysql
rest:
container_name: rest
build:
context: ./cmd/rest
dockerfile: ./Dockerfile
platform: "linux/amd64"
restart: always
ports:
- 80:8000
networks:
- default
links:
- redis
- mysql
depends_on:
- redis
- mysql
binlog:
container_name: binlog
build:
context: ./cmd/binlog
dockerfile: ./Dockerfile
platform: "linux/amd64"
restart: always
links:
- redis
- mysql
depends_on:
- redis
- mysql
job:
container_name: job
build:
context: ./cmd/job
dockerfile: ./Dockerfile
platform: "linux/amd64"
restart: always
links:
- redis
- mysql
depends_on:
- redis
- mysql
worker:
container_name: worker
build:
context: ./cmd/worker
dockerfile: ./Dockerfile
platform: "linux/amd64"
restart: always
links:
- redis
- mysql
depends_on:
- redis
- mysql
networks:
frontend:
backend:
开始构建
docker compose up -d
从上面的yml中可以了解到,compose构建容器可以通过镜像构建,也可以通过compose构建,compose构建是独立的网络,不能把compose以外的容器link起来。特别要注意的是因为我的本地是mac的M1芯片,系统架构的不同会导致容器无法启动成功,报错
exec /cmd/binlog: exec format error
yml中已经注释。其他标签基本与dockerfile 中大同小异。