前提
- dockers version:1.13或者更高(17.03.1-ce)
- 安裝
Docker Compose
理解service
在一個分布式應用中,不同的部分都被稱為service。
services實際上是containers in production。一個service啟動一個image,它規(guī)定了image的運行方式(使用哪些端口,需要多少個container的復制集才能滿足service等)。
docker-compose.yml文件可以很簡單的定義、運行、規(guī)劃services。
docker-compose.yml
可以把該文件保存在任何位置。確保image已經上傳。
version: "3"
services:
web:
# replace username/repo:tag with your name and image details
image: username/repository:tag
deploy:
replicas: 5
resources:
limits:
cpus: "0.1"
memory: 50M
restart_policy:
condition: on-failure
ports:
- "80:80"
networks:
- webnet
networks:
webnet:
- 從
registry拉取image - 運行5個
image的實例作為服務,稱為web,限制每個使用最多10%的CPU和50M的內存 - 如果停止運行立即重新啟動
container - 映射本地80端口到
web的80端口 - 指導
web的containers通過負載均衡網絡webnet共享80端口。(在內部,containerswill publish toweb’s port 80 at an ephemeral port) - 使用默認設置定義
webnet網絡
運行新的負債均衡的app
docker swarm init
# app name: getstartedlab
docker stack deploy -c docker-compose.yml getstartedlab
# See a list of the five containers you just launched
docker stack ps getstartedlab
Scale(規(guī)劃) the app
可以修改replicas,保存,重新運行docker stack deploy:
docker stack deploy -c docker-compose.yml getstartedlab
Docker will do an in-place update, no need to tear the stack down first or kill any containers.
摧毀app和swarm
docker stack rm getstartedlab
This removes the app, but our one-node swarm is still up and running (as shown by docker node ls). Take down the swarm with docker swarm leave --force.
docker stack ls # List all running applications on this Docker host
docker stack deploy -c <composefile> <appname> # Run the specified Compose file
docker stack services <appname> # List the services associated with an app
docker stack ps <appname> # List the running containers associated with an app
docker stack rm <appname> # Tear down an application