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