這次要介紹的是Docker Compose
的部分,為什這會需要Docker Compose,當想要佈署的Container數量太多時,在管理各服務之間關聯時,就會很不方便和麻煩。
Docker Compose是一個可以讓你透過指令控制多個Container,主要也是用YAML
格式來去描述和定義各服務的關係,白話來說就是管理container的文件檔。
linux安裝docker-compose
sudo curl -L "https://github.com/docker/compose/releases/download/1.27.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
安裝完成
➜ ~ docker-compose --version
docker-compose version 1.27.2, build 18f557f9
不同系統如何安裝docker-compose,請參考安裝連結
version: '3'
services:
httpserver:
image: httpserver:v1.0.1
ports:
- "8080:8080"
depends_on:
- grpcserver
links:
- grpcserver
restart: always
container_name: hs
grpcserver:
image: grpcserver:v1.0.0
ports:
- 8081:8081
restart: always
container_name: gs
--link
是相同效果,透過標籤解決容器連線問題使用docker-compose指令需要和docker-compose.yml
同一層目錄。
-d
表示背景執行docker-compose up -d
docker-compose ps
結果
Name Command State Ports
----------------------------------------------------
gs ./grpcserver Up 0.0.0.0:8081->8081/tcp
hs ./httpserver Up 0.0.0.0:8080->8080/tcp
docker-compose logs
結果
Attaching to hs, gs
gs | {"level":"info","msg":"grpc server start","time":"2020-09-19T07:44:09Z"}
gs | {"level":"info","msg":"user add ok: a00017","time":"2020-09-19T07:44:34Z"}
gs | {"level":"info","msg":"user login ok: a00017","time":"2020-09-19T07:44:45Z"}
gs | {"level":"warning","msg":"user login password error: a00017","time":"2020-09-19T07:44:48Z"}
gs | {"level":"warning","msg":"user login password error: a00017","time":"2020-09-19T07:45:53Z"}
gs | {"level":"warning","msg":"user add failed: a00017","time":"2020-09-19T07:46:03Z"}
hs | {"level":"info","msg":"http server start","time":"2020-09-19T07:44:10Z"}
hs | {"err":"unexpected end of JSON input","level":"error","msg":"Error Log","time":"2020-09-19T07:46:45Z","trace":"userLogin_2"}
docker-compose stop
docker-compose rm
//container會被停止,然後被刪除,建立的network也會被刪除
docker-compose down
結果
Stopping hs ... done
Stopping gs ... done
Removing hs ... done
Removing gs ... done
Removing network docker-compose_default