qdqht00 2020-03-26
项目地址: https://github.com/docker/compose
Compose 是用来定义和运行一个或多个容器应用的工具
。
Compose 可以简化
容器镜像的建立及容器的运行。
Compose 使用python语言开发,非常适合在单机环境
里部署一个或多个容器,并自动把多个容器互相关联
起来。
Compose 还是Docker三剑客
之一。
通过docker-api来与docker-server进行交互的。
服务(service):一个应用的容器,实际上可以包括若干运行相同镜像的容器实例。
项目(project):由一组关联的应用容器组成的一个完整业务单元,在docker-compose.yml中定义。
# 安装依赖 yum install -y yum-utils device-mapper-persistent-data lvm2 # yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo # 指定docker社区版的镜像源 yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo yum-config-manager --enable docker-ce-edge yum-config-manager --enable docker-ce-test # 安装docker社区版 yum install -y docker-ce # 启动docker systemctl start docker # 查看docker版本 docker --version # 开机启动 chkconfig docker on
# 官方下载地址 curl -L https://github.com/docker/compose/releases/download/1.23.0-rc2/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose # 国内下载地址 curl -L https://get.daocloud.io/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose # 如果没有curl命令则可以安装curl: yum instal -y curl chmod +x /usr/local/bin/docker-compose docker-compose version
# 环境变量可以用来配置 Compose 的行为,以DOCKER_开头的变量和用来配置 Docker 命令行客户端的使用一样。 COMPOSE_PROJECT_NAME COMPOSE_FILE DOCKER_HOST DOCKER_TLS_VERIFY DOCKER_CERT_PATH
整理的不全,详细请看 官方文档
# 例子(不能正常运行,仅供参考) version: ‘2‘ networks: helloworld: driver: bridge services: web: image: hello-world restart: always net: "bridge" ports: - "80:80" depends_on: - redis networks: - helloworld dns: - 8.8.8.8 - 9.9.9.9 links: - web extra_hosts: - "redis:192.168.1.100" redis: image: redis container_name: redis commond: redis-server.sh ./redis.conf volumes: - ~/data:/data:rw expose: - "6379" networks: - helloworld
services: web: image: hello-world
# 也可以是相对路径,只要上下文确定就可以读取到Dockerfile。 build: /path/to/build/dir
# 设定上下文根目录,然后以该目录为准指定Dockerfile。 build: ./dir
# build都是一个目录,如果要指定Dockerfile文件需要在build标签的子级标签中使用dockerfile标签指定。 # 如果同时指定image和build两个标签,那么Compose会构建镜像并且把镜像命名为image值指定的名字。 build: context: ../ dockerfile: path/of/Dockerfile
build: context: ./dir
build: context: . dockerfile: Dockerfile-alternate
command: bundle exec thin -p 3000
container_name: app
version: ‘2‘ services: web: build: . depends_on: - db - redis redis: image: redis db: image: postgres
pid: "host"
ports: - "3000" - "8000:8000" - "49100:22" - "127.0.0.1:8001:8001" # 当使用HOST:CONTAINER格式来映射端口时,如果使用的容器端口小于60可能会得到错误得结果,因为YAML将会解析xx:yy这种数字格式为60进制。所以建议采用字符串格式。
extra_hosts: - "somehost:162.242.195.82" - "otherhost:50.31.209.229" 启动后查看容器内部hosts:
volumes: # 只是指定一个路径,Docker 会自动在创建一个数据卷(这个路径是容器内部的)。 - /var/lib/mysql # 使用绝对路径挂载数据卷 - /opt/data:/var/lib/mysql # 以 Compose 配置文件为中心的相对路径作为数据卷挂载到容器。 - ./cache:/tmp/cache # 使用用户的相对路径(~/ 表示的目录是 /home/<用户目录>/ 或者 /root/)。 - ~/configs:/etc/configs/:ro # 已经存在的命名的数据卷。 - datavolume:/var/lib/mysql # 如果不使用宿主机的路径,可以指定一个volume_driver。 # volume_driver: mydriver
volumes_from: - service_name - container_name
dns:8.8.8.8 dns: - 8.8.8.8 - 9.9.9.9
expose: - "3000" - "8000"
links: - db - db:database - redis
net: "bridge" net: "none" net: "host"
env_file: .env env_file: - ./common.env - ./apps/web.env - /opt/runtime_opts.env
extends: file: common.yml service: webapp
restart: no restart: always restart: on-failure restart: unless-stopped
docker-compose --h
Define and run multi-container applications with Docker. Usage: docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...] docker-compose -h|--help Options: -f, --file FILE Specify an alternate compose file(指定yml文件的名字) (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 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