Docker 字面上的意思是「碼頭工人」,碼頭上會有打包、運送…等服務,碼頭工人 (Docker) 可快速的用貨櫃 (Container) 將貨物 (Application) 裝上船。
建置 image,然後使用 Registry 來管理 image,再使用 Docker Engine 將 container 和包含的 App 在任意平台 (不管是實體機、虛擬機還是雲端) 上執行
以 Node.js 為例,每個專案都需要有開發環境
雖然較省錢,但每個專案都需維護 VM 檔案,以 VMware 為例,包括 虛擬磁碟 (.vmdk)、VM 的記憶體 (.vmem)、虛擬 BIOS (.nvram)、設定檔 (.vmx)、補充設定檔 (.vmxf)、紀錄檔 (.log)、快照 (.vmsd)、快照狀態資訊 (.vmsn)、紀錄 VM 暫停狀態 (.vmss) … 等檔案
D:\VM\Ubuntu $ ls -al
total 30558838
drwxr-xr-x 1 Titan 197609 0 一月 10 01:42 .
drwxr-xr-x 1 Titan 197609 0 八月 10 11:21 ..
-rw-r--r-- 1 Titan 197609 8684 一月 10 01:42 'Ubuntu.nvram'
-rw-r--r-- 1 Titan 197609 27544518656 一月 10 01:42 'Ubuntu.vmdk'
-rw-r--r-- 1 Titan 197609 0 八月 6 21:37 'Ubuntu.vmsd'
-rw-r--r-- 1 Titan 197609 2994 一月 10 01:42 'Ubuntu.vmx'
-rw-r--r-- 1 Titan 197609 383 八月 6 23:17 'Ubuntu.vmxf'
-rw-r--r-- 1 Titan 197609 3741319168 一月 10 00:59 'Ubuntu-f82919f4.vmem'
-rw-r--r-- 1 Titan 197609 6130557 一月 10 01:42 'Ubuntu-f82919f4.vmss'
-rw-r--r-- 1 Titan 197609 270160 一月 10 01:42 vmware.log
...
(虛擬化技術 vs 容器化技術)
常見:VirtualBox、VMWare
以往的虛擬化技術是在 Host OS 上建立虛擬環境,透過 Hypervisor 模擬一套完整的硬體環境資源 (Guest OS),目標是建立一個可以用來執行整套 OS 的沙箱獨立執行環境,所以 VM 可以建立多個獨立的環境,使用者就能 Guest OS 上安裝其他應用程式
而容器化技術則是透過在 Host OS 上執行 Container Engine 來建立各個 Container (虛擬執行環境,直接使用 Host OS 的系統資源),每個 Container 也都是彼此獨立,但 Container 是共用相同的核心,共享系統資源
Docker 為什麼會比傳統 VM 輕量?
因為少跑了 N 個完整的 Guest OS
其實 container (容器) 和 VM 的目標是相似的,都是為了將 APP 和其依賴分離成可以在任何環境上獨立執行。另外,container 和 VM 減少了對於實體硬體的需求,無論是在成本和效益上,都可以更加有效的利用運算資源。
Virtualization | Containerization | |
---|---|---|
啟動 | 慢 (最快也要分鐘) | 快 (秒開) |
容量 | 大 (GB) | 小 (MB) |
效能 | 慢 | 快 |
host 可支撐數量 | 數個~數十個 | 數個~數百個 |
複製相同環境 | 超慢 | 快 |
原生硬體的效能遠遠超過 VM 的模擬硬體效能,將 Docker 直接安裝在實體伺服器的 Linux 中,一定比先安裝 VM Linux,再在此 VM 中執行 Docker 要快上許多。但企業考量的重點除了效能之外更有部署的便利性、現存系統的穩定度、整個系統的高可用性等重點,筆書建議還是將 Docker 部署在 VM 中,犧牲一點點效能,換來的是更方便的部署、更穩定的環境以及更彈性的設定,這是絕對值得的。
本頁摘至最完整的 Docker 聖經一文
進去後會看到一個倒數的時間,因為每次 PWD 會提供 4 小時的使用時間。接著只要按「+ ADD NEW INSTANCE」就可以建立一台新的 VM。
看到 shell 就可以開始練習 Docker
執行 docker run hello-world
指令後,會看到下面一大串輸出:
$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
ca4f61b1923c: Pull complete
Digest: sha256:66ef312bbac49c39a89aa9bcc3cb4f3c9e7de3788c944158df3ee0176d32b751
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://cloud.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/engine/userguide/
它會告訴你執行此指令都做了哪些事:
hello-world
此 image 回來。可以到 Docker Hub 看 hello-world
這個 image,裡面有官方提供產生該 image 的 Dockerfile
:
FROM scratch
COPY hello /
CMD ["/hello"]
FROM scratch
:使用哪個 image 作為基底 (Base Image),scratch
是官方提供的空白 imageCOPY hello /
:將 hello
執行檔複製到 /
根目錄CMD ["/hello"]
:執行 hello
此執行檔簡單來說就是在 container 裡面執行 hello
這個執行檔來列印出這堆訊息。
info
:查看 Docker 系統資訊$ docker info
pull
:取得 (拉取) imagelatset
版Already exists
?在 pull image 時,如果主機內已有的 image 中有某些地方相同,就不需要再 pull 一次$ docker pull [repository name]:[TAG|@DIGEST]
$ docker pull node:boron-slim
boron-slim: Pulling from library/node
ad74af05f5a2: Already exists
2b032b8bbe8b: Already exists
a9a5b35f6ead: Pull complete
3245b5a1c52c: Extracting [========> ] 54.03MB/131.9MB
afa075743392: Download complete
...