這章節將會介紹一些docker-compose較為進階的功能,希望有特殊需求的小夥伴可以參考看看。
可以透過environment進行單值的環境變數輸入
environment:
- REDIS_HOST=redis
- REDIS_PORT=6379
可以透過env_file來設定整個檔案的環境變數
env_file:
- prd.env
能夠掛載特定的資料夾或是已存在的資料卷容器
volumes:
- ./app/:/usr/src/app/app
此外,也能夠加上ro之類的參數,設定為唯讀
volumes:
- ./app/:/usr/src/app/app:ro
能夠從其他containers或volumes掛載特定資料,同樣也能加上讀寫的參數。
volumes_from:
- service_name:ro
- container:container_name:rw
$ docker-compose -f <docker-compose.yaml> config
使用config command會讓你顯示目前docker-compose所解析到的配置。
$ docker-compose -f docker-compose-prd.yaml config
networks:
ironman-net:
driver: bridge
services:
ironman:
container_name: ironman
depends_on:
- redis
environment:
REDIS_HOST: redis
REDIS_PORT: '6379'
image: ghjjhg567/ironman:latest
logging:
driver: json-file
options:
max-file: '3'
max-size: 1M
networks:
ironman-net: null
restart: always
volumes:
- /Users/shihjie/PycharmProjects/ironman_2020/app:/usr/src/app/app:rw
nginx:
container_name: nginx
image: nginx
networks:
ironman-net: null
ports:
- 80:80/tcp
restart: always
volumes:
- /Users/shihjie/PycharmProjects/ironman_2020/nginx/nginx.conf:/etc/nginx/nginx.conf:rw
- /Users/shihjie/PycharmProjects/ironman_2020/nginx/default.conf:/etc/nginx/conf.d/default.conf:rw
redis:
container_name: redis
image: redis:4.0
networks:
ironman-net: null
ports:
- 6379:6379/tcp
restart: always
version: '3.0'
$ docker-compose -f <base.yaml> -f <extension.yaml>
還記得上個章節所說到的如何讓docker-compose因應不同環境啟動嗎?
更好的做法是有個base template,並且讓環境相關配置寫在extension當中。
base.yaml(將原先yaml檔當中的env_file配置給拿掉當作base yaml)
version: '3'
services:
nginx:
container_name: nginx
image: nginx
ports:
- "80:80"
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
restart: always
networks:
- ironman-net
redis:
container_name: redis
image: redis:4.0
ports:
- "6379:6379"
restart: always
networks:
- ironman-net
ironman:
container_name: ironman
image: ghjjhg567/ironman:latest
volumes:
- ./app/:/usr/src/app/app
restart: always
networks:
- ironman-net
networks:
ironman-net:
driver: bridge
之後依序建立stg與prd環境的yaml檔
stg.yaml
version: '3'
services:
ironman:
env_file:
- stg.env
prd.yaml
version: '3'
services:
ironman:
env_file:
- prd.env
使用上面教過的config我們可以知道docker-compose已經吃到了環境設定檔了。
$ docker-compose -f base.yaml -f stg.yaml config
networks:
ironman-net:
driver: bridge
services:
ironman:
container_name: ironman
environment:
REDIS_HOST: redis
REDIS_PORT: '6379'
image: ghjjhg567/ironman:latest
networks:
ironman-net: {}
restart: always
volumes:
- /Users/shihjie/PycharmProjects/ironman_2020/app:/usr/src/app/app:rw
nginx:
container_name: nginx
image: nginx
networks:
ironman-net: {}
ports:
- 80:80/tcp
restart: always
volumes:
- /Users/shihjie/PycharmProjects/ironman_2020/nginx/default.conf:/etc/nginx/conf.d/default.conf:rw
- /Users/shihjie/PycharmProjects/ironman_2020/nginx/nginx.conf:/etc/nginx/nginx.conf:rw
redis:
container_name: redis
image: redis:4.0
networks:
ironman-net: {}
ports:
- 6379:6379/tcp
restart: always
version: '3.0'
$ docker-compose -f up —build
如果在yaml當中同時有著image與build的話,則會在輸入—build的argument同時,去依照設定的Dockerfile在啟動前重新build image。
version: '3'
services:
nginx:
container_name: nginx
image: nginx
ports:
- "80:80"
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
restart: always
networks:
- ironman-net
redis:
container_name: redis
image: redis:4.0
ports:
- "6379:6379"
restart: always
networks:
- ironman-net
ironman:
container_name: ironman
image: ghjjhg567/ironman:latest
build:
context: .
dockerfile: Dockerfile
volumes:
- ./app/:/usr/src/app/app
restart: always
networks:
- ironman-net
networks:
ironman-net:
driver: bridge
能夠使compose內的container互相連結
links:
- nginx
- redis
能夠使compose外的containers也能與compose內的containers進行連結,但前提是還是要有一格以上的外部container連到compose內的network
external_links:
- postgres
- mongodb
depend on 可以讓你的containers有順序性的create,舉例來說,如果我想要讓redis container先create,這樣web service的cache才能將data丟進去,這時我就能讓web service depend on redis
depends_on:
- redis
通過logging為compose配置logs的儲存格式與容量大小。
version: '3'
services:
nginx:
container_name: nginx
image: nginx
ports:
- "80:80"
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
restart: always
networks:
- ironman-net
redis:
container_name: redis
image: redis:4.0
ports:
- "6379:6379"
restart: always
networks:
- ironman-net
ironman:
container_name: ironman
image: ghjjhg567/ironman:latest
volumes:
- ./app/:/usr/src/app/app
env_file:
- prd.env
restart: always
networks:
- ironman-net
networks:
ironman-net:
driver: bridge
運用單值標籤,進行資源的配置與限制。這邊就不對其值一一解說。
cpu_shares: 73
cpu_quota: 50000
cpuset: 0,1
mem_limit: 1000000000
memswap_limit: 2000000000
privileged: true
restart: always
read_only: true
shm_size: 64M
stdin_open: true
tty: true
類似於Dockerfile中的entrypoint,而在yaml中設定entrypoint還能夠覆蓋掉dockerfile原先的entrypoint。
entrypoint: docker-entrypoint.sh
此外 compose的entrypoint還有另種書寫格式
entrypoint:
- /usr/local/bin/uwsgi
- --ini
- /usr/src/app/uwsgi.ini
能夠在Container啟動時進行健康檢查,檢查像是depend_on的服務是否能通、或是data initalize有沒有成功....等。此外,Health Check在每個Container只能出現一次。
那我簡單介紹一下healthCheck嚇得key-value吧:
version: '3'
services:
nginx:
container_name: nginx
image: nginx
ports:
- "80:80"
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
restart: always
networks:
- ironman-net
redis:
container_name: redis
image: redis:4.0
ports:
- "6379:6379"
restart: always
networks:
- ironman-net
ironman:
container_name: ironman
image: ghjjhg567/ironman:latest
volumes:
- ./app/:/usr/src/app/app
env_file:
- prd.env
restart: always
networks:
- ironman-net
depends_on:
- nginx
- redis
healthcheck:
test: ["CMD-SHELL", "curl", "-X", "GET", "http://nginx"]
interval: 10s
timeout: 5s
retries: 5
networks:
ironman-net:
driver: bridge
以上面為例,健康檢查的commmand為
$ curl -X GET http://nginx
本篇章所有程式碼將放在下面的github project當中的branch day-13
https://github.com/Neskem/ironman_2020/tree/day-13
這章節主要帶給大家較為進階與不為人知的compose功能以及功能的實作方式,那docker-compose的章節也就在這告一個段落了。下個章節開始會進入到Kubernetes的世界,敬請期待!!
Docker-Compose獸發出了銀色光芒並開始放空,看來是要進化了。