簡介
[x] Compose 用來定義和運(yùn)行多個容器的 Docker 應(yīng)用程序喷橙。在 Compose 中你可以使用 YAML 文件來配置你的應(yīng)用服務(wù)况褪。然后刻炒,只需要一個簡單的命令堤框,就可以創(chuàng)建并啟動你配置的所有服務(wù)。
[x] 使用Compose 基本上分為三步:
1.Dockerfile 定義應(yīng)用的運(yùn)行環(huán)境 (前面已經(jīng)介紹過)靡挥。
2.docker-compose.yml 定義組成應(yīng)用的各服務(wù)
3.docker-compose up 啟動整個應(yīng)用
- [x] ==注意==:docker-compose是非集群多個應(yīng)用管理方式序矩,即只部署在一臺宿主機(jī)上。
- 部署在集群上跋破,請使用docker stack簸淀,使用類似。
compose安裝
安裝地址參考
https://docs.docker.com/compose/install/
https://docs.docker.com/compose/completion/#install-command-completion
1.下載compose
sudo curl -L "https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
2.設(shè)置可執(zhí)行權(quán)限
sudo chmod +x /usr/local/bin/docker-compose
3.安裝bash命令tab補(bǔ)充
- [x] 安裝腳本到/etc/bash_completion.d/
sudo curl -L https://raw.githubusercontent.com/docker/compose/1.22.0/contrib/completion/bash/docker-compose -o /etc/bash_completion.d/docker-compose
- [x] 某些系統(tǒng)無法自動補(bǔ)全毒返,需要做些額外處理
## 在~/.bashrc 末尾添加
. /etc/bash_completion.d/docker-compose
source ~/.bashrc
4.版本查看
$ docker-compose --version
docker-compose version 1.22.0, build 1719ceb
5.卸載
sudo rm /usr/local/bin/docker-compose
compose配置說明
[x] 官方文檔:
https://docs.docker.com/compose/compose-file/#build[x] 配置樣例:
version: '3'
services:
redis:
image: redis:alpine
deploy:
replicas: 6
update_config:
parallelism: 2
delay: 10s
restart_policy:
condition: on-failure
配置選項
bulid
- [x] 服務(wù)除了可以基于指定的鏡像租幕,還可以基于一份 Dockerfile,指定 Dockerfile 所在文件夾的路徑饿悬。
build: 可以是相對路徑令蛉,或者結(jié)合context的相對路徑聚霜。
build:
context: ../
dockerfile: path/of/Dockerfile
image
- [x] 指定鏡像
## service_name:服務(wù)名稱狡恬。
## image:鏡像名稱或鏡像 ID。
services:
service_name:
image: ubuntu:14.04
args
[x] 給容器傳遞參數(shù)
[x] Dockerfile 中添加參數(shù)
ARG buildno
ARG gitcommithash
RUN echo "Build number: $buildno"
RUN echo "Based on commit: $gitcommithash"
- [x] compose設(shè)置參數(shù)值
build:
context: .
args:
buildno: 1
gitcommithash: cdc3b19
build:
context: .
args:
- buildno=1
- gitcommithash=cdc3b19
- [x] 不設(shè)置值蝎宇,自動生成
args:
- buildno
- gitcommithash
command
- [x] 覆蓋默認(rèn)命令
command: bundle exec thin -p 3000
或者
command: ["bundle", "exec", "thin", "-p", "3000"]
depends_on
[x] 配置容器依賴弟劲。更多參考https://docs.docker.com/compose/compose-file/#depends_on
[x] 例如下面容器會先啟動 redis 和 db 兩個服務(wù),最后才啟動 web 服務(wù):
version: '3'
services:
web:
build: .
depends_on:
- db
- redis
redis:
image: redis
db:
image: postgres
- [x] 注意的是姥芥,默認(rèn)情況下使用 docker-compose up web 這樣的方式啟動 web 服務(wù)時兔乞,也會啟動 redis 和 db 兩個服務(wù),因為在配置文件中定義了依賴關(guān)系凉唐。
pid
- [x] 將PID模式設(shè)置為主機(jī)PID模式庸追,跟主機(jī)系統(tǒng)共享進(jìn)程命名空間。容器使用這個標(biāo)簽將能夠訪問和操縱其他容器和宿主機(jī)的名稱空間台囱。
pid: "host"
ports
- [x] 映射端口淡溯。
ports:
- "3000"
- "3000-3005"
- "8000:8000"
- "9090-9091:8080-8081"
- "49100:22"
- "127.0.0.1:8001:8001"
- "127.0.0.1:5000-5010:5000-5010"
- "6060:6060/udp"
- [x] 注意:當(dāng)使用HOST:CONTAINER格式來映射端口時,如果你使用的容器端口小于60你可能會得到錯誤得結(jié)果簿训,因為YAML將會解析xx:yy這種數(shù)字格式為60進(jìn)制咱娶。所以建議采用字符串格式。
extra_hosts
- [x] 添加主機(jī)名的標(biāo)簽强品,就是往/etc/hosts文件中添加一些記錄膘侮,與Docker client的--add-host類似:
extra_hosts:
- "somehost:162.242.195.82"
- "otherhost:50.31.209.229"
## 啟動之后查看容器內(nèi)部hosts:
162.242.195.82 somehost
50.31.209.229 otherhost
volumes
- [x] 掛載一個目錄或者一個已存在的數(shù)據(jù)卷容器,可以直接使用 [HOST:CONTAINER] 這樣的格式的榛,或者使用 [HOST:CONTAINER:ro] 這樣的格式琼了,后者對于容器來說,數(shù)據(jù)卷是只讀的夫晌,這樣可以有效保護(hù)宿主機(jī)的文件系統(tǒng)表伦。
Compose的數(shù)據(jù)卷指定路徑可以是相對路徑谦去,使用 . 或者 .. 來指定相對目錄。
數(shù)據(jù)卷的格式可以是下面多種形式:
volumes:
// 只是指定一個路徑蹦哼,Docker 會自動在創(chuàng)建一個數(shù)據(jù)卷(這個路徑是容器內(nèi)部的)鳄哭。
- /var/lib/mysql
// 使用絕對路徑掛載數(shù)據(jù)卷
- /opt/data:/var/lib/mysql
// 以 Compose 配置文件為中心的相對路徑作為數(shù)據(jù)卷掛載到容器。
- ./cache:/tmp/cache
// 使用用戶的相對路徑(~/ 表示的目錄是 /home/<用戶目錄>/ 或者 /root/)纲熏。
- ~/configs:/etc/configs/:ro
// 已經(jīng)存在的命名的數(shù)據(jù)卷妆丘。
- datavolume:/var/lib/mysql
- [x] 如果你不使用宿主機(jī)的路徑,你可以指定一個volume_driver局劲。
volume_driver: mydriver
compose常用命令
- [x] 命令描述參考
https://docs.docker.com/compose/reference/
命令提示
Commands:
build Build or rebuild services
bundle Generate a Docker bundle from the Compose file
config Validate and view the Compose file
create Create services
down Stop and remove containers, networks, images, and volumes
events Receive real time events from containers
exec Execute a command in a running container
help Get help on a command
images List images
kill Kill containers
logs View output from containers
pause Pause services
port Print the public port for a port binding
ps List containers
pull Pull service images
push Push service images
restart Restart services
rm Remove stopped containers
run Run a one-off command
scale Set number of containers for a service
start Start services
stop Stop services
top Display the running processes
unpause Unpause services
up Create and start containers
version Show the Docker-Compose version information
構(gòu)建啟動容器
docker-compose up -d nginx
登錄容器
docker-compose exec nginx bash
停止并且刪除所有容器勺拣、鏡像、掛載鱼填、鏡像
docker-compose down
顯示所有容器
docker-compose ps
重新啟動容器
docker-compose restart nginx
暫停容器
docker-compose pause nginx
恢復(fù)容器
docker-compose unpause nginx
刪除容器(刪除前必須關(guān)閉容器)
docker-compose rm nginx
停止容器
docker-compose stop nginx
啟動容器
docker-compose start nginx
不啟動依賴容器并且執(zhí)行后刪除容器
docker-compose run --no-deps --rm web python manage.py db upgrade
構(gòu)建鏡像
docker-compose build nginx
## 不帶緩存的構(gòu)建药有。
docker-compose build --no-cache nginx
查看日志
docker-compose logs nginx
## 查看實時日志
docker-compose logs -f nginx
驗證(docker-compose.yml)文件配置
docker-compose config -q
以json的形式輸出docker日志
docker-compose events --json nginx
stack常用命令
[root@node146 harbor]# docker stack --help
Usage: docker stack COMMAND
Manage Docker stacks
Options:
Commands:
deploy Deploy a new stack or update an existing stack
ls List stacks
ps List the tasks in the stack
rm Remove one or more stacks
services List the services in the stack
Run 'docker stack COMMAND --help' for more information on a command.
- [x] 樣例:
## 創(chuàng)建服務(wù)
docker stack deploy -c /usr/local/portainer/portainer-agent-stack.yml portainer
## 刪除服務(wù)
docker stack rm portainer