【趣學程式】Docker之Docker的常用命令

  • 2019 年 12 月 16 日
  • 筆記

Docker的常用命令

底層原理

  • Docker是如何工作的

Docker是一個Client-Server結構的系統,Docker守護進程運行在主機上, 然後通過Socket連接從客戶端訪問,守護進程從客戶端接受命令並管理運行在主機上的容器。容器,是一個運行時環境。

  • 為什麼Docker比VM快

(1)docker有著比虛擬機更少的抽象層。由亍docker不需要Hypervisor實現硬體資源虛擬化,運行在docker容器上的程式直接使用的都是實際物理機的硬體資源。因此在CPU、記憶體利用率上docker將會在效率上有明顯優勢。 (2)docker利用的是宿主機的內核,而不需要Guest OS。因此,當新建一個容器時,docker不需要和虛擬機一樣重新載入一個作業系統內核。然而避免引尋、載入作業系統內核返個比較費時費資源的過程,當新建一個虛擬機時,虛擬機軟體需要載入Guest OS,返個新建過程是分鐘級別的。而docker由於直接利用宿主機的作業系統,則省略了等待過程,因此新建一個docker容器只需要幾秒鐘。

常用命令

幫助命令

查看docker的版本  docker version    查看docker的詳細資訊  docker info    查看docker的幫助資訊  docker --help  

鏡像命令

  • 名詞解釋
  1. REPOSITORY:表示鏡像的倉庫源
  2. TAG:鏡像的標籤
  3. IMAGE ID:鏡像
  4. IDCREATED:鏡像創建時間
  5. SIZE:鏡像大小

同一倉庫源可以有多個 TAG,代表這個倉庫源的不同個版本,我們使用 REPOSITORY:TAG 來定義不同的鏡像。如果你不指定一個鏡像的版本標籤,例如你只使用 ubuntu,docker 將默認使用 ubuntu:latest 鏡像

列出本地主機上的鏡像      docker images [OPTIONS]        OPTIONS說明:      	-a :列出本地所有的鏡像(含中間映像層)      	-q :只顯示鏡像ID。      	--digests :顯示鏡像的摘要資訊      	--no-trunc :顯示完整的鏡像資訊      搜索某個鏡像      docker search [OPTIONS]  某個XXX鏡像名字        在這個網站上搜索的,該網站類似github是docker的倉庫      	https://hub.docker.com      	OPTIONS說明:      		--no-trunc : 顯示完整的鏡像描述      		--automated : 只列出 automated build類型的鏡像;              --help: 顯示幫助    下載鏡像      docker pull 某個XXX鏡像名字  	docker pull 鏡像名字[:TAG]    	註:[:TAG] 為版本號碼,可以不用寫,默認為最新版本 latest        例如:          docker pull tomcat          docker pull centos    刪除鏡像     刪除單個      docker rmi 某個XXX鏡像名字或ID     刪除多個  	docker rmi  -f 鏡像ID     刪除全部      docker rmi -f 鏡像名1:TAG 鏡像名2:TAG      docker rmi -f $(docker images -qa)    

容器命令

有鏡像才能創建容器,這是根本前提(下載一個CentOS鏡像)  	docker pull centos      新建並啟動容器      docker run [OPTIONS] IMAGE [COMMAND] [ARG...]        OPTIONS說明(常用):有些是一個減號,有些是兩個減號            --name="容器新名字": 為容器指定一個名稱;          -d: 後台運行容器,並返回容器ID,也即啟動守護式容器;          -i:以交互模式運行容器,通常與 -t 同時使用;          -t:為容器重新分配一個偽輸入終端,通常與 -i 同時使用;          -P: 隨機埠映射;          -p: 指定埠映射,有以下四種格式                ip:hostPort:containerPort                ip::containerPort                hostPort:containerPort                containerPort    #使用鏡像centos:latest以交互模式啟動一個容器,在容器內執行/bin/bash命令。  docker run -it centos /bin/bash    列出當前所有正在運行的容器      docker ps [OPTIONS]        OPTIONS說明(常用):          -a :列出當前所有正在運行的容器+歷史上運行過的    docker ps -a          -l :顯示最近創建的容器。docker ps -l          -n:顯示最近n個創建的容器。docker ps -n 10          -q :靜默模式,只顯示容器編號。docker ps -q    退出容器      exit    容器停止退出      ctrl+P+Q    容器不停止退出      啟動容器      docker start 容器ID或者容器名    重啟容器      docker restart 容器ID或者容器名    停止容器      docker stop 容器ID或者容器名    強制停止容器      docker kill 容器ID或者容器名    刪除已停止的容器      docker rm 容器ID      一次性刪除多個容器          docker rm -f $(docker ps -a -q)          docker ps -a -q | xargs docker rm        啟動守護式容器  	docker run -d 容器名  查看容器日誌  	docker logs -f -t --tail 容器ID  		*   -t 加入時間戳  		*   -f 跟隨最新的日誌列印  		*   --tail 數字 顯示最後多少條  查看容器內運行的進程  	docker top 容器ID  查看容器內部細節  	docker inspect 容器ID    進入正在運行的容器並以命令行交互  	docker exec -it 容器ID bashShell      	:exec命令可以在宿主主機外對docker的容器進行執行shell命令          :例如:docker exec -it 容器ID  ls -l /    	重新進入docker attach 容器ID  	上述兩個區別  		attach 直接進入容器啟動命令的終端,不會啟動新的進程  		exec 是在容器中打開新的終端,並且可以啟動新的進程    從容器內拷貝文件到主機上  	docker cp  容器ID:容器內路徑 目的主機路徑  

常用命令速查表

命令

es

cn

attach

Attach to a running container

# 當前 shell 下 attach 連接指定運行鏡像

build

Build an image from a Dockerfile

# 通過 Dockerfile 訂製鏡像

commit

Create a new image from a container changes

# 提交當前容器為新的鏡像

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 服務獲取容器實時事件

exec

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

# 從tar包中的內容創建一個新的文件系統映像[對應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 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 containers

# 列出容器列表

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

Remove 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 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 code

# 截取容器停止時的退出狀態值

今天的分享就到這裡啦