root@mysql-2:~# cat docker-composer.yaml
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個
image
指定使用的鏡像 同Dockerfile裏面的FROM 如果此鏡象不存在 會自己嘗試拉去此鏡象
image: hanye131:mysql57
image: hanye131:nginx172
build
服務除了可以基於指定的鏡像,還可以指定目錄下的Dockerfile構建。可以絕對路徑和相對路徑
相對路徑
build: ./datadir
絕對路徑
build: /path/to/datadir
如果想要指定目錄下的Dockerfile:context
build:
context: /data/soft/redis
dockerfile: Dockerfile
environment 指定環境變量
# 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 指定構建後的容器名字
container_name: redis #指定容器名稱 即構建後容器對完展示的名字
container_name: nginx
depends_on 指定構建容器 啓動順序 這樣就不會應爲存在依賴而報錯出現問題
例如: 先啓動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
ports 映射端口
使用主機端口:容器端口格式 或者 只是指定容器的端口,宿主機會隨機映射端口。
注意:當使用主機端口:宿主機端口格式來映射端口時,如果你使用的容器端口小於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 這個標籤解決的是容器連接問題
links: #,這個標籤解決的是容器連接問題,與Docker client的--link一樣效果,會連接到其它服務中的容器。
- nginx
- mysql
extra_hosts: #寫入數據到/ect/hosts文件
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和dns_search
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 指定掛在目錄
volumes:
- "/etc/redis.conf:/etc/redis.conf:rw"
- "/etc/localtime:/etc/localtime:ro"
- "/usr/local/redis/var:/data/redis:rw"
volumes_from 指定掛在容器的目錄
volumes_from: #調用其他容器的掛在盤
- nginx
sysctls 指定開啓內核參數 直接修改/etc/sysctl.conf
sysctls:
net.core.somaxconn: '1024'
fs.file-max: "1000000"
ulimits 設置容器打開文件線程數量
ulimits: #設置線程和打開文件數量
nproc: 65535
nofile:
soft: 20000
hard: 40000
cap_add 增加容器可以使用那些功能
cap_add: # 指定容器具有那些內核功能
- ALL
cap_dorp 刪除容器可以使用的內核功能
cap_drop: #指定去掉那些內核能力
- NET_ADMIN
restart 容器出現問題是否重啓
no是默認的重啓策略,在任何情況下都不會重啓容器。 指定爲always時,容器總是重新啓動。 如果退出代碼指示出現故障錯誤,則on-failure將重新啓動容器。
restart: “no”
restart: always
restart: on-failure
restart: unless-stopped
hostname 指定容器名稱
hostname: redis 指定容器啓動後的名字
healthcheck: #健康檢查
healthcheck: #健康檢查
test: ["CMD","curl","http://localhost"]
interval: 1m30s # 每次檢查之間的間隔時間
timeout: 10s # 運行命令的超時時間
retries: 3 # 重試次數
start_period: 40s # v3.4 以上新增的選項, 定義容器啓動時間間隔
disable: true # true 或 false, 表示是否禁用健康狀態檢測和 test: NONE 相同
logging: #設置log服務
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 擴展原來的yaml文件 會覆蓋原有配置
extends:
file: nginx.yml
service: nginx
deploy 設置資源限制
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 的內存空間
network 將容器加入指定網絡
networks # 將容器加入指定網絡 (等同於 docker network connect 的作用), networks 可以位於 compose 文件頂級鍵和 services 鍵的二級鍵
aliases # 同一網絡上的容器可以使用服務名稱或別名連接到其中一個服務的容器
ipv4_address # IP V4 格式
ipv6_address # IP V6 格式