Docker Compose
Docker Compose是 docker 提供的一个命令行工具,用来定义和运行由多个容器组成的应用
使用 compose,可以通过 YAML 文件声明式的定义应用程序的各个服务,并由单个命令完成应用的创建和启动
Docker Compose 将所管理的容器分为三层:
工程(project)
服务(service)
容器(container)
Docker Compose 运行目录下的所有文件(docker-compose.yml)组成一个工程
一个工程包含多个服务,每个服务中定义了容器运行的镜像、参数、依赖,一个服务可包括多个容器实例
Docker Compose的配置文件是docker-compose.yml
1.查看版本
docker-compose -v
2.命令格式
docker-compose --help
docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...] 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) --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 deploy keys in v3 files to their non-Swarm equivalent Commands: build Build or rebuild services bundle Generate a Docker bundle from the Compose file 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 the Docker-Compose version information
3.使用docker-compose up构建、启动容器
格式
docker-compose up [options] [SERVICE...]
说明:
该命令可以自动完成包括构建镜像,(重新)创建服务,启动服务,并关联服务相关容器的一系列操作
docker-compose up启动的容器都在前台,控制台将会同时打印所有容器的输出信息,可以很方便进行调试
使用docker-compose up -d,
后台启动并运行容器
使用docker-compose pull [serviceName],
更新本地的镜像
eg:
安装 kafka
docker-compose.yml
version: '2' services: zookeeper: image: zookeeper:latest kafka: image: confluentinc/cp-kafka:latest depends_on: - zookeeper ports: - 9092:9092 environment: KAFKA_BROKER_ID: 1 KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181 KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:29092,PLAINTEXT_HOST://localhost:9092 KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
我是用的是Docker Toolbox,将该文件放到Docker Toolbox所在的目录下,eg:D:\Program Files\Docker Toolbox
运行
docker-compose up -d
docker-compose.yml 说明:
version:指定 docker-compose.yml 文件的写法格式
services:多个容器集合
image:指定服务所使用的镜像
environment:环境变量配置,可以用数组或字典两种方式
expose:暴露端口,只将端口暴露给连接的服务,而不暴露给主机
ports:对外暴露的端口定义,和 expose 对应
links:将指定容器连接到当前连接,可以设置别名,避免ip方式导致的容器重启动态改变的无法连接情况
depends_on:解决了容器的依赖、启动先后的问题
build:指定 Dockerfile 所在文件夹的路径
查看所有容器
docker-compose ps Name Command State Ports ----------------------------------------------------------------------------------------------------------- dockertoolbox_kafka_1 /etc/confluent/docker/run Up 0.0.0.0:9092->9092/tcp dockertoolbox_zookeeper_1 /docker-entrypoint.sh zkSe ... Up 2181/tcp, 2888/tcp, 3888/tcp, 8080/tcp
使用docker查看
docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 000f56dba572 confluentinc/cp-kafka:latest "/etc/confluent/dock…" 16 minutes ago Up 3 minutes 0.0.0.0:9092->9092/tcp dockertoolbox_kafka_1 05d07d85916b zookeeper:latest "/docker-entrypoint.…" 16 minutes ago Up 16 minutes 2181/tcp, 2888/tcp, 3888/tcp, 8080/tcp dockertoolbox_zookeeper_1
查看服务日志输出
docker-compose logs
绑定的公共端口
docker-compose port kafka 9092 0.0.0.0:9092
启动指定服务已存在的容器
docker-compose start kafka Starting kafka ... done docker-compose ps Name Command State Ports ----------------------------------------------------------------------------------------------------------- dockertoolbox_kafka_1 /etc/confluent/docker/run Up 0.0.0.0:9092->9092/tcp dockertoolbox_zookeeper_1 /docker-entrypoint.sh zkSe ... Up 2181/tcp, 2888/tcp, 3888/tcp, 8080/tcp
停止已运行的服务的容器
docker-compose stop kafka Stopping dockertoolbox_kafka_1 ... done docker-compose ps Name Command State Ports ------------------------------------------------------------------------------------------------------------- dockertoolbox_kafka_1 /etc/confluent/docker/run Exit 143 dockertoolbox_zookeeper_1 /docker-entrypoint.sh zkSe ... Up 2181/tcp, 2888/tcp, 3888/tcp, 8080/tcp