Docker 常用命令
當下技術都會形成客戶端-服務端的一種形式,需要我們通過客戶端的命令來驅使服務端執行相應的操作,所以需要學習相關的命令。例如Redis命令 等。
一、幫助命令
- docker version
- docker info
- docker --help
二、鏡像命令
以Docker的Logo理解鏡像與容器的關係
藍色的大海 ---宿主機系統
鯨魚 --- Docker
集裝箱 --- 容器實例(來自我們的鏡像模版)
-
docker imagers
列出本地主機上的鏡像
說明:
REPOSITORY:表示鏡像的倉庫源 TAG:鏡像的標籤 IMAGE ID:鏡像ID CREATED:鏡像創建時間 SIZE:鏡像大小
同一倉庫源可以有多個 TAG,代表這個倉庫源的不同個版本,我們使用 REPOSITORY:TAG 來定義不同的鏡像。
如果你不指定一個鏡像的版本標籤,例如你只使用 ubuntu,docker 將默認使用 ubuntu:latest 鏡像OPTIONS說明:
-a :列出本地所有的鏡像(含中間映像層)
-q :只顯示鏡像ID
–digests :顯示鏡像的摘要信息
–no-trunc :顯示完整的鏡像信息 -
docker search 某個鏡像的名字
去docker hub 上查找某個鏡像,查是從docker hub上查,拉取是從配置的阿里雲上拉取
命令:docker search [OPTIONS] 鏡像名字
OPTIONS說明:
--no-trunc : 顯示完整的鏡像描述 -s : 列出收藏數不小於指定值的鏡像。(常用) --automated : 只列出 automated build類型的鏡像;
-
docker pull 某個XXX鏡像名字
命令:docker pull 鏡像名字[:TAG]
-
docker rmi 某個XXX鏡像名字ID
刪除鏡像
刪除單個 docker rmi -f 鏡像ID 刪除多個 docker rmi -f 鏡像名1:TAG 鏡像名2:TAG 刪除全部 docker rmi -f $(docker images -qa)
三、容器命令
有鏡像才能創建容器,這個是根本前提。(下載一個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
docker run -it 啓動交互式容器
-
列出當前所有正在運行的容器
docker -ps
-
退出容器
exit 容器停止,退出
ctrl+P+Q 容器不停止,退出
-
啓動容器
docker start 容器ID
-
重啓容器
docker restart 容器ID
-
停止容器
docker stop 容器ID
-
強制停止容器
docker kill 容器ID
-
刪除已經停止的容器
docker rm 容器ID
強制刪除: -f
批量刪除:docker rm $(docker ps -qa)
四、小結
-
docker常用命令
命令 | 含義 | 翻譯 |
---|---|---|
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 | # 截取容器停止時的退出狀態值 |