基础命令
- 启动
docker-compose up
- 只加载指定容器
docker-compose up --build xxxx
- 后台启动
docker-compose up -d
- 停止
docker-compose stop
- 停止并删除容器
docker-compose down
常用的配置参数说明
参数名 | 类型 | 说明 |
---|---|---|
services | 字典 | key为服务名称 |
networks | 字典 | key为网络名称 |
services
服务名
参数名 | 类型 | 说明 |
---|---|---|
image | str | 镜像名称,如果有此镜像,则直接使用,否则如果有build,则构建,没有则拉取 |
ports | list | 暴露的端口,例如"0.0.0.0:30000:6379"
|
networks | list | 网络名 |
restart | str | 重启方式,no 不自动重启 always 为自动启动 on-failure 非正常退出时重启 |
logging | 字典 | 日志配置 |
logging.driver | 日志方案 | 默认为json-file
|
logging.options | 对象 | 针对不同的日志方案有不同的取值,对于json-file ,可以取{"max-size": "50m","max-file": "10" } |
container_name | str | 容器名 |
volumes | list | 挂载路径,如"$HOME/DataSnaptoBook:/var/lib/mysql" |
environment | 字典 | 环境变量,如MYSQL_ROOT_PASSWORD: xxxx
|
build | 字典 | 镜像构建参数 |
build.context | str | 构建路径 |
build.dockerfile | str | dockerfile文件名 |
env_file | list | 环境变量配置文件路径,如.env ,内部格式为NAME=test
|
networks
网络名
参数名 | 类型 | 说明 |
---|---|---|
driver | str | 网络名称 |
示例配置文件
version: '3'
services:
app-redis:
image: "redis:latest"
ports:
- "0.0.0.0:30000:6379"
networks:
- app-network
restart: always
logging:
driver: "json-file"
options:
max-size: "50m"
max-file: "10"
container_name: app-redis
app-database:
container_name: app-database
image: "mysql:8.0.20"
ports:
- "0.0.0.0:30001:3306"
volumes:
- "$HOME/Dataapp:/var/lib/mysql"
environment:
MYSQL_ROOT_PASSWORD: SDJHGFHJS
MYSQL_DATABASE: app_db
MYSQL_USER: app-dbu
MYSQL_PASSWORD: SGDHJFGS
networks:
- app-network
restart: always
logging:
driver: "json-file"
options:
max-size: "50m"
max-file: "10"
app-server:
container_name: app-server
build:
context: .
dockerfile: Dockerfile
image: app:2407261
ports:
- "0.0.0.0:30002:80"
networks:
- app-network
restart: always
logging:
driver: "json-file"
options:
max-size: "50m"
max-file: "10"
environment:
RUN_TYPE: API
ENV_TYPE: produce
volumes:
- "$HOME/appServers:/home/appServers"
env_file:
- "$HOME/Sensitive/envs.conf"
app-celery:
container_name: app-celery
build:
context: .
dockerfile: Dockerfile
image: app:2407261
networks:
- app-network
restart: always
logging:
driver: "json-file"
options:
max-size: "50m"
max-file: "10"
environment:
RUN_TYPE: CELERY
ENV_TYPE: produce
volumes:
- "$HOME/appServers:/home/appServers"
env_file:
- "$HOME/Sensitive/envs.conf"
networks:
app-network:
driver: bridge