文章目录
-
- docker-compose
-
- 1、docker-compose字段
- 2、docker-compose模版命令
-
- [1、docker-compose up](#1、docker-compose up)
- [2、docker-compose down](#2、docker-compose down)
- [3、docker-compose rm](#3、docker-compose rm)
- [4、docker-compose logs](#4、docker-compose logs)
- 3、案例
docker-compose
- 为什么需要docker-compose
-
python编写的docker
-
一个项目需要很多个容器一起运行,才能完成
-
就会形成一个依赖,就是a容器启动后,b容器才能启动
-
这个时候就需要容器的编排了
-
以项目为角度,来进行解决
-
将一个项目中需要的容器进行了编排
-
多个容器相互配合来使用
-
定义这个项目需要很多的服务(容器)
-
1、docker-compose字段
bash
project # 定义一组关联的容器(服务)组成的一个业务单元
service # 一个项目的中一个容器称之为一个服务,服务就是容器
[root@docker ems]# cat docker-compose.yaml
version: "2.6.0" # docker-compose的版本
services: # 服务,多个容器
tomcat:
container_name: tomcat_01 # 容器自定义的名字
image: tomcat:8.0
ports:
- 8080:8080
tomcat01:
image: tomcat:8.0
ports:
- 8081:8080
redis:
image: redis:5.0.12
ports:
- 6379:6379
mysql:
image: mysql
ports:
- 3306:3306
environment: # 环境变量
- "MYSQL_ROOT_PASSWORD=root"
volumes:
# - /root/mysqldata1:/var/lib/mysql
- mysqldata:/var/lib/mysql
volumes: # 声明数据卷
mysqldata:
[root@docker docker]# docker-compose up
2、volumes字段
-
绝对路径
-
数据卷别名的话,需要声明
bash
volumes:
# - /root/mysqldata1:/var/lib/mysql
- mysqldata:/var/lib/mysql
volumes:
mysqldata: # 声明数据卷
# 查看卷
[root@docker ems]# docker volume ls
DRIVER VOLUME NAME
local 03be689ca6328c4c03ed05198d6145b7bb42919bf2c298c8e8369db44f778780
local 398a3dca9fde9bbbc0e61682b7b8f99a93bc19e744cda5d2786b434ff17c43ed
local 93026f95ece586b88d00607bfb12291e96b0ad73fb8d5b24836491a6def313d1
local bc976311848ef09c5438d7473ffd6e28aaac3a5a29421e0b1c9b5133e65a4b34
local ca2fd36d0d4bad7e6d783e423c801f590f7dcf319214962194a9f7679fb75624
local ems_mysqldata
local v1
3、build字段
-
基于当前目录构建镜像
-
在启动容器之前,先根据dockerfile构建镜像
4、depends_on
- 控制着容器之间启动先后顺序
bash
tomcat01:
image: tomcat:8.0
ports:
- 8081:8080
depends_on: # 先启动redis和mysql再来启动tomcat01
- redis
- mysql
2、docker-compose模版命令
1、docker-compose up
bash
Usage: docker compose up [SERVICE...]
Create and start containers
Options:
--abort-on-container-exit Stops all containers if any container was stopped. Incompatible with -d # 只要有容器停止就停止所有容器,与-d选项不兼容
--always-recreate-deps Recreate dependent containers. Incompatible with --no-recreate. # 总是重新创建所依赖的容器,与--no-recreate选项不兼容
--attach stringArray Attach to service output.
--attach-dependencies Attach to dependent containers.
--build Build images before starting containers. # 在启动容器之前构建镜像
-d, --detach Detached mode: Run containers in the background # 后台运行服务容器,输出新容器名称
--exit-code-from string Return the exit code of the selected service container. Implies --abort-on-container-exit # 为指定服务的容器返回退出码
--force-recreate Recreate containers even if their configuration and image havent changed. # 强制重新创建容器,即使配置和镜像没有变化
--no-build Dont build an image, even if its missing. # 不构建缺失的镜像
--no-color Produce monochrome output.
--no-deps Dont start linked services. # 不启动所连接的服务
--no-log-prefix Dont print prefix in logs.
--no-recreate If containers already exist, dont recreate them. Incompatible with --force-recreate.
--no-start Dont start the services after creating them.
--quiet-pull Pull without printing progress information. # 拉取镜像时不会输出进程信息
--remove-orphans Remove containers for services not defined in the Compose file. # 移除compose文件中未定义的服务容器
-V, --renew-anon-volumes Recreate anonymous volumes instead of retrieving data from the previous containers.
--scale scale Scale SERVICE to NUM instances. Overrides the scale setting in the Compose file if present. # 服务=数值,来设置服务的实例数量
-t, --timeout int Use this timeout in seconds for container shutdown when attached or when containers are already running. (default 10) # 设置停止连接的容器或已运行容器所等待的超时时间
--wait Wait for services to be running|healthy. Implies detached mode.
2、docker-compose down
bash
Usage: docker compose down
Stop and remove containers, networks
Options:
--remove-orphans Remove containers for services not defined in the Compose file. # 删除不在compose文件中定义的服务容器
--rmi string Remove images used by services. "local" remove only images that dont have a custom tag ("local"|"all") # 删除服务使用的镜像
-t, --timeout int Specify a shutdown timeout in seconds (default 10) # 设置停止容器的超时时间(默认10秒)
-v, --volumes volumes Remove named volumes declared in the volumes section of the Compose file and anonymous volumes attached to containers. # 删除由服务定义创建的数据卷
# 常规清理(清理容器和网络)
docker-compose down
# 带数据卷清理
docker-compose down -v
# 删除镜像
docker-compse down --rmi all
# 超时设置(设置容器停止超时时间为30秒)
docker-compose down --timeout 30
3、docker-compose rm
bash
Usage: docker compose rm [SERVICE...]
Removes stopped service containers
By default, anonymous volumes attached to containers will not be removed. You
can override this with -v. To list all volumes, use "docker volume ls".
Any data which is not in a volume will be lost.
Options:
-f, --force Dont ask to confirm removal # 删除不问询确认
-s, --stop Stop the containers, if required, before removing # 删除前先停止容器
-v, --volumes Remove any anonymous volumes attached to containers # 删除容器附加的卷
4、docker-compose logs
查看容器的信息
3、案例
bash
version: "v2.6.0"
services:
tomcat01:
image: mysql
ports:
- 8080:8080 # 宿主机端口:容器端口
environment:
- "MYSQL_ROOT_PASSWORD=root"
volumes:
- /root/mysqldata1:/var/lib/mysql # 宿主日目录:容器目录
- mysqldata:/var/lib/mysql11
networks:
- b1
tomcat02:
image: awesome
build: ./webapp # 构建上下文
depends_on:
- tomcat02
volumes:
mysqldata: # 创建一个匿名卷
networks:
b1: # 会创建一个网络出来的