說明:本人來源于Docker官方文檔的個人翻譯,如有疏漏嘁扼,還請見諒信粮。
原文地址:https://docs.docker.com/compose/
一、Docker-Compose的安裝與基礎(chǔ)命令
操作系統(tǒng)版本:CentOS7.6
1.使用root用戶安裝依賴
yum -y install epel-release
yum -y install python-pip python-devel libffi-devel openssl-devel libc-devel gcc make
2.安裝docker compose(使用非root用戶趁啸,這里使用的是gavin用戶)
sudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
3.授予執(zhí)行權(quán)限
sudo chmod +x /usr/local/bin/docker-compose
4.檢查是否安裝成功
docker-compose --version
5.啟動docker-compose
docker-compose up
6.后臺運行docker-compose
docker-compose up -d
7.查看后臺運行的進程
docker-compose ps
8.停止后臺運行的進程
docker-compose stop
9.移除容器并刪除數(shù)據(jù)卷
docker-compose down --volumes
二强缘、Docker堆棧與分布式應(yīng)用捆綁
【Produce bundle】
1.produce bundle
docker-compose bundle
【Create a stack from bundle】
2.通過docker deploy命令創(chuàng)建一個堆棧
docker deploy --help
3.部署之前創(chuàng)建一個堆棧
docker deploy vossibility-stack
4.驗證服務(wù)是否正確創(chuàng)建
docker service ls
【Manage stacks】
5.通過docker stack命令管理堆棧
docker stack --help
【Bundle file format】
一個捆綁有兩個頂級字段:version和services.
三、通過Swarm使用Compose
$ eval "$(docker-machine env --swarm <name of swarm master machine>)"
$ docker-compose up
【局限性】
1.創(chuàng)建鏡像
If you want to use Compose to scale the service in question to multiple nodes, build the image, push it to a registry such as Docker Hub, and reference it from docker-compose.yml:
$ docker build -t myusername/web .
$ docker push myusername/web
$ cat docker-compose.yml
web:
image: myusername/web
$ docker-compose up -d
$ docker-compose scale web=3
2.多重依賴
如果某項服務(wù)具有強制進行聯(lián)合調(diào)度的類型的多個依賴項(請參閱下面的自動調(diào)度)不傅,則Swarm可能會在不同的節(jié)點上調(diào)度依賴項旅掂,從而使依賴服務(wù)無法進行調(diào)度。 例如访娶,在這里foo需要與bar和baz共同調(diào)度:
version: "2"
services:
foo:
image: foo
volumes_from: ["bar"]
network_mode: "service:baz"
bar:
image: bar
baz:
image: baz
The problem is that Swarm might first schedule bar and baz on different nodes (since they’re not dependent on one another), making it impossible to pick an appropriate node for foo.
To work around this, use manual scheduling to ensure that all three services end up on the same node:
version: "2"
services:
foo:
image: foo
volumes_from: ["bar"]
network_mode: "service:baz"
environment:
- "constraint:node==node-1"
bar:
image: bar
environment:
- "constraint:node==node-1"
baz:
image: baz
environment:
- "constraint:node==node-1"
3.主機端口和重建容器
Specify a named volume, and use a volume driver which is capable of mounting the volume into the container regardless of what node it’s scheduled on.
Compose does not give Swarm any specific scheduling instructions if a service uses only named volumes.
version: "2"
services:
web:
build: .
ports:
- "80:8000"
volumes:
- web-logs:/var/log/web
volumes:
web-logs:
driver: custom-volume-driver
Remove the old container before creating the new one. You lose any data in the volume.
$ docker-compose stop web
$ docker-compose rm -f web
$ docker-compose up web
【容器編排】
1.自動編排
network_mode: "service:..." and network_mode: "container:..." (and net: "container:..." in the version 1 file format).
volumes_from
links
2.手動編排
四商虐、Environment variables in Compose
1.在Composer files中替換環(huán)境變量
web:
image: "webapp:${TAG}"
2.在容器中設(shè)置環(huán)境變量
docker run -e VARIABLE=VALUE ...:
web:
environment:
- DEBUG=1
3.將環(huán)境變量傳遞給容器
docker run -e VARIABLE ...:
web:
environment:
- DEBUG
4.env_file配置選項
docker run --env-file=FILE ...:
web:
env_file:
- web-variables.env
5.設(shè)置docker-compose run的環(huán)境變量
docker-compose run -e DEBUG=1 web python console.py
docker-compose run -e DEBUG web python console.py
6. .env文件
$ cat .env
TAG=v1.5
$ cat docker-compose.yml
version: '3'
services:
web:
image: "webapp:${TAG}"
$ docker-compose config
version: '3'
services:
web:
image: 'webapp:v1.5'
$ export TAG=v2.0
$ docker-compose config
version: '3'
services:
web:
image: 'webapp:v2.0'
$ cat ./Docker/api/api.env
NODE_ENV=test
$ cat docker-compose.yml
version: '3'
services:
api:
image: 'node:6-alpine'
env_file:
- ./Docker/api/api.env
environment:
- NODE_ENV=production
$ docker-compose exec api node
> process.env.NODE_ENV
'production'
五、Extend services in Compose
在文件和工程間共享Compose配置
Compose支持兩種方法來共享通用配置:
1.通過使用多個Compose文件擴展整個Compose文件
2.使用擴展字段擴展單個服務(wù)(適用于2.1或更高版本的Compose文件)
【Multiple Compose files】
1.Understanding multiple Compose files
2.Example use case
<DIFFERENT ENVIRONMENTS>
docker-compose.yml
web:
image: example/my_web_app:latest
depends_on:
- db
- cache
db:
image: postgres:latest
cache:
image: redis:latest
docker-compose.override.yml
web:
build: .
volumes:
- '.:/code'
ports:
- 8883:80
environment:
DEBUG: 'true'
db:
command: '-d'
ports:
- 5432:5432
cache:
ports:
- 6379:6379
docker-compose.prod.yml
web:
ports:
- 80:80
environment:
PRODUCTION: 'true'
cache:
environment:
TTL: '500'
在生產(chǎn)環(huán)境部署這個docker-compose:
docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d
<ADMINISTRATIVE TASKS>
docker-compose.yml
web:
image: example/my_web_app:latest
depends_on:
- db
db:
image: postgres:latest
docker-composer.admin.yml
dbadmin:
build: database_admin/
depends_on:
- db
啟動一個正常的docker-compose文件和一個數(shù)據(jù)庫備份的docker-compose文件
docker-compose -f docker-compose.yml -f docker-compose.admin.yml run dbadmin db-backup
【Extending services】
請記住崖疤,使用extends不能在服務(wù)之間共享volumes_from和depends_on秘车。
1.Understand the extends configuration
docker-compose.yml
web:
extends:
file: common-services.yml
service: webapp
common-services.yml
webapp:
build: .
ports:
- "8000:8000"
volumes:
- "/data"
web:
extends:
file: common-services.yml
service: webapp
environment:
- DEBUG=1
cpu_shares: 5
important_web:
extends: web
cpu_shares: 10
web:
extends:
file: common-services.yml
service: webapp
environment:
- DEBUG=1
cpu_shares: 5
depends_on:
- db
db:
image: postgres
2.Example use case
common.yml
app:
build: .
environment:
CONFIG_FILE_PATH: /code/config
API_KEY: xxxyyy
cpu_shares: 5
docker-compose.yml
webapp:
extends:
file: common.yml
service: app
command: /code/run_web_app
ports:
- 8080:8080
depends_on:
- queue
- db
queue_worker:
extends:
file: common.yml
service: app
command: /code/run_worker
depends_on:
- queue
【Adding and overriding configuration】
# original service
command: python app.py
# local service
command: python otherapp.py
# result
command: python otherapp.py
For the multi-value options ports, expose, external_links, dns, dns_search, and tmpfs, Compose concatenates both sets of values:
# original service
expose:
- "3000"
# local service
expose:
- "4000"
- "5000"
# result
expose:
- "3000"
- "4000"
- "5000"
In the case of environment, labels, volumes, and devices, Compose “merges” entries together with locally-defined values taking precedence. For environment and labels, the environment variable or label name determines which value is used:
# original service
environment:
- FOO=original
- BAR=original
# local service
environment:
- BAR=local
- BAZ=local
# result
environment:
- FOO=original
- BAR=local
- BAZ=local
Entries for volumes and devices are merged using the mount path in the container:
# original service
volumes:
- ./original:/foo
- ./original:/bar
# local service
volumes:
- ./local:/bar
- ./local:/baz
# result
volumes:
- ./original:/foo
- ./local:/bar
- ./local:/baz
六、Networking in Compose
【Update containers】
version: "3"
services:
web:
build: .
links:
- "db:database"
db:
image: postgres
【Specify custom networks】
version: "3"
services:
proxy:
build: ./proxy
networks:
- frontend
app:
build: ./app
networks:
- frontend
- backend
db:
image: postgres
networks:
- backend
networks:
frontend:
# Use a custom driver
driver: custom-driver-1
backend:
# Use a custom driver which takes special options
driver: custom-driver-2
driver_opts:
foo: "1"
bar: "2"
【Configure the default network】
version: "3"
services:
web:
build: .
ports:
- "8000:8000"
db:
image: postgres
networks:
default:
# Use a custom driver
driver: custom-driver-1
【Use a pre-existing network】
networks:
default:
external:
name: my-pre-existing-network
六劫哼、問題解決
1.啟動docker-compose報錯
[gavin@gavin composetest]$ docker-compose up
ERROR: Couldn't connect to Docker daemon at http+docker://localhost - is it running?
If it's at a non-standard location, specify the URL with the DOCKER_HOST environment variable.
【解決方法】
(1)使用gavin用戶新建docker組叮趴,并將當(dāng)前用戶加入docker組
sudo groupadd docker
sudo gpasswd -a ${USER} docker
(2)使用root用戶重啟docker服務(wù)
systemctl restart docker.service
(3)使用gavin用戶啟動docker-compose
cd composetest
docker-compose up