程式語言初學者 Docker 入門第一章 —— 簡介

用 Docker 的好處簡單來說就是

Build, Ship and Run Any App Anywhere

你可以在任何地方搭建、遷移、執行你的 App

是不是聽起來很方便!(大致上)不受機器限制,不用裝討人厭的環境!

起源

Virtual Machine (VM) 虛擬機起源

我想要在同一台電腦上運行不同作業系統,虛擬機用來解決這個問題

Virtual machines were originally designed to allow multiple operating systems to run on a single physical machine. The objective is to allow users to create a virtual environment that’s isolated from the underlying hardware. VMs abstract hardware details to make it easier to run applications on different hardware architectures and use hardware resources more efficiently.

虛擬機器最初設計的目的是允許多個作業系統在單一實體機上運行。目標是允許使用者建立與底層硬體隔離的虛擬環境。虛擬機器抽象化了硬體細節,以便更輕鬆地在不同硬體架構上運行應用程式並更有效地使用硬體資源。

Docker 起源

我想要在同個作業系統上運行多個應用程式,Docker 透過容器化 (containerize) 解決了這個問題。

Docker was designed to provide a lightweight and portable way to package and run applications in an isolated and reproducible environment. Docker abstracts operating system details to address the challenge of deploying applications across different environments, such as development, testing, and production. It can be very challenging to manage software environment updates and maintain environment consistency everywhere. This is especially true for organizations that run hundreds of applications or decompose applications into hundreds of microservices.
 
Docker 旨在提供一種輕量級且可移植的方式,以便在隔離且可重複的環境中打包和運行應用程式。 Docker 抽象化了作業系統細節,以解決跨不同環境(例如開發、測試和生產)部署應用程式的挑戰。管理軟體環境更新並維護各地環境的一致性可能非常具有挑戰性。對於運行數百個應用程式或將應用程式分解為數百個微服務的組織來說尤其如此。

簡單來說

VM 可讓您在任何硬體上執行虛擬機器。
Docker 允許您在任何作業系統上執行應用程式。

何謂虛擬化? 

Virtualization is a process that allows a computer to share its hardware resources with multiple digitally separated environments. Each virtualized environment runs within its allocated resources, such as memory, processing power, and storage. With virtualization, organizations can switch between different operating systems on the same server without rebooting.

允許電腦與多個數位隔離環境共享其硬體資源的過程。每個虛擬化環境都在其分配的資源(例如記憶體、處理能力和儲存)內運作。透過虛擬化,不用重開機就可以在同一台伺服器上的不同作業系統之間切換。

虛擬化中的兩個重要概念

Virtual machine 虛擬機

A virtual machine is a software-defined computer that runs on a physical computer with a separate operating system and computing resources. The physical computer is called the host machine and virtual machines are guest machines. Multiple virtual machines can run on a single physical machine. Virtual machines are abstracted from the computer hardware by a hypervisor.

虛擬機器是一種軟體定義的計算機,運行在具有獨立作業系統和計算資源的實體計算機上。實體電腦稱為主機,虛擬機器稱為客機。多個虛擬機器可以在單一實體機上運作。虛擬機器是由虛擬機器管理程式從電腦硬體抽象化出來的。

Hypervisor 管理程式

The hypervisor is a software component that manages multiple virtual machines in a computer. It ensures that each virtual machine gets the allocated resources and does not interfere with the operation of other virtual machines.

虛擬機器管理程式是管理電腦中多個虛擬機器的軟體元件。它確保每個虛擬機器獲得分配的資源並且不會干擾其他虛擬機器的運作。

圖1. A guest OS resides within a VM,
which is installed on top of a hypervisor and host.

虛擬化的兩種實現方式

虛擬化透過以下兩種不同方式來實現

  1. 傳統:硬體模擬
  2. 作業系統軟體:像 Docker 的虛擬化容器:更為優雅,它充分利用作業系統本身具備的機制和特性,可以實現超越傳統虛擬機的輕量級虛擬化。

容器技術屬於作業系統級虛擬化

傳統虛擬化方式:在硬體層實現虛擬化,需有額外虛擬機管理軟體和虛擬機作業系統層

App A App B
Bins/Libd Bins/Libd
Guest OS Guest OS
Hypervisor
Host OS
Server

Docker 虛擬化方式:作業系統層上實現虛擬化,直接使用本機的作業系統,因此更加輕量化

App A App B
Bins/Libd Bins/Libd
Docker Engine
Host OS
Server

看出差異了嗎?把 Hypervisor + Host OS 層換成 Docker Engine

Hypervisor 會在機器的實體硬體與虛擬機器之間進行協調。
Docker Engine 會在作業系統與 Docker 容器之間進行協調。

了解以上基本原理之後,就知道其實他們是可以 mix 在一起的(圖 2.),有這個觀念就好,這邊就不細談。

圖2. By mixing and matching Docker hosts with “traditional” VMs, sysadmins can be assured they are getting the maximum utilization out of their physical hardware.

Docker 三大核心概念

之後章節會細講,這邊先提個概念。
  • 映像檔 — Image (第三章)

    類似於虛擬機映像檔,建立 Docker 容器的基礎。使用者可以從網路下載一個已經做好的應用系統映像檔直接使用。

  • 容器 — Container(第四章)

    類似輕量級的 sandbox,Docker 利用 Container 來運行和隔離應用。Container 是用 image 所創造的執行實例 instance。可以將其建立、開始、停止、刪除。這些容器彼此互相隔離,互不見彼此。可視為簡易版的 Linux 系統環境(包括 root 使用者權限,PID namespace, User Namespace, Network Namespace)以及運行字中的應用程式包裝而成的執行環境。

  • 倉庫 — Repository(第五章)

    程式碼儲存庫, Docker 集中存放 image 的場所。每個 repository 會有多個 image,存放 repository 的地方叫 Registry 倉庫註冊伺服器。

    依據儲存地映像檔是否公開分享,分為:公開倉庫(Public)(Docker Hub:目前最大的公開倉庫,為關方提公);私有倉庫(Private)

Reference

Comments

Popular posts from this blog

《 Imgproxy 使用分析一:圖片下載速度優化分析:Akamai CDN vs Imgproxy 效能比較》

《 Akamai + S3 與 CloudFront + Imgproxy + S3 使用分析二:壓縮圖片設計流程:檔案大小 vs 載入時間的權衡》

PostgreSQL 具有的 NoSQL 特性