SlideShare a Scribd company logo
1 of 16
Docker Sharing
Why one process per container
話說從頭
• Web 服務跟 db 應該放在同一
個 container 才對
• 這沒辦法說服我!
• 根據我的想像,愈多
container 應該是愈吃資源
有一天,對話是這樣的…
• 但是這樣不好管理,通常一個
container 只放一個 service
• ………
• ㄟ…其實我也不清楚底層是怎
麼做的
先說結論
• One container should have one service
• 讓 container 更易於管理
• 維持可擴充性 (對 web 服務而言)
其它人怎麼說
• Docker 官網 Run only one process per container
• Run only one process per container. In almost all cases, you should only
run a single process in a single container. Decoupling applications into
multiple containers makes it much easier to scale horizontally and reuse
containers. If that service depends on another service, make use of
container linking.
其它人怎麼說
• 或是來看看最近火紅的微服務 (Microservices)
• 是一種軟體架構風格
• 以專注於單一責任與功能的小型功能區塊 (Small Building Blocks) 為基礎
• 以模組化的方式組合出複雜的大型應用程式
• 各功能區塊使用與語言無關的 API 集相互通訊
• (以上來自維基百科)
其它人怎麼說
• 我的田野調查
• 通常 docker service 只會監控 PID 為 1 的 process
• container 裡如果手動啟動其它服務,假如 container 死了,就失控了
PID 為 1 ?!
先來看 Docker 的架構
再來從 Linux 的 Process Namespace 說起
• Every time a computer with Linux boots up, it starts with just
one process, with process identifier (PID) 1, this process is the
root of the process tree
• The PID namespace allows one to spin off a new tree, with its
own PID 1 process
來張圖吧
Process Namespace 就
像這樣
來看個實際的例子
• 以 redis container 為例
• 以下為 Dockerfile example
FROM ubuntu:14.04
RUN apt-get update && apt-get -y install redis-server
EXPOSE 6379
CMD /usr/bin/redis-server #在 bin/sh 中執行
CMD [“/usr/bin/redis-server”] #exec 執行
來看個實際的例子
• 最後的 CMD 裡,最好以 bash 方式執行 Application
• PID 1 為 bash,可確保接收子 process 時可以正確的處理
來看個實際的例子
實際操作
所以…
• container 為 Docker Daemon 的子 process
• container 各自擁有自己的 process namespace
• container 之間是相互隔離的
• 啟動 container 時,跟著 container 一起啟動的 process 的 PID 為1
• container 停止時,會將 PID 1 的 process 及其子 process 終止
References
• Each container should have only one concern
https://docs.docker.com/engine/userguide/eng-
image/dockerfile_best-practices/#/each-container-should-have-
only-one-concern
• 理解Docker容器的進程管理
https://yq.aliyun.com/articles/5545
• Separation Anxiety: A Tutorial for Isolating Your System with
Linux Namespaces
https://www.toptal.com/linux/separation-anxiety-isolating-your-
system-with-linux-namespaces
References
• Docker 和 PID 1 殭屍進程問題
https://www.oschina.net/translate/docker-and-the-pid-1-
zombie-reaping-problem

More Related Content

Similar to Why one process per container

Windows Container 101: dotNET, Container, Kubernetes
Windows Container 101: dotNET, Container, KubernetesWindows Container 101: dotNET, Container, Kubernetes
Windows Container 101: dotNET, Container, KubernetesWill Huang
 
Docker 最佳实践
Docker 最佳实践Docker 最佳实践
Docker 最佳实践YuLing Liu
 
Azure Container Service 使用 DC / OS 管理 docker 容器
Azure Container Service 使用 DC / OS 管理 docker 容器Azure Container Service 使用 DC / OS 管理 docker 容器
Azure Container Service 使用 DC / OS 管理 docker 容器Ch Rick
 
企業導入微服務實戰 - updated
企業導入微服務實戰 - updated企業導入微服務實戰 - updated
企業導入微服務實戰 - updatedPaul Chao
 
廣宣學堂: 企業導入微服務實戰
廣宣學堂: 企業導入微服務實戰廣宣學堂: 企業導入微服務實戰
廣宣學堂: 企業導入微服務實戰Paul Chao
 
Dci vs aggregate_dddtw_2021-0.3-16-9
Dci vs aggregate_dddtw_2021-0.3-16-9Dci vs aggregate_dddtw_2021-0.3-16-9
Dci vs aggregate_dddtw_2021-0.3-16-9teddysoft
 
A brief introduction to Vagrant – 原來 VirtualBox 可以這樣玩
A brief introduction to Vagrant – 原來 VirtualBox 可以這樣玩A brief introduction to Vagrant – 原來 VirtualBox 可以這樣玩
A brief introduction to Vagrant – 原來 VirtualBox 可以這樣玩Wen-Tien Chang
 
kubernetes入门培训
kubernetes入门培训kubernetes入门培训
kubernetes入门培训bobo45284
 
Docker workshop
Docker workshopDocker workshop
Docker workshopWei Tung
 
Azure Container Registry(preview)x Web App On Linux(preview)
Azure Container Registry(preview)x Web App On Linux(preview)Azure Container Registry(preview)x Web App On Linux(preview)
Azure Container Registry(preview)x Web App On Linux(preview)Ch Rick
 
HDInsight for Microsoft Users
HDInsight for Microsoft UsersHDInsight for Microsoft Users
HDInsight for Microsoft UsersKuo-Chun Su
 
Solver Workshop 規劃求解工作坊 (2010.06.12 @ CCL)
Solver Workshop 規劃求解工作坊 (2010.06.12 @ CCL)Solver Workshop 規劃求解工作坊 (2010.06.12 @ CCL)
Solver Workshop 規劃求解工作坊 (2010.06.12 @ CCL)Calvin C. Yu
 

Similar to Why one process per container (15)

Windows Container 101: dotNET, Container, Kubernetes
Windows Container 101: dotNET, Container, KubernetesWindows Container 101: dotNET, Container, Kubernetes
Windows Container 101: dotNET, Container, Kubernetes
 
Docker 最佳实践
Docker 最佳实践Docker 最佳实践
Docker 最佳实践
 
Azure Container Service 使用 DC / OS 管理 docker 容器
Azure Container Service 使用 DC / OS 管理 docker 容器Azure Container Service 使用 DC / OS 管理 docker 容器
Azure Container Service 使用 DC / OS 管理 docker 容器
 
Linking error
Linking errorLinking error
Linking error
 
企業導入微服務實戰 - updated
企業導入微服務實戰 - updated企業導入微服務實戰 - updated
企業導入微服務實戰 - updated
 
廣宣學堂: 企業導入微服務實戰
廣宣學堂: 企業導入微服務實戰廣宣學堂: 企業導入微服務實戰
廣宣學堂: 企業導入微服務實戰
 
Dci vs aggregate_dddtw_2021-0.3-16-9
Dci vs aggregate_dddtw_2021-0.3-16-9Dci vs aggregate_dddtw_2021-0.3-16-9
Dci vs aggregate_dddtw_2021-0.3-16-9
 
Docker基礎
Docker基礎Docker基礎
Docker基礎
 
A brief introduction to Vagrant – 原來 VirtualBox 可以這樣玩
A brief introduction to Vagrant – 原來 VirtualBox 可以這樣玩A brief introduction to Vagrant – 原來 VirtualBox 可以這樣玩
A brief introduction to Vagrant – 原來 VirtualBox 可以這樣玩
 
kubernetes入门培训
kubernetes入门培训kubernetes入门培训
kubernetes入门培训
 
Docker workshop
Docker workshopDocker workshop
Docker workshop
 
Azure Container Registry(preview)x Web App On Linux(preview)
Azure Container Registry(preview)x Web App On Linux(preview)Azure Container Registry(preview)x Web App On Linux(preview)
Azure Container Registry(preview)x Web App On Linux(preview)
 
Kubernetes on gcp
Kubernetes on gcpKubernetes on gcp
Kubernetes on gcp
 
HDInsight for Microsoft Users
HDInsight for Microsoft UsersHDInsight for Microsoft Users
HDInsight for Microsoft Users
 
Solver Workshop 規劃求解工作坊 (2010.06.12 @ CCL)
Solver Workshop 規劃求解工作坊 (2010.06.12 @ CCL)Solver Workshop 規劃求解工作坊 (2010.06.12 @ CCL)
Solver Workshop 規劃求解工作坊 (2010.06.12 @ CCL)
 

Why one process per container

  • 1. Docker Sharing Why one process per container
  • 2. 話說從頭 • Web 服務跟 db 應該放在同一 個 container 才對 • 這沒辦法說服我! • 根據我的想像,愈多 container 應該是愈吃資源 有一天,對話是這樣的… • 但是這樣不好管理,通常一個 container 只放一個 service • ……… • ㄟ…其實我也不清楚底層是怎 麼做的
  • 3. 先說結論 • One container should have one service • 讓 container 更易於管理 • 維持可擴充性 (對 web 服務而言)
  • 4. 其它人怎麼說 • Docker 官網 Run only one process per container • Run only one process per container. In almost all cases, you should only run a single process in a single container. Decoupling applications into multiple containers makes it much easier to scale horizontally and reuse containers. If that service depends on another service, make use of container linking.
  • 5. 其它人怎麼說 • 或是來看看最近火紅的微服務 (Microservices) • 是一種軟體架構風格 • 以專注於單一責任與功能的小型功能區塊 (Small Building Blocks) 為基礎 • 以模組化的方式組合出複雜的大型應用程式 • 各功能區塊使用與語言無關的 API 集相互通訊 • (以上來自維基百科)
  • 6. 其它人怎麼說 • 我的田野調查 • 通常 docker service 只會監控 PID 為 1 的 process • container 裡如果手動啟動其它服務,假如 container 死了,就失控了
  • 7. PID 為 1 ?!
  • 9. 再來從 Linux 的 Process Namespace 說起 • Every time a computer with Linux boots up, it starts with just one process, with process identifier (PID) 1, this process is the root of the process tree • The PID namespace allows one to spin off a new tree, with its own PID 1 process
  • 11. 來看個實際的例子 • 以 redis container 為例 • 以下為 Dockerfile example FROM ubuntu:14.04 RUN apt-get update && apt-get -y install redis-server EXPOSE 6379 CMD /usr/bin/redis-server #在 bin/sh 中執行 CMD [“/usr/bin/redis-server”] #exec 執行
  • 12. 來看個實際的例子 • 最後的 CMD 裡,最好以 bash 方式執行 Application • PID 1 為 bash,可確保接收子 process 時可以正確的處理
  • 14. 所以… • container 為 Docker Daemon 的子 process • container 各自擁有自己的 process namespace • container 之間是相互隔離的 • 啟動 container 時,跟著 container 一起啟動的 process 的 PID 為1 • container 停止時,會將 PID 1 的 process 及其子 process 終止
  • 15. References • Each container should have only one concern https://docs.docker.com/engine/userguide/eng- image/dockerfile_best-practices/#/each-container-should-have- only-one-concern • 理解Docker容器的進程管理 https://yq.aliyun.com/articles/5545 • Separation Anxiety: A Tutorial for Isolating Your System with Linux Namespaces https://www.toptal.com/linux/separation-anxiety-isolating-your- system-with-linux-namespaces
  • 16. References • Docker 和 PID 1 殭屍進程問題 https://www.oschina.net/translate/docker-and-the-pid-1- zombie-reaping-problem