




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、閱讀目錄· 1.1 Docker 守護進程 · 1.2 Docker 客戶端 · 1.3 Docker 內部 · 1.4 libcontainer · 1.5 命名空間Namespaces · 1.6 資源配額cgroups · 3.1 Search images · 3.2 Pull images · 3.3 Running an interactive shell · 3.4 相關快捷鍵 · 4.1 docker help · 4.2 docker search
2、3; 4.3 docker info · 4.4 docker pull && docker push · 4.5 docker images · 4.6 docker rmi · 4.7 docker run · 4.8 docker start|stop|kill. . · 4.9 Docker 1.3 新增特性和命令 · 4.10 Docker 1.5 新特性 · 5.1 自動映射端口 · 5.2 綁定端口到指定接口 · 6.1 Docker 四種網絡模式 · 6
3、.2 列出當前主機網橋 · 6.3 查看當前 docker0 ip · 6.4 運行一個容器 · 6.5 不同主機間容器通信 · 7.1 FROM · 7.2 MAINTAINER · 7.3 CMD · 7.4 EXPOSE · 7.5 ENV · 7.6 ADD · 7.7 COPY · 7.8 ENTRYPOINT · 7.9 VOLUME · 7.10 USER · 7.11 WORKDIR · 7.12 ONBUILD · 7
4、.13 Dockerfile Examples · 7.14 docker build · 7.15 dockerfile 最佳實踐 · 8.1 數據卷 · 8.2 創建和掛載一個數據卷容器 · 8.3 備份、恢復或遷移數據卷 · 8.4 刪除 Volumes · 9.1 容器命名 · 9.2 鏈接容器 · 10.1 快速構建 · 10.2 不使用容器構建 registry · 10.3 提交指定容器到私有庫 一、Docker 簡介 Docker 兩個主要部件: · Dock
5、er: 開源的容器虛擬化平臺 · Docker Hub: 用于分享、管理 Docker 容器的 Docker SaaS 平臺 - Docker Hub Docker 使用客戶端-服務器 (C/S) 架構模式。Docker 客戶端會與 Docker 守護進程進行通信。Docker 守護進程會處理復雜繁重的任務,例如建立、運行、發布你的 Docker 容器。Docker 客戶端和守護進程可以運行在同一個系統上,當然你也可以使用 Docker 客戶端去連接一個遠程的 Docker 守護進程。Docker 客戶端和守護進程之間通過 socket 或者 RESTful API 進行通信。 1.
6、1 Docker 守護進程 如上圖所示,Docker 守護進程運行在一臺主機上。用戶并不直接和守護進程進行交互,而是通過 Docker 客戶端間接和其通信。 回到頂部1.2 Docker 客戶端 Docker 客戶端,實際上是 docker 的二進制程序,是主要的用戶與 Docker 交互方式。它接收用戶指令并且與背后的 Docker 守護進程通信,如此來回往復。 回到頂部1.3 Docker 內部 要理解 Docker 內部構建,需要理解以下三種部件: · Docker 鏡像 - Docker images · Docker 倉庫 - Docker registeries
7、 · Docker 容器 - Docker containers Docker 鏡像 Docker 鏡像是 Docker 容器運行時的只讀模板,每一個鏡像由一系列的層 (layers) 組成。Docker 使用 UnionFS 來將這些層聯合到單獨的鏡像中。UnionFS 允許獨立文件系統中的文件和文件夾(稱之為分支)被透明覆蓋,形成一個單獨連貫的文件系統。正因為有了這些層的存在,Docker 是如此的輕量。當你改變了一個 Docker 鏡像,比如升級到某個程序到新的版本,一個新的層會被創建。因此,不用替換整個原先的鏡像或者重新建立(在使用虛擬機的時候你可能會這么做),只是一個新 的
8、層被添加或升級了。現在你不用重新發布整個鏡像,只需要升級,層使得分發 Docker 鏡像變得簡單和快速。 Docker 倉庫 Docker 倉庫用來保存鏡像,可以理解為代碼控制中的代碼倉庫。同樣的,Docker 倉庫也有公有和私有的概念。公有的 Docker 倉庫名字是 Docker Hub。Docker Hub 提供了龐大的鏡像集合供使用。這些鏡像可以是自己創建,或者在別人的鏡像基礎上創建。Docker 倉庫是 Docker 的分發部分。 Docker 容器 Docker 容器和文件夾很類似,一個Docker容器包含了所有的某個應用運行所需要的環境。每一個 Docker 容器都是從 Dock
9、er 鏡像創建的。Docker 容器可以運行、開始、停止、移動和刪除。每一個 Docker 容器都是獨立和安全的應用平臺,Docker 容器是 Docker 的運行部分。 回到頂部1.4 libcontainer Docker 從 0.9 版本開始使用 libcontainer 替代 lxc,libcontainer 和 Linux 系統的交互圖如下: · 圖片來源: Docker 0.9: introducing execution drivers and libcontainer 回到頂部1.5 命名空間Namespaces pid namespace 不同用戶的進程就是通過 p
10、id namespace 隔離開的,且不同 namespace 中可以有相同 PID。具有以下特征: · 每個 namespace 中的 pid 是有自己的 pid=1 的進程(類似 /sbin/init 進程) · 每個 namespace 中的進程只能影響自己的同一個 namespace 或子 namespace 中的進程 · 因為 /proc 包含正在運行的進程,因此在 container 中的 pseudo-filesystem 的 /proc 目錄只能看到自己 namespace 中的進程 · 因為 namespace 允許嵌套,父 names
11、pace 可以影響子 namespace 的進程,所以子 namespace 的進程可以在父 namespace 中看到,但是具有不同的 pid 參考文檔:Introduction to Linux namespaces Part 3: PID mnt namespace 類似 chroot,將一個進程放到一個特定的目錄執行。mnt namespace 允許不同 namespace 的進程看到的文件結構不同,這樣每個 namespace 中的進程所看到的文件目錄就被隔離開了。同 chroot 不同,每個 namespace 中的 container 在 /proc/mounts 的信息只包含所
12、在 namespace 的 mount point。 net namespace 網絡隔離是通過 net namespace 實現的, 每個 net namespace 有獨立的 network devices, IP addresses, IP routing tables, /proc/net 目錄。這樣每個 container 的網絡就能隔離開來。 docker 默認采用 veth 的方式將 container 中的虛擬網卡同 host 上的一個 docker bridge 連接在一起。 參考文檔:Introduction to Linux namespaces Part 5: NET
13、uts namespace UTS ("UNIX Time-sharing System") namespace 允許每個 container 擁有獨立的 hostname 和 domain name, 使其在網絡上可以被視作一個獨立的節點而非 Host 上的一個進程。 參考文檔:Introduction to Linux namespaces Part 1: UTS ipc namespace container 中進程交互還是采用 Linux 常見的進程間交互方法 (interprocess communication - IPC), 包括常見的信號量、消息隊列和共享內
14、存。然而同 VM 不同,container 的進程間交互實際上還是 host 上具有相同 pid namespace 中的進程間交互,因此需要在IPC資源申請時加入 namespace 信息 - 每個 IPC 資源有一個唯一的 32bit ID。 參考文檔:Introduction to Linux namespaces Part 2: IPC user namespace 每個 container 可以有不同的 user 和 group id, 也就是說可以以 container 內部的用戶在 container 內部執行程序而非 Host 上的用戶。 有了以上 6 種 namespace
15、從進程、網絡、IPC、文件系統、UTS 和用戶角度的隔離,一個 container 就可以對外展現出一個獨立計算機的能力,并且不同 container 從 OS 層面實現了隔離。 然而不同 namespace 之間資源還是相互競爭的,仍然需要類似 ulimit 來管理每個 container 所能使用的資源 - cgroup。 Reference · Docker Getting Start: Related Knowledge · Docker 介紹以及其相關術語、底層原理和技術 回到頂部1.6 資源配額cgroups cgroups 實現了對資源的配額和度量。 cgro
16、ups 的使用非常簡單,提供類似文件的接口,在 /cgroup 目錄下新建一個文件夾即可新建一個 group,在此文件夾中新建 task 文件,并將 pid 寫入該文件,即可實現對該進程的資源控制。具體的資源配置選項可以在該文件夾中新建子 subsystem ,子系統前綴.資源項 是典型的配置方法, 如 memory.usageinbytes 就定義了該 group 在 subsystem memory 中的一個內存限制選項。 另外,cgroups 中的 subsystem 可以隨意組合,一個 subsystem 可以在不同的 group 中,也可以一個 group 包含多個 subsyste
17、m - 也就是說一個 subsystem。 · memory o 內存相關的限制 · cpu o 在 cgroup 中,并不能像硬件虛擬化方案一樣能夠定義 CPU 能力,但是能夠定義 CPU 輪轉的優先級,因此具有較高 CPU 優先級的進程會更可能得到 CPU 運算。 通過將參數寫入 cpu.shares ,即可定義改 cgroup 的 CPU 優先級 - 這里是一個相對權重,而非絕對值 · blkio o block IO 相關的統計和限制,byte/operation 統計和限制 (IOPS 等),讀寫速度限制等,但是這里主要統計的都是同步 IO ·
18、 devices o 設備權限限制 參考文檔:how to use cgroup 二、Docker 安裝 docker 的相關安裝方法這里不作介紹,具體安裝參考 官檔 獲取當前 docker 版本 $ sudo docker versionClient version: 1.3.2Client API version: 1.15Go version (client): go1.3.3Git commit (client): 39fa2fa/1.3.2OS/Arch (client): linux/amd64Server version: 1.3.2Server API version: 1.1
19、5Go version (server): go1.3.3Git commit (server): 39fa2fa/1.3.2三、Docker 基礎用法 Docker HUB : Docker鏡像首頁,包括官方鏡像和其它公開鏡像 因為國情的原因,國內下載 Docker HUB 官方的相關鏡像比較慢,可以使用 鏡像,鏡像保持和官方一致,關鍵是速度塊,推薦使用。 回到頂部3.1 Search images $ sudo docker search ubuntu回到頂部3.2 Pull images $ sudo docker pull ubuntu # 獲取 ubuntu 官方鏡像 $ sudo
20、docker images # 查看當前鏡像列表 回到頂部3.3 Running an interactive shell $ sudo docker run -i -t ubuntu:14.04 /bin/bash· docker run - 運行一個容器 · -t - 分配一個(偽)tty (link is external) · -i - 交互模式 (so we can interact with it) · ubuntu:14.04 - 使用 ubuntu 基礎鏡像 14.04 · /bin/bash - 運行命令 bash shell
21、 注: ubuntu 會有多個版本,通過指定 tag 來啟動特定的版本 image:tag $ sudo docker ps # 查看當前運行的容器, ps -a 列出當前系統所有的容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES6c9129e9df10 ubuntu:14.04 /bin/bash 6 minutes ago Up 6 minutes cranky_babbage回到頂部3.4 相關快捷鍵 · 退出:Ctrl-Dorexit · detach:Ctrl-P + Ctrl-Q · at
22、tach:docker attach CONTAINER-ID 四、Docker 命令幫助 回到頂部4.1 docker help docker command $ sudo docker # docker 命令幫助Commands: attach Attach to a running container # 當前 shell 下 attach 連接指定運行鏡像 build Build an image from a Dockerfile # 通過 Dockerfile 定制鏡像 commit Create a new image from a container's changes
23、 # 提交當前容器為新的鏡像 cp Copy files/folders from the containers filesystem to the host path # 從容器中拷貝指定文件或者目錄到宿主機中 create Create a new container # 創建一個新的容器,同 run,但不啟動容器 diff Inspect changes on a container's filesystem # 查看 docker 容器變化 events Get real time events from the server # 從 docker 服務獲取容器實時事件 exe
24、c Run a command in an existing container # 在已存在的容器上運行命令 export Stream the contents of a container as a tar archive # 導出容器的內容流作為一個 tar 歸檔文件對應 import history Show the history of an image # 展示一個鏡像形成歷史 images List images # 列出系統當前鏡像 import Create a new filesystem image from the contents of a tarball # 從t
25、ar包中的內容創建一個新的文件系統映像對應 export info Display system-wide information # 顯示系統相關信息 inspect Return low-level information on a container # 查看容器詳細信息 kill Kill a running container # kill 指定 docker 容器 load Load an image from a tar archive # 從一個 tar 包中加載一個鏡像對應 save login Register or Login to the docker registry
26、 server # 注冊或者登陸一個 docker 源服務器 logout Log out from a Docker registry server # 從當前 Docker registry 退出 logs Fetch the logs of a container # 輸出當前容器日志信息 port Lookup the public-facing port which is NAT-ed to PRIVATE_PORT # 查看映射端口對應的容器內部源端口 pause Pause all processes within a container # 暫停容器 ps List conta
27、iners # 列出容器列表 pull Pull an image or a repository from the docker registry server # 從docker鏡像源服務器拉取指定鏡像或者庫鏡像 push Push an image or a repository to the docker registry server # 推送指定鏡像或者庫鏡像至docker源服務器 restart Restart a running container # 重啟運行的容器 rm Remove one or more containers # 移除一個或者多個容器 rmi Remov
28、e one or more images # 移除一個或多個鏡像無容器使用該鏡像才可刪除,否則需刪除相關容器才可繼續或 -f 強制刪除 run Run a command in a new container # 創建一個新的容器并運行一個命令 save Save an image to a tar archive # 保存一個鏡像為一個 tar 包對應 load search Search for an image on the Docker Hub # 在 docker hub 中搜索鏡像 start Start a stopped containers # 啟動容器 stop Stop
29、a running containers # 停止容器 tag Tag an image into a repository # 給源中鏡像打標簽 top Lookup the running processes of a container # 查看容器中運行的進程信息 unpause Unpause a paused container # 取消暫停容器 version Show the docker version information # 查看 docker 版本號 wait Block until a container stops, then print its exit cod
30、e # 截取容器停止時的退出狀態值Run 'docker COMMAND -help' for more information on a command.docker option Usage of docker: -api-enable-cors=false Enable CORS headers in the remote API # 遠程 API 中開啟 CORS 頭 -b, -bridge="" Attach containers to a pre-existing network bridge # 橋接網絡 use 'none'
31、to disable container networking -bip="" Use this CIDR notation address for the network bridge's IP, not compatible with -b # 和 -b 選項不兼容,具體沒有測試過 -d, -daemon=false Enable daemon mode # daemon 模式 -D, -debug=false Enable debug mode # debug 模式 -dns= Force docker to use specific DNS servers
32、# 強制 docker 使用指定 dns 服務器 -dns-search= Force Docker to use specific DNS search domains # 強制 docker 使用指定 dns 搜索域 -e, -exec-driver="native" Force the docker runtime to use a specific exec driver # 強制 docker 運行時使用指定執行驅動器 -fixed-cidr="" IPv4 subnet for fixed IPs (ex: /16) thi
33、s subnet must be nested in the bridge subnet (which is defined by -b or -bip) -G, -group="docker" Group to assign the unix socket specified by -H when running in daemon mode use '' (the empty string) to disable setting of a group -g, -graph="/var/lib/docker" Path to use a
34、s the root of the docker runtime # 容器運行的根目錄路徑 -H, -host= The socket(s) to bind to in daemon mode # daemon 模式下 docker 指定綁定方式tcp or 本地 socket specified using one or more tcp:/host:port, unix:/path/to/socket, fd:/* or fd:/socketfd. -icc=true Enable inter-container communication # 跨容器通信 -insecure-regist
35、ry= Enable insecure communication with specified registries (no certificate verification for HTTPS and enable HTTP fallback) (e.g., localhost:5000 or /16) -ip="" Default IP address to use when binding container ports # 指定監聽地址,默認所有 ip -ip-forward=true Enable net.ipv4.ip_forw
36、ard # 開啟轉發 -ip-masq=true Enable IP masquerading for bridge's IP range -iptables=true Enable Docker's addition of iptables rules # 添加對應 iptables 規則 -mtu=0 Set the containers network MTU # 設置網絡 mtu if no value is provided: default to the default route MTU or 1500 if no default route is availab
37、le -p, -pidfile="/var/run/docker.pid" Path to use for daemon PID file # 指定 pid 文件位置 -registry-mirror= Specify a preferred Docker registry mirror -s, -storage-driver="" Force the docker runtime to use a specific storage driver # 強制 docker 運行時使用指定存儲驅動 -selinux-enabled=false Enable
38、selinux support # 開啟 selinux 支持 -storage-opt= Set storage driver options # 設置存儲驅動選項 -tls=false Use TLS; implied by tls-verify flags # 開啟 tls -tlscacert="/root/.docker/ca.pem" Trust only remotes providing a certificate signed by the CA given here -tlscert="/root/.docker/cert.pem"
39、Path to TLS certificate file # tls 證書文件位置 -tlskey="/root/.docker/key.pem" Path to TLS key file # tls key 文件位置 -tlsverify=false Use TLS and verify the remote (daemon: verify client, client: verify daemon) # 使用 tls 并確認遠程控制主機 -v, -version=false Print version information and quit # 輸出 docker 版
40、本信息回到頂部4.2 docker search $ sudo docker search -helpUsage: docker search TERMSearch the Docker Hub for images # 從 Docker Hub 搜索鏡像 -automated=false Only show automated builds -no-trunc=false Don't truncate output -s, -stars=0 Only displays with at least xxx stars示例: $ sudo docker search -s 100 ubu
41、ntu # 查找 star 數至少為 100 的鏡像,找出只有官方鏡像 start 數超過 100,默認不加 s 選項找出所有相關 ubuntu 鏡像 NAME DESCRIPTION STARS OFFICIAL AUTOMATEDubuntu Official Ubuntu base image 425 OK 回到頂部4.3 docker info $ sudo docker info Containers: 1 # 容器個數 Images: 22 # 鏡像個數 Storage Driver: devicemapper # 存儲驅動 Pool Name: docker-8:17-32212
42、25728-pool Pool Blocksize: 65.54 kB Data file: /data/docker/devicemapper/devicemapper/data Metadata file: /data/docker/devicemapper/devicemapper/metadata Data Space Used: 1.83 GB Data Space Total: 107.4 GB Metadata Space Used: 2.191 MB Metadata Space Total: 2.147 GB Library Version: 1.02.84-RHEL7 (2
43、014-03-26) Execution Driver: native-0.2 # 存儲驅動 Kernel Version: 3.10.0-123.el7.x86_64Operating System: CentOS Linux 7 (Core) 回到頂部4.4 docker pull && docker push $ sudo docker pull -help # pull 拉取鏡像 Usage: docker pull OPTIONS NAME:TAG Pull an image or a repository from the registry -a, -all-tag
44、s=false Download all tagged images in the repository $ sudo docker push # push 推送指定鏡像 Usage: docker push NAME:TAG Push an image or a repository to the registry示例: $ sudo docker pull ubuntu # 下載官方 ubuntu docker 鏡像,默認下載所有 ubuntu 官方庫鏡像 $ sudo docker pull ubuntu:14.04 # 下載指定版本 ubuntu 官方鏡像 $ sudo docker
45、push 00:5000/ubuntu # 推送鏡像庫到私有源可注冊 docker 官方賬戶,推送到官方自有賬戶 $ sudo docker push 00:5000/ubuntu:14.04 # 推送指定鏡像到私有源 回到頂部4.5 docker images 列出當前系統鏡像 $ sudo docker images -helpUsage: docker images OPTIONS NAME List images -a, -all=false Show all images (by default filter out the interme
46、diate image layers) # -a 顯示當前系統的所有鏡像,包括過渡層鏡像,默認 docker images 顯示最終鏡像,不包括過渡層鏡像 -f, -filter= Provide filter values (i.e. 'dangling=true') -no-trunc=false Don't truncate output -q, -quiet=false Only show numeric IDs示例: $ sudo docker images # 顯示當前系統鏡像,不包括過渡層鏡像 $ sudo docker images -a # 顯示當前系
47、統所有鏡像,包括過渡層鏡像 $ sudo docker images ubuntu # 顯示當前系統 docker ubuntu 庫中的所有鏡像 REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZEubuntu 12.04 ebe4be4dd427 4 weeks ago 210.6 MBubuntu 14.04 e54ca5efa2e9 4 weeks ago 276.5 MBubuntu 14.04-ssh 6334d3ac099a 7 weeks ago 383.2 MB回到頂部4.6 docker rmi 刪除一個或者多個鏡像 $ sudo dock
48、er rmi -helpUsage: docker rmi IMAGE IMAGE. Remove one or more images -f, -force=false Force removal of the image # 強制移除鏡像不管是否有容器使用該鏡像 -no-prune=false Do not delete untagged parents # 不要刪除未標記的父鏡像 回到頂部4.7 docker run $ sudo docker run -helpUsage: docker run OPTIONS IMAGE COMMAND ARG. Run a command in a
49、 new container -a, -attach= Attach to stdin, stdout or stderr. -c, -cpu-shares=0 CPU shares (relative weight) # 設置 cpu 使用權重 -cap-add= Add Linux capabilities -cap-drop= Drop Linux capabilities -cidfile="" Write the container ID to the file # 把容器 id 寫入到指定文件 -cpuset="" CPUs in which
50、 to allow execution (0-3, 0,1) # cpu 綁定 -d, -detach=false Detached mode: Run container in the background, print new container id # 后臺運行容器 -device= Add a host device to the container (e.g. -device=/dev/sdc:/dev/xvdc) -dns= Set custom dns servers # 設置 dns -dns-search= Set custom dns search domains # 設
51、置 dns 域搜索 -e, -env= Set environment variables # 定義環境變量 -entrypoint="" Overwrite the default entrypoint of the image # ? -env-file= Read in a line delimited file of ENV variables # 從指定文件讀取變量值 -expose= Expose a port from the container without publishing it to your host # 指定對外提供服務端口 -h, -host
52、name="" Container host name # 設置容器主機名 -i, -interactive=false Keep stdin open even if not attached # 保持標準輸出開啟即使沒有 attached -link= Add link to another container (name:alias) # 添加鏈接到另外一個容器 -lxc-conf= (lxc exec-driver only) Add custom lxc options -lxc-conf="lxc.cgroup.cpuset.cpus = 0,1" -m, -memory="" Memory
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 工程合同毀約賠償協議書
- 足療養生店合同范本
- 落戶和就業協議書
- 幼犬簽購買協議書
- 房裝修售后協議書
- 電動車貸款協議書
- 房子交定金協議書
- 結婚后孩子撫養協議書
- 家具店入股協議書
- 增授權繳費協議書
- 人教版七年級地理下冊 第十章、第十一章 評估測試卷(含解析)
- 消化內科診療指南和技術操作規范
- 2025-2030方塊地毯行業市場現狀供需分析及重點企業投資評估規劃分析研究報告
- 小兒推拿(大全)課件
- 全身麻醉和睡眠
- 科技與文化融合的傳播方式
- 基層武裝工作知識
- 生產異常處理方法及流程
- 廣東省廣州市越秀區2025年中考一模歷史模擬試題(含答案)
- 《小米銷售培訓》課件
- 2025年北京鐵路局集團招聘筆試參考題庫含答案解析
評論
0/150
提交評論