version: "3" services: redis: # depends_on: 启动容器顺序 # - nginx # - php-fpm # - mysql #image: hanye131/redis-4.0.11 build: context: /data/soft/redis dockerfile: Dockerfile restart: always hostname: redis container_name: redis #指定容器名称 ports: - "6800:6379" volumes: - "/etc/redis.conf:/etc/redis.conf:rw" - "/etc/localtime:/etc/localtime:ro" - "/usr/local/redis/var:/data/redis:rw" #volumes_from: #调用其余容器的挂在盘 # - nginx sysctls: net.core.somaxconn: '1024' extra_hosts: #写入数据到/ect/hosts文件 - "hanye.com:192.168.1.39" - "redis.conf:192.168.1.39" links: #,这个标签解决的是容器链接问题,与Docker client的--link同样效果,会链接到其它服务中的容器。 - nginx - mysql dns_search: - 192.168.1.1 - 114.114.114.114 cap_add: # 指定容器具备那些内核功能 - ALL cap_drop: #指定去掉那些内核能力 - NET_ADMIN healthcheck: #健康检查 test: ["CMD","curl","http://localhost"] timeout: 10s retries: 3 interval: 60s ulimits: #设置线程和打开文件数量 nproc: 65535 nofile: soft: 20000 hard: 40000 logging: #设置log文件大小 driver: "json-file" options: max-size: "20m" #文件大小20m max-file: "10" #文件保留10个
指定使用的镜像 同Dockerfile里面的FROM 若是此镜象不存在 会本身尝试拉去此镜象 image: hanye131:mysql57 image: hanye131:nginx172
服务除了能够基于指定的镜像,还能够指定目录下的Dockerfile构建。能够绝对路径和相对路径 相对路径 build: ./datadir 绝对路径 build: /path/to/datadir 若是想要指定目录下的Dockerfile:context build: context: /data/soft/redis dockerfile: Dockerfile
# environment: # - REDIS_CONF=on # - REQUIREPASSWD=hanye131 # - MAXCLIENTS_NUM=6000 # - MAXMEMORY_SIZE=4096 这样在redis.conf里面能够调用此变量: maxclients ${MAXCLIENTS_NUM} maxmemory ${MAXMEMORY_SIZE}M requirepass "${REQUIREPASSWD}"
container_name: redis #指定容器名称 即构建后容器对完展现的名字 container_name: nginx
例如: 先启动redis 在启动mysql 最后启动nginx # depends_on: 启动容器顺序 # - mysql # - nginx 写法: version: "3" services: redis: image: redis:latest depends_on: - mysql - nginx container_name: redis mysql: image: mysql:5.7 container_name: mysql57 nginx: image: nginx:1.17.2 container_name: nginx1172
使用主机端口:容器端口格式 或者 只是指定容器的端口,宿主机会随机映射端口。 注意:当使用主机端口:宿主机端口格式来映射端口时,若是你使用的容器端口小于60你可能会获得错误得结果,由于YAML将会解析xx:yy这种数字格式为60进制,因此建议使用字符串格式 ports: - "8080:80" - "3309:3306" - "192.168.1.39:6379:6379" - "3000-3005" #暴漏容器3000-3005端口 v3.2新增参数 ports: - target: 80 # 容器端口 published: 8080 # 宿主机端口 protocol: tcp # 协议类型 mode: host # host 在每一个节点上发布主机端口, ingress 对于群模式端口进行负载均衡
links: #,这个标签解决的是容器链接问题,与Docker client的--link同样效果,会链接到其它服务中的容器。 - nginx - mysql 实例: version: "3.2" services: redis: image: fangxin:redis4.0.11 hostname: redis container_name: link-redis4011 privileged: true ports: - "7000:6379" cap_add: - ALL volumes: - "/etc/redis.conf:/etc/redis/redis.conf:rw" - "/etc/localtime:/etc/localtime:rw" - "/mnt/redis:/data/redis:rw" - "/data/soft/redis/startredis.sh:/startredis.sh:rw" - "/data/soft/redis/supervisord.conf:/etc/supervisord.conf:rw" restart: always links: - nginx:nginx - php-fpm:php-fpm sysctls: net.core.somaxconn: '1024' nginx: image: hanye:nginx172 container_name: link-ginx hostname: hanye_nginx cap_add: - ALL ports: - "8082:80" - "8443:443" volumes: - "/data/soft/nginx_compose/html:/usr/local/nginx/html:rw" - "/data/soft/nginx_compose/conf:/usr/local/nginx/conf:rw" php-fpm: image: bitnami/php-fpm container_name: link-php-fpm ports: - "9000:9000" volumes: - "/data/soft/nginx_compose/html:/usr/local/nginx/html:rw" - "/data/soft/nginx_compose/conf:/usr/local/nginx/conf:rw" hostname: hanye_php-fpm
例如: redis: links: - mysql:link-mysql - php-fpm:link-php-fpm mysql: links: - redis:link-redis - php-fpm:link-php-fpm
报错以下:php
external_links: - redis-cluster4 - db:mysql
extra_hosts: #写入数据到/ect/hosts文件 - "hanye.com:192.168.1.39" - "redis.conf:192.168.1.39" 即在/etc/hosts里面添加: 192.168.1.39 hanye.com 192.168.1.39 redis.conf
dns: 8.8.8.8 dns: - 8.8.8.8 - 114.114.114 dns_search: #会在/etc/resolv.conf 添加解析域 - hanye.com - 192.168.1.1
volumes: - "/etc/redis.conf:/etc/redis.conf:rw" - "/etc/localtime:/etc/localtime:ro" - "/usr/local/redis/var:/data/redis:rw"
volumes_from: #调用其余容器的挂在盘 - nginx
sysctls: net.core.somaxconn: '1024' fs.file-max: "1000000"
ulimits: #设置线程和打开文件数量 nproc: 65535 nofile: soft: 20000 hard: 40000
cap_add: # 指定容器具备那些内核功能 - ALL
cap_drop: #指定去掉那些内核能力 - NET_ADMIN
no是默认的重启策略,在任何状况下都不会重启容器。 指定为always时,容器老是从新启动。 若是退出代码指示出现故障错误,则on-failure将从新启动容器。 restart: “no” restart: always restart: on-failure restart: unless-stopped
hostname: redis 指定容器启动后的名字
healthcheck: #健康检查 test: ["CMD","curl","http://localhost"] interval: 1m30s # 每次检查之间的间隔时间 timeout: 10s # 运行命令的超时时间 retries: 3 # 重试次数 start_period: 40s # v3.4 以上新增的选项, 定义容器启动时间间隔 disable: true # true 或 false, 表示是否禁用健康状态检测和 test: NONE 相同
driver: "json-file" driver: "syslog" driver: "none" 只有驱动程序 json-file 和 journald 驱动程序能够直接从 docker-compose up 和 docker-compose logs 获取日志。使用任何其余方式不会显示任何日志。 driver: "syslog" options: syslog-address: "tcp://192.168.0.39" 日志发送到39的syslog服务,须要开启39的syslog对外访问 存放到文件 logging: #设置log文件大小 driver: "json-file" options: max-size: "20m" #单个文件大小20m max-file: "10" #文件保留10个
extends: file: nginx.yml service: nginx
version: "3.7" services: redis: image: redis:alpine deploy: resources: limits: # 设置容器的资源限制 cpus: '0.50' # 设置该容器最多只能使用 50% 的 CPU memory: 50M #设置该容器最多只能使用 50M 的内存空间 reservations: # 设置为容器预留的系统资源(随时可用) cpus: '0.25' # 为该容器保留 20% 的 CPU memory: 20M # 为该容器保留 20M 的内存空间
networks # 将容器加入指定网络 (等同于 docker network connect 的做用), networks 能够位于 compose 文件顶级键和 services 键的二级键 aliases # 同一网络上的容器可使用服务名称或别名链接到其中一个服务的容器 ipv4_address # IP V4 格式 ipv6_address # IP V6 格式 例子: 指定容器ip地址 version: "3.6" services: nginx: image: nginx:latest container_name: nginx1 privileged: true restart: always ports: - "9090:80" networks: hanye: ipv4_address: 172.3.0.10 redis: image: redis:latest container_name: redis1 hostname: redis1 privileged: true restart: always ulimits: nproc: 65535 nofile: soft: 30000 hard: 30000 ports: - "6300:6379" networks: hanye: ipv4_address: 172.3.0.11 networks: hanye: ipam: driver: default config: - subnet: 172.3.0.0/24