1. 程式人生 > 其它 >Docker-Docker Swarm

Docker-Docker Swarm

1.1 購買伺服器

購買3臺伺服器,所有主機都安裝dockerdocker-compose

1.2 Swarm叢集產生

compose、machine 和 swarm 是docker 原生提供的三大編排工具。簡稱docker三劍客。

1、伺服器硬體要求

硬體資源 最小配置 推薦配置
CPU 1 CPU 2CPU
記憶體 1GB 2~4GB
硬碟 20 GB 40GB

2、節點資訊

主機名 IP地址 具體說明
docker01 8.134.122.252 swarm-manager節點
docker02 8.134.114.8 swarm-work01節點
docker03 8.134.123.77 swarm-work02節點

3、當前伺服器應用狀態

4、容器化部署存在問題

  • 怎麼保證資料完整性,怎麼去管很多微服容器,怎麼去更新容器而不影響客戶的業務?
  • 如果容器down掉了。怎麼自動恢復?

=》解決以上問題,docker-swarm橫空出世!!!。==

1.3 安裝docker-swarm

1、基本概述

Docker Swarm 和 Docker Compose 一樣,都是 Docker 官方容器編排專案,但不同的是,Docker Compose 是一個在單個伺服器或主機上建立多個容器的工具,可以將組成某個應該的多個docker容器編排在一起,同時管理。而 Docker Swarm 則可以在多個伺服器或主機上建立容器叢集服務,其主要作用是把若干臺Docker主機抽象為一個整體,並且通過一個入口(docker stack)統一管理這些Docker主機上的各種Docker資源。

stack 是構成特定環境中的 service 集合, 它是自動部署多個相互關聯的服務的簡便方法,而無需單獨定義每個服務。stack file 是一種 yaml 格式的檔案,類似於 docker-compose.yml 檔案,它定義了一個或多個服務,並定義了服務的環境變數、部署標籤、容器數量以及相關的環境特定配置等。

2、官方文件:https://docs.docker.com/engine/swarm/

3、Docker Swarm由兩部分組成

  • Docker叢集:將一個或多個Docker節點組織起來,使用者就能以叢集的方式進行管理。
  • 應用編排:有一套API用來部署和管理容器。

1.4 管理節點

1、初始化第一個管理節點

advertise-addr和listen-addr這兩個引數註解:

  • 前者用來指定其他節點連線m0時的地址。
  • 後者指定承載swarm流量的IP和埠。
  • 建立管理節點,會在本地新建docker網路。

2、manager節點說明

MANAGER STATUS列

  • Leader 意味著該節點是使得群的所有群管理和編排決策的主要管理器節點。
  • Reachable 意味著節點是管理者節點正在參與Raft共識。如果管理節點不可用,則該節點有資格被選為新的管理節點。
  • Unavailable 意味著節點是不能與其他管理器通訊的管理器。如果管理器節點不可用,應該將新的管理器節點加入群集,或者將工作器節點升級為管理器。

AVAILABILITY列

  • Active 意味著排程程式可以將任務分配給節點。
  • Pause 意味著排程程式不會將新任務分配給節點,但現有任務仍在執行。
  • Drain 意味著排程程式不會向節點分配新任務,排程程式關閉所有現有任務並在可用節點上排程它們。

docker01伺服器

注意:IP地址選擇的是阿里雲伺服器的私服!!!

[root@guardwhy01 ~]# docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
44443742f9b7   bridge    bridge    local
5f09756231c5   host      host      local
9d85ae8a4d3d   none      null      local
[root@guardwhy01 ~]# docker swarm init --advertise-addr 172.21.251.249:2377 --listen-addr 172.21.251.249:2377
Swarm initialized: current node (ujswj6ys93qhtji45oxszv4zr) is now a manager.

To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-2js986t4enexu18fpn5dnticbmx0df9qni5nsfsn6ngykl931s-1tattfxbp5ahqz7wk1ha7ycfh 172.21.251.249:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

[root@guardwhy01 ~]# docker network ls
NETWORK ID     NAME              DRIVER    SCOPE
44443742f9b7   bridge            bridge    local
c372c3473958   docker_gwbridge   bridge    local
5f09756231c5   host              host      local
jh4ohdal24uo   ingress           overlay   swarm
9d85ae8a4d3d   none              null      local
[root@guardwhy01 ~]# docker node ls
ID                            HOSTNAME     STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
ujswj6ys93qhtji45oxszv4zr *   guardwhy01   Ready     Active         Leader           20.10.7
[root@guardwhy01 ~]# 

3、加入新的節點

Docker Swarm的新節點加入是從管理節點(docker01伺服器)獲取一長串命令,稱為join token,任何加入叢集只要執行join token即可加入Swarm叢集。

docker02伺服器

[root@guardwhy02 ~]# docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
475d6f0e7633   bridge    bridge    local
4212954a0f4f   host      host      local
7fb6fab37bee   none      null      local
[root@guardwhy02 ~]# docker swarm join --token SWMTKN-1-2js986t4enexu18fpn5dnticbmx0df9qni5nsfsn6ngykl931s-1tattfxbp5ahqz7wk1ha7ycfh 172.21.251.249:2377
This node joined a swarm as a worker.
[root@guardwhy02 ~]# 

如果有新的work節點需要加入,在m0執行命令docker swarm join-token worker即可得到管理work節點的join token

docker01伺服器

[root@guardwhy01 ~]# docker swarm join-token worker
To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-2js986t4enexu18fpn5dnticbmx0df9qni5nsfsn6ngykl931s-1tattfxbp5ahqz7wk1ha7ycfh 172.21.251.249:2377

[root@guardwhy01 ~]# docker node ls
ID                            HOSTNAME     STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
ujswj6ys93qhtji45oxszv4zr *   guardwhy01   Ready     Active         Leader           20.10.7
sq9x6zrg7u2izo4tzmrffbyz1     guardwhy02   Ready     Active                          20.10.7
[root@guardwhy01 ~]# 

如果有新的管理節點需要加入,在m0執行命令docker swarm join-token manager即可得到管理manager節點的join token

docker01伺服器

[root@guardwhy01 ~]# docker swarm join-token manager
To add a manager to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-2js986t4enexu18fpn5dnticbmx0df9qni5nsfsn6ngykl931s-aw4p1x4xwul0iw1ma2aid49cg 172.21.251.249:2377
[root@guardwhy01 ~]#

docker03伺服器

[root@guardwhy03 ~]# docker swarm join --token SWMTKN-1-2js986t4enexu18fpn5dnticbmx0df9qni5nsfsn6ngykl931s-aw4p1x4xwul0iw1ma2aid49cg 172.21.251.249:2377
This node joined a swarm as a manager.
[root@guardwhy03 ~]# 

在manager檢視所有節點。

docker01伺服器

[root@guardwhy01 ~]# docker node ls
ID                            HOSTNAME     STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
ujswj6ys93qhtji45oxszv4zr *   guardwhy01   Ready     Active         Leader           20.10.7
sq9x6zrg7u2izo4tzmrffbyz1     guardwhy02   Ready     Active                          20.10.7
25wan9kvxtew5m48hvkng8jr5     guardwhy03   Ready     Active         Reachable        20.10.7
[root@guardwhy01 ~]# 

1.5 節點相關操作

1、驗證節點

master節點

[root@guardwhy01 ~]# docker info


work節點

[root@guardwhy02 ~]# docker info


2、節點許可權提升/降低

manager節點降低為worker節點,在manager節點執行如下命令

[root@guardwhy01 ~]# docker node ls
ID                            HOSTNAME     STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
ujswj6ys93qhtji45oxszv4zr *   guardwhy01   Ready     Active         Reachable        20.10.7
sq9x6zrg7u2izo4tzmrffbyz1     guardwhy02   Ready     Active                          20.10.7
25wan9kvxtew5m48hvkng8jr5     guardwhy03   Ready     Active         Leader           20.10.7
[root@guardwhy01 ~]# docker node demote guardwhy03
Manager guardwhy03 demoted in the swarm.
[root@guardwhy01 ~]# docker node ls
ID                            HOSTNAME     STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
ujswj6ys93qhtji45oxszv4zr *   guardwhy01   Ready     Active         Leader           20.10.7
sq9x6zrg7u2izo4tzmrffbyz1     guardwhy02   Ready     Active                          20.10.7
25wan9kvxtew5m48hvkng8jr5     guardwhy03   Unknown   Active                          20.10.7
[root@guardwhy01 ~]# 

將worker節點提升為manager節點,在manager節點執行如下命令

[root@guardwhy01 ~]# docker node ls
ID                            HOSTNAME     STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
ujswj6ys93qhtji45oxszv4zr *   guardwhy01   Ready     Active         Leader           20.10.7
sq9x6zrg7u2izo4tzmrffbyz1     guardwhy02   Ready     Active                          20.10.7
25wan9kvxtew5m48hvkng8jr5     guardwhy03   Ready     Active                          20.10.7
[root@guardwhy01 ~]# docker node promote guardwhy02 
Node guardwhy02 promoted to a manager in the swarm.
[root@guardwhy01 ~]# docker node ls
ID                            HOSTNAME     STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
ujswj6ys93qhtji45oxszv4zr *   guardwhy01   Ready     Active         Leader           20.10.7
sq9x6zrg7u2izo4tzmrffbyz1     guardwhy02   Ready     Active         Reachable        20.10.7
25wan9kvxtew5m48hvkng8jr5     guardwhy03   Ready     Active                          20.10.7
[root@guardwhy01 ~]# 

3、節點脫離叢集

docker03伺服器

docker03伺服器中,使用以下命令

[root@guardwhy03 ~]# docker swarm leave
Node left the swarm.
[root@guardwhy03 ~]# 

docker01伺服器

docker01伺服器中,使用以下命令

[root@guardwhy01 ~]# docker node ls
ID                            HOSTNAME     STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
ujswj6ys93qhtji45oxszv4zr *   guardwhy01   Ready     Active         Reachable        20.10.7
sq9x6zrg7u2izo4tzmrffbyz1     guardwhy02   Ready     Active         Leader           20.10.7
25wan9kvxtew5m48hvkng8jr5     guardwhy03   Down      Active                          20.10.7
[root@guardwhy01 ~]# 

4、刪除脫離叢集的節點

刪除節點命令: docker node rm 節點名稱|節點ID

[root@guardwhy01 ~]# docker node ls
ID                            HOSTNAME     STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
ujswj6ys93qhtji45oxszv4zr *   guardwhy01   Ready     Active         Reachable        20.10.7
sq9x6zrg7u2izo4tzmrffbyz1     guardwhy02   Ready     Active         Leader           20.10.7
25wan9kvxtew5m48hvkng8jr5     guardwhy03   Down      Active                          20.10.7
[root@guardwhy01 ~]# docker node rm guardwhy03
guardwhy03
[root@guardwhy01 ~]# docker node ls
ID                            HOSTNAME     STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
ujswj6ys93qhtji45oxszv4zr *   guardwhy01   Ready     Active         Reachable        20.10.7
sq9x6zrg7u2izo4tzmrffbyz1     guardwhy02   Ready     Active         Leader           20.10.7
[root@guardwhy01 ~]# 

manager中重新新增worker節點

[root@guardwhy01 ~]# docker swarm join-token worker
To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-2js986t4enexu18fpn5dnticbmx0df9qni5nsfsn6ngykl931s-1tattfxbp5ahqz7wk1ha7ycfh 172.21.251.249:2377

[root@guardwhy01 ~]#
[root@guardwhy03 ~]# docker swarm join --token SWMTKN-1-2js986t4enexu18fpn5dnticbmx0df9qni5nsfsn6ngykl931s-1tattfxbp5ahqz7wk1ha7ycfh 172.21.251.249:2377
This node joined a swarm as a worker.
[root@guardwhy03 ~]# 

新增成功!!!

5、安裝圖形介面

docker官方地址: https://hub.docker.com/r/dockersamples/visualizer

拉取基礎映象

注意:要在manager伺服器建立容器!!!

docker pull dockersamples/visualizer:latest

執行圖形介面映象

docker run -itd --name visualizer -p 8091:8080 \
-e HOST=8.134.122.252 \
-e PORT=8080 \
-v /var/run/docker.sock:/var/run/docker.sock dockersamples/visualizer:latest

檢視執行結果

開啟瀏覽器,點選連結: http://8.134.122.252:8091/

6、Swarm命令小結

常用命令 具體作用
docker swarm init 初始化一個 swarm 群集
docker swarm join 加入群集作為節點或管理器
docker swarm join-token 管理用於加入群集的令牌
docker swarm leave 離開 swarm 群集
docker swarm unlock 解鎖 swarm 群集
docker swarm unlock-key 管理解鎖鑰匙
docker swarm update 更新 swarm 群集

7、node命令小結

常用命令 具體作用
docker node demote 從 swarm 群集管理器中降級一個或多個節點
docker node inspect 顯示一個或多個節點的詳細資訊
docker node ls 列出 swarm 群集中的節點
docker node promote 將一個或多個節點推入到群集管理器中
docker node ps 列出在一個或多個節點上執行的任務,預設為當前節點。
docker node rm 從 swarm 群集刪除一個或多個節點
docker node update 更新一個節點

1.6 Docker service

1、docker service命令小結

常用命令 具體作用
docker service create 建立服務
docker service inspect 顯示一個或多個服務的詳細資訊
docker service logs 獲取服務的日誌
docker service ls 列出服務
docker service rm 刪除一個或多個服務
docker service scale 設定服務的例項數量
docker service update 更新服務
docker service rollback 恢復服務至update之前的配置

2、需求實現


2、叢集所有的節點都下載nginx基礎映象

docker pull nginx:1.18.0-alpine
docker pull nginx:1.19.3-alpine

3、在docker01伺服器(manager節點)中建立overlay網路。

[root@guardwhy01 data]# docker network ls
NETWORK ID     NAME              DRIVER    SCOPE
0eb90cd90de7   bridge            bridge    local
c372c3473958   docker_gwbridge   bridge    local
5f09756231c5   host              host      local
jh4ohdal24uo   ingress           overlay   swarm
9d85ae8a4d3d   none              null      local
[root@guardwhy01 data]# docker network create -d overlay nginx-net
yoyo8isxn654fmkeygfsi3nlw
[root@guardwhy01 data]# 

4、建立5個nginx容器的叢集

[root@guardwhy01 data]# docker service create --name nginx --network nginx-net -p 80:80 --replicas 5 nginx:1.18.0-alpine 
u0etuzildau4h931nybmjbgtx
overall progress: 5 out of 5 tasks 
1/5: running   [==================================================>] 
2/5: running   [==================================================>] 
3/5: running   [==================================================>] 
4/5: running   [==================================================>] 
5/5: running   [==================================================>] 
verify: Service converged 
[root@guardwhy01 data]# docker node ls
ID                            HOSTNAME     STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
ujswj6ys93qhtji45oxszv4zr *   guardwhy01   Ready     Active         Leader           20.10.7
sq9x6zrg7u2izo4tzmrffbyz1     guardwhy02   Ready     Active                          20.10.7
pu1ykn1mnx0ptbiw7bjwltrq6     guardwhy03   Ready     Active                          20.10.7
[root@guardwhy01 data]# docker ps -a
CONTAINER ID   IMAGE                             COMMAND                  CREATED         STATUS                            PORTS                                       NAMES
f42ef1d0c1e5   nginx:1.18.0-alpine               "/docker-entrypoint.…"   3 minutes ago   Up 3 minutes                      80/tcp                                      nginx.4.qc3yxnwk3zpeurfg3cj6kfy3t
d4c2fa9dbab0   nginx:1.18.0-alpine               "/docker-entrypoint.…"   3 minutes ago   Up 3 minutes                      80/tcp                                      nginx.2.d89kask69omz214ivu14fduo4
3d648fc53506   dockersamples/visualizer:latest   "/sbin/tini -- node …"   6 hours ago     Up 4 seconds (health: starting)   0.0.0.0:8091->8080/tcp, :::8091->8080/tcp   visualizer
[root@guardwhy01 data]# 

訪問連結: http://8.134.122.252:8091/

5、命令檢視服務情況。

[root@guardwhy01 data]# docker service ls
ID             NAME      MODE         REPLICAS   IMAGE                 PORTS
u0etuzildau4   nginx     replicated   5/5        nginx:1.18.0-alpine   *:80->80/tcp
[root@guardwhy01 data]#

注意: docker service ls命令只能在manager節點使用,在worker節點無法檢視

6、檢視伺服器容器情況

[root@guardwhy02 home]# docker ps
CONTAINER ID   IMAGE                 COMMAND                  CREATED          STATUS          PORTS     NAMES
a77fba3447f0   nginx:1.18.0-alpine   "/docker-entrypoint.…"   14 minutes ago   Up 14 minutes   80/tcp    nginx.3.haruvjy8v68otk1cxdkc4rs90
e13fb38a0602   nginx:1.18.0-alpine   "/docker-entrypoint.…"   14 minutes ago   Up 14 minutes   80/tcp    nginx.1.hwedkhibak90mzvh7jt9oaaau
[root@guardwhy02 home]# 

注意: 在manager或者worker節點都可以執行docker ps命令。

7、manager節點只用於管理叢集,不部署服務。

[root@guardwhy01 data]# docker node update --availability drain guardwhy01
guardwhy01
[root@guardwhy01 data]# docker ps
CONTAINER ID   IMAGE                             COMMAND                  CREATED       STATUS                    PORTS                                       NAMES
3d648fc53506   dockersamples/visualizer:latest   "/sbin/tini -- node …"   6 hours ago   Up 14 minutes (healthy)   0.0.0.0:8091->8080/tcp, :::8091->8080/tcp   visualizer
[root@guardwhy01 data]# 

訪問連結: http://8.134.122.252:8091/

8、將服務縮減為3個容器

[root@guardwhy01 data]# docker service scale nginx=3
nginx scaled to 3
overall progress: 3 out of 3 tasks 
1/3: running   [==================================================>] 
2/3: running   [==================================================>] 
3/3:   
verify: Service converged 
[root@guardwhy01 data]# 

訪問連結: http://8.134.122.252:8091/

9、升級nginx版本

進入其中一個容器檢視nginx的版本資訊。

[root@guardwhy02 home]# docker images
REPOSITORY   TAG             IMAGE ID       CREATED        SIZE
nginx        1.18.0-alpine   684dbf9f01f3   2 months ago   21.9MB
nginx        1.19.3-alpine   4efb29ff172a   8 months ago   21.8MB
[root@guardwhy02 home]# docker ps -a
CONTAINER ID   IMAGE                 COMMAND                  CREATED          STATUS          PORTS     NAMES
a77fba3447f0   nginx:1.18.0-alpine   "/docker-entrypoint.…"   29 minutes ago   Up 29 minutes   80/tcp  nginx.3.haruvjy8v68otk1cxdkc4rs90
e13fb38a0602   nginx:1.18.0-alpine   "/docker-entrypoint.…"   29 minutes ago   Up 29 minutes   80/tcp    nginx.1.hwedkhibak90mzvh7jt9oaaau
[root@guardwhy02 home]# docker exec -it e13fb38a0602 sh
/ # nginx -v
nginx version: nginx/1.18.0
nginx version: nginx/1.18.0
/ # exit
[root@guardwhy02 home]# 

更新映象

注意:更新映象只能在manager節點使用。

[root@guardwhy01 data]# docker service update --image nginx:1.19.3-alpine nginx
nginx
overall progress: 3 out of 3 tasks 
1/3: running   [==================================================>] 
2/3: running   [==================================================>] 
3/3: running   [==================================================>] 
verify: Service converged 
[root@guardwhy01 data]# 

更新映象以後,檢視結果!!!

新增或者更新一個對外埠

[root@guardwhy01 data]# docker service update --publish-add 8087:80 nginx
nginx
overall progress: 5 out of 5 tasks 
1/5: running   [==================================================>] 
2/5: running   [==================================================>] 
3/5: running   [==================================================>] 
4/5: running   [==================================================>] 
5/5: running   [==================================================>] 
verify: Service converged 
[root@guardwhy01 data]# 

訪問連結: http://8.134.122.252:8087/

10、刪除服務

[root@guardwhy01 data]# docker network ls
NETWORK ID     NAME              DRIVER    SCOPE
0eb90cd90de7   bridge            bridge    local
c372c3473958   docker_gwbridge   bridge    local
5f09756231c5   host              host      local
jh4ohdal24uo   ingress           overlay   swarm
yoyo8isxn654   nginx-net         overlay   swarm
9d85ae8a4d3d   none              null      local
[root@guardwhy01 data]# docker service rm nginx
nginx
[root@guardwhy01 data]# docker network rm nginx-net
nginx-net
[root@guardwhy01 data]# docker service ls
ID        NAME      MODE      REPLICAS   IMAGE     PORTS
[root@guardwhy01 data]# 

1.7 Docker stack

1、docker stack命令小結

常用命令 具體作用
docker stack deploy 部署新的堆疊或更新現有堆疊
docker stack ls 列出現有堆疊
docker stack ps 列出堆疊中的任務
docker stack rm 刪除一個或多個堆疊
docker stack services 列出堆疊中的服務

2、在manager節點中建立docker-compose.yml檔案

version: '3'
services:
  nginx-web:
    image: nginx:1.19.3-alpine
    container_name: nginx
    networks:
     - nginx-net
    restart: always
    ports:
      - 80:80
    deploy:
      replicas: 5

networks:
  nginx-net:
    driver: overlay

3、執行nginx

[root@guardwhy01 data]# docker stack deploy nginx-stack -c docker-compose.yml
Ignoring unsupported options: restart

Ignoring deprecated options:

container_name: Setting the container name is not supported.

Creating service nginx-stack_nginx-web
[root@guardwhy01 data]# docker service ls
ID             NAME                    MODE         REPLICAS   IMAGE                 PORTS
i8nabdgwztf0   nginx-stack_nginx-web   replicated   5/5        nginx:1.19.3-alpine   *:80->80/tcp
[root@guardwhy01 data]# 

訪問連結: http://8.134.122.252:8091/

4、檢視NAME中的服務名為:nginx-stack_nginx-web所有容器

[root@guardwhy01 data]# docker service ps nginx-stack_nginx-web
ID             NAME                      IMAGE                 NODE         DESIRED STATE   CURRENT STATE            ERROR     PORTS
j91t0720gitm   nginx-stack_nginx-web.1   nginx:1.19.3-alpine   guardwhy02   Running         Running 11 minutes ago             
5qooi8nxt1xb   nginx-stack_nginx-web.2   nginx:1.19.3-alpine   guardwhy02   Running         Running 11 minutes ago             
r8cdsibuwsec   nginx-stack_nginx-web.3   nginx:1.19.3-alpine   guardwhy03   Running         Running 11 minutes ago             
ze7d0p8m8wx6   nginx-stack_nginx-web.4   nginx:1.19.3-alpine   guardwhy02   Running         Running 11 minutes ago             
itzy9fa87v50   nginx-stack_nginx-web.5   nginx:1.19.3-alpine   guardwhy03   Running         Running 11 minutes ago             
[root@guardwhy01 data]# 

5、刪除stack服務,執行以下命令

[root@guardwhy01 data]# docker stack rm nginx-stack
Removing service nginx-stack_nginx-web
Removing network nginx-stack_nginx-net
[root@guardwhy01 data]# 


6、小結

  • networks中也可以不指定driver:overlay,因為docker swarm預設網路型別是overlay。
  • 整個networks都可以不用配置,stack部署時會預設建立網路。定義網路在docker stack deploy時,會先預設建立一個網路。
  • 注意一定要把映象先拉取到本地然後再執行。

1.8 Stack和Compose區別

  • Docker stack會忽略了構建指令,無法使用stack命令構建新映象,它是需要映象是預先已經構建好的。 所以docker-compose更適合於開發場景。
  • Docker Compose是一個Python專案,它使用Docker API規範來操作容器。所以需要安裝Docker -compose,以便與Docker一起在計算機上使用。
  • Docker Stack功能包含在Docker引擎中,不需要安裝額外的包來使用它。docker stacks 只是swarm mode的一部分。
  • Docker stack不支援基於第2版寫的docker-compose.yml ,也就是version版本至少為3。然而Docker Compose對版本為2和3的 檔案仍然可以處理。
  • docker stack把docker compose的所有工作都做完了,因此docker stack將佔主導地位。對於大多數使用者來說,切換到使用docker stack既不困難,也不需要太多的開銷。如果是Docker新手,或正在選擇用於新專案的技術,請使用docker stack