1. 程式人生 > 其它 >Docker基礎 - 04映象及倉庫

Docker基礎 - 04映象及倉庫

Docker基礎 - 04映象及倉庫

一、Docker映象

1.1 映象

Docker映象含有啟動容器所需要的檔案系統及其內容,用於建立並啟動docker容器。
  • 採用分層構建機制,最底層為bootfs,上一層為rootfs
  • bootfs: 用於系統引導的檔案系統,包括bootloader和kernel,容器啟動完成後會被解除安裝以節約記憶體資源。
  • rootfs: 位於bootfs之上,表現為docker容器的根檔案系統。 Base Image
    • 傳統模式中,系統啟動時,核心掛載rootfs時會首先將其掛載為"只讀"模式,完整性自檢完成後將其重新掛載為讀寫模式。
    • Docker中, rootfs由核心掛載為"只讀"模式, 而後通過"聯合掛載"技術額外掛載一個"可寫"層。

1.2 Docker Image Layer

位於下層的映象稱為父映象(parent image),最底層的稱為基礎映象(base image)

最上層為"可讀寫"層, 其下的均為"只讀"層。

1.3 Aufs

  • advanced multi-layered unification filesystem: 高階多層統一檔案系統
  • 用於為Linux檔案系統實現"聯合掛載"
  • aufs是之前的UnionFS的重新實現,2006年由 Junjiro Okajima開發
  • Docker 最初使用aufs作為容器檔案系統層,目前仍作為儲存後端之一來支援
  • aufs的競爭產品是overlayfs, overlayfs自從3.18版本開始被合併到Linux核心
  • docker的分層映象,除了aufs, docker還支援 btrfs, devicemapper和vfs等
    • 在Ubuntu系統下,docker預設Ubuntu的aufs;而在CentOS7上,用的是devicemapper

1.4 Devicemapper

(1.4.1) Device Mapper是 Linux 2.6核心中支援邏輯卷管理的通用裝置對映機制,它為實現用於儲存資源管理的塊裝置驅動提供了一個高度模組化的核心架構。

(1.4.2) 在核心中通過一個一個模組化的target driver外掛實現對IO請求的過濾或者重新定向等工作,

當前已經實現的target driver外掛包括軟raid、軟加密、邏輯卷條帶、多路徑、映象、快照等。

  • liner、mirror、snapshot、multipath 表示的就是target driver。
  • 在這些外掛中,Thin Provisioning Snapshot,Docker使用Thin Provisioning的Snapshot的技術實現了類似aufs的分層映象。

二、Docker Registry

2.1 映象拉取

啟動容器時,docker daemon會嘗試從本地獲取相關的映象;本地映象不存在時,將會從Registry中下載該映象並儲存到本地

docker pull <registry>[:port]/[<namespace>/]<name>:<tag>

2.2 Registry 分類

Registry用於儲存docker映象, 包括映象的層次結構和元資料

  • Sponsor Registry: 第三方的registry, 供客戶和Docker社群使用
  • Mirror Registry: 第三方的registry, 只讓客戶使用
  • Vendor Registry: 由釋出Docker映象的供應商提供的registry
  • Private Registry: 通過設有防火牆和額外的安全層的私有實體提供的registry

2.3 Registry 結構

Repository

  • 由某特定的docker映象的所有迭代版本組成的映象倉庫
  • 一個Registry中可以存在多個Repository
    • Repository可分為"頂層倉庫"和"使用者倉庫"
    • 使用者倉庫名稱格式為"使用者名稱/倉庫名"
  • 每個倉庫可以包含多個Tag(標籤),每個標籤對應一個映象

Index

  • 維護使用者賬戶、映象的校驗以及公共名稱空間的資訊
  • 相當於為Registry提供了一個完成使用者認證等功能的檢索介面

2.4 映象推送

Docker Registry中的映象通常由開發人員製作,而後推送至"公共"或"私有"Registry上儲存,供其他人員使用。

[root@component ~]# docker push --help

Usage:  docker push [OPTIONS] NAME[:TAG]

Push an image or a repository to a registry

Options:
  -a, --all-tags                Push all tagged images in the repository
      --disable-content-trust   Skip image signing (default true)
  -q, --quiet                   Suppress verbose output

2.5 Docker Hub

Docker Hub is a cloud-based registry service which allows you to link to code repositories, build you images and test them,

stores manually pushed images, and links to Docker Cloud so you can deploy images to you hosts.

major features: Image Repositories| Automated Builds | Webhooks | Origanizations | GitHub and Bitbucket Integration

2.6 Quay.io

RedHat coreos/flannel

三、映象操作

3.1 映象生成

  • Dockerfile
  • 基於容器製作
  • Docker Hub automated builds

3.2 基於容器製作映象

# 使用 busybox 製作一個httpd的web服務映象
[root@cl-server ~]# docker pull busybox
[root@cl-server ~]# docker run -it --name b1 busybox
/ # ls /
bin   dev   etc   home  proc  root  sys   tmp   usr   var
/ # mkdir -p /data/html
/ # vi /data/html/index.html
/ # cat /data/html/index.html
<h1>Hello, Busybox httpd Web Server.</h1>

[root@cl-server ~]# docker commit -p d689609f9c15
sha256:b680392e10f2253c55e8a3616c1c9cdf9c52a59d741ce058a251f2ce133fa623
[root@cl-server ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
<none>       <none>    b680392e10f2   4 seconds ago   1.23MB
busybox      latest    388056c9a683   2 weeks ago     1.23MB
[root@cl-server ~]# docker tag b680392e10f2 kunking/httpd:v0.1-1
[root@cl-server ~]# docker images
REPOSITORY      TAG       IMAGE ID       CREATED          SIZE
kunking/httpd   v0.1-1    b680392e10f2   56 seconds ago   1.23MB
busybox         latest    388056c9a683   2 weeks ago      1.23MB

在kunking/httpd:v0.1-1基礎上,生成新的映象

--author,-a Author

--change,-c Apply Dockerfile instruction to the created image

--message,-m Commit message

--pause, -p Pause container during commit, default=true

docker commit -a "Bearpx <[email protected]>" -c 'CMD ["/bin/httpd","-f","-h","/data/html"]' -p b1 kunking/httpd:v0.2

  

3.3 為映象設定標籤: docker tag

Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
[root@cl-server ~]# docker tag --help
Usage: docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

[root@cl-server ~]# docker tag 5a38ea0db06f kunking/httpd:v0.1-3

  

3.4 登入DockerHub倉庫或阿里雲映象倉庫

# docker login
[root@cl-server ~]# docker login --help
Usage:  docker login [OPTIONS] [SERVER]

Log in to a Docker registry.
If no server is specified, the default is defined by the daemon.

Options:
  -p, --password string   Password
      --password-stdin    Take the password from stdin
  -u, --username string   Username

# docker logout
[root@cl-server ~]# docker logout --help
Usage:  docker logout [SERVER]

Log out from a Docker registry.
If no server is specified, the default is defined by the daemon.
[root@cl-server ~]# docker login --username=157****4250 registry.cn-hangzhou.aliyuncs.com
[root@cl-server .docker]# pwd
/root/.docker
[root@cl-server .docker]# cat config.json 
{
	"auths": {
		"registry.cn-hangzhou.aliyuncs.com": {
			"auth": "MTU3MjUyNTQyNTA6emhhaTg5Mzg5aW1hZ2Vz"
		}
	}
}

3.5 映象匯入和匯出

docker save -o myimages.tar.gz  kunking/httpd:v0.2  kunking/tm:v0.2
docker load -i myimages.tar.gz

 

[root@cl-server ~]# docker save --help
Usage:  docker save [OPTIONS] IMAGE [IMAGE...]
Save one or more images to a tar archive (streamed to STDOUT by default)
Options:
  -o, --output string   Write to a file, instead of STDOUT

[root@cl-server ~]# docker load --help
Usage:  docker load [OPTIONS]
Load an image from a tar archive or STDIN
Options:
  -i, --input string   Read from tar archive file, instead of STDIN
  -q, --quiet          Suppress the load output

  

3.6 image管理

[root@component ~]# docker image --help

Usage:  docker image COMMAND

Manage images

Commands:
  build       Build an image from a Dockerfile
  history     Show the history of an image
  import      Import the contents from a tarball to create a filesystem image
  inspect     Display detailed information on one or more images
  load        Load an image from a tar archive or STDIN
  ls          List images
  prune       Remove unused images
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rm          Remove one or more images
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

Run 'docker image COMMAND --help' for more information on a command.

docker 映象命令  

  • docker images
  • docker images -a / docker images -q / docker images --digests / docker images --digests --no-trunc
  • docker search XXX : 從 docker Hub 查詢
  • docker search -s 30 tomcat
  • docker search -s 30 --no-trunc tomcat
  • docker pull tomcat | docker pull tomcat:latest
  • docker rmi hello-world
  • docker rmi -f hello-world
  • docker rmi -f $(docker images -q)
  • docker history XXX: 檢視映象的各映象層的大小
[root@component ~]# docker history nginx
IMAGE          CREATED       CREATED BY                                      SIZE      COMMENT
87a94228f133   3 weeks ago   /bin/sh -c #(nop)  CMD ["nginx" "-g" "daemon…   0B        
<missing>      3 weeks ago   /bin/sh -c #(nop)  STOPSIGNAL SIGQUIT           0B        
<missing>      3 weeks ago   /bin/sh -c #(nop)  EXPOSE 80                    0B        
<missing>      3 weeks ago   /bin/sh -c #(nop)  ENTRYPOINT ["/docker-entr…   0B        
<missing>      3 weeks ago   /bin/sh -c #(nop) COPY file:09a214a3e07c919a…   4.61kB    
<missing>      3 weeks ago   /bin/sh -c #(nop) COPY file:0fd5fca330dcd6a7…   1.04kB    
<missing>      3 weeks ago   /bin/sh -c #(nop) COPY file:0b866ff3fc1ef5b0…   1.96kB    
<missing>      3 weeks ago   /bin/sh -c #(nop) COPY file:65504f71f5855ca0…   1.2kB     
<missing>      3 weeks ago   /bin/sh -c set -x     && addgroup --system -…   64MB      
<missing>      3 weeks ago   /bin/sh -c #(nop)  ENV PKG_RELEASE=1~buster     0B        
<missing>      3 weeks ago   /bin/sh -c #(nop)  ENV NJS_VERSION=0.6.2        0B        
<missing>      3 weeks ago   /bin/sh -c #(nop)  ENV NGINX_VERSION=1.21.3     0B        
<missing>      3 weeks ago   /bin/sh -c #(nop)  LABEL maintainer=NGINX Do…   0B        
<missing>      3 weeks ago   /bin/sh -c #(nop)  CMD ["bash"]                 0B        
<missing>      3 weeks ago   /bin/sh -c #(nop) ADD file:910392427fdf089bc…   69.3MB