docker入門概覽

docker入門概覽

標籤 : docker



本文對docker進行大致介紹,包括概述,安裝,簡單使用,架構,基本原理等方面

寫在前面

  • 本文是自己學習docker的一個記錄和整理,啃英文文檔挺喫力的,懶得翻譯,所以寫這篇類似“索引”的文章,希望能幫助和我一樣的新手快速入門
  • 本文主要參考官方文檔(Docker Document)和相關技術博客
  • 如果有理解有誤的地方還望不吝指正

概述

什麼是Docker?

可以參考下面三篇文章。從我使用的感受來看,我覺得Docker就是一個應用打包工具,把寫好的應用用docker打包發佈,然後別人就可以直接部署使用了,特別方便。

什麼是Docker Engine?

Docker Engine is a client-server application with these major components:

  • A server which is a type of long-running program called a daemon process.
  • A REST API which specifies interfaces that programs can use to talk to the daemon and instruct it what to do.
  • A command line interface (CLI) client.

我覺得官網的解釋很言簡意賅,附上圖(摘自官網)

Docker Engine

Docker的用處

  • Faster delivery of your applications
  • Deploying and scaling more easily
  • Achieving higher density and running more workloads

安裝

安裝參考Install Docker Engine

Ubuntu

以ubuntu 14.04 爲例,參考Installation on Ubuntu安裝Docker engine

這裏列出重要的步驟:

  1. 更新apt源,包括添加證書,密鑰等
  2. 用sudo apt-get安裝
  3. 進一步配置,主要是創建docker用戶組

注 :如果輸入docker info出問題,多半是權限問題,以sudo運行試試

Mac OS X

Mac下安裝參考Installation on Mac OS X

Mac下有兩種安裝方式供選

  • Docker for Mac : Mac的原生應用,沒有使用虛擬機(VirtualBox),而是使用的HyperKit
  • Docker Toolbox : 會安裝虛擬機,使用docker-machine來運行boot2docker 和Docker Engine

兩者的區別請參考 Docker for Mac vs. Docker Toolbox

演示

先不多說,跑起來體驗下。具體的步驟和指令在Docker簡明教程這篇文章已經寫得很清楚了,在此不再贅述

架構和原理

dokcer architecture

由上圖可知,docker是一個client-server架構

  • The Docker daemon : 運行在主機上
  • The Docker client : 用戶和dokcer daemon交互的接口

docker的內部主要有三種資源/組件

  • docker images : build component,只可讀
  • docker registries : distribution component,images共享庫
  • docker containers : run component

這裏重點說說images and containers

Docker使用union file systems 把不同的層(layer)做整合成單一的image. Union File System的中一種是AUFS,可以參考這篇博文

官網文檔對image的layers是這麼描述的

Each Docker image references a list of read-only layers that represent filesystem differences. Layers are stacked on top of each other to form a base for a container’s root filesystem

新版docker(version>=1.10)的存儲模型有變化

Previously, image and layer data was referenced and stored using a randomly generated UUID. In the new model this is replaced by a secure content hash.

container-based-on-ubuntu15.04

而container和image的主要區別就在於top writable layer,所有對image的更改都保存在這一層。換句話說,多個container可以共享同一個image,這就大大節省了空間。實現image和container的管理有兩個關鍵的技術:stackable image layers 和 copy-on-write (CoW).

multiple containers

從圖中可以看出,copy-on-write (CoW)是一個很好的策略,既節省了空間,又避免了因數據共享帶來的寫衝突問題,從而提高效率。

結語

本文主要對docker做簡單介紹,對於一些更詳細的知識,如docker file,volume,network,docker compose等等,會另寫文章進行介紹。至於很具體的操作指令,比如怎麼安裝,怎麼build image和run container來跑一個簡單的docker hello world,請參考官方文檔Docker Engine部分的“get started with docker”或者”learn by example”,也可參考文末的一些參考資料

參考資料


作者@brianway更多文章:個人網站 | CSDN | oschina

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章