Docker 經常使用命令

1、經常使用命令

一、顯示 Docker 版本信息。

docker version

二、顯示 Docker 系統信息,包括鏡像和容器數

docker info

三、幫助

docker --help

2、鏡像命令

一、docker images

1)、含義
列出本地主機上的鏡像mysql

2)、說明nginx

參數說明
一、REPOSITORY:表示鏡像的倉庫源
二、TAG:鏡像的標籤
三、IMAGE ID:鏡像ID
四、CREATED:鏡像建立時間
五、SIZE:鏡像大小

OPTIONS 說明
-a :列出本地全部的鏡像(含中間映像層,默認狀況下,過濾掉中間映像層);
--digests :顯示鏡像的摘要信息;
-f :顯示知足條件的鏡像;
--format :指定返回值的模板文件;
--no-trunc :顯示完整的鏡像信息;
-q :只顯示鏡像IDweb

二、docker search

1)、含義
從Docker Hub查找鏡像
2)、示例

3)、說明sql

OPTIONS 說明
--automated :只列出 automated build類型的鏡像;
--no-trunc :顯示完整的鏡像描述;
-s :列出收藏數不小於指定值的鏡像

三、docker pull

1)、含義
下載鏡像
2)、示例docker

docker pull mysql

四、docker rmi

1)、含義
刪除鏡像
2)、演示shell

刪除單個鏡像 : docker rmi -f 鏡像ID
刪除多個鏡像:docker rim -f 鏡像ID1 鏡像ID2
刪除所有鏡像:docker rmi -f $(docker images -qa)

3、容器命令

一、docker pull centos


有鏡像才能建立容器centos

二、docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

新建並啓動容器

1)、OPTIONS 說明bash

--name="容器新名字": 爲容器指定一個名稱;
-d: 後臺運行容器,並返回容器ID,也即啓動守護式容器;
-i:以交互模式運行容器,一般與 -t 同時使用;
-t:爲容器從新分配一個僞輸入終端,一般與 -i 同時使用;
-P: 隨機端口映射;
-p: 指定端口映射,有如下四種格
         ip:hostPort:containerPort
         ip::containerPort
           hostPort:containerPort
           containerPort

2)、啓動交互式容器
服務器

使用鏡像centos:latest以交互模式啓動一個容器,在容器內執行/bin/bash命令。ide

docker run -it centos /bin/bash

三、docker ps[OPTIONS]

一、含義
列出當前全部正在運行的容器
二、OPTIONS 說明

OPTIONS說明(經常使用):
-a :列出當前全部正在運行的容器+歷史上運行過的
-l :顯示最近建立的容器。
-n:顯示最近n個建立的容器。
-q :靜默模式,只顯示容器編號。
--no-trunc :不截斷輸出。

四、退出容器

一、容器中止退出

exit

二、容器不中止退出

crl+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 容器名

使用鏡像 centos:latest 之後臺模式啓動一個容器

docker run -d centos

問題:而後 docker ps -a 進行查看, 會發現容器已經退出
很重要的要說明的一點: Docker 容器後臺運行,就必須有一個前臺進程.
容器運行的命令若是不是那些一直掛起的命令(好比運行top,tail),就是會自動退出的。

這個是docker的機制問題,好比你的 web 容器,咱們以 nginx
爲例,正常狀況下,咱們配置啓動服務只須要啓動響應的
service 便可。例如

service nginx start

可是,這樣作, nginx 爲後臺進程模式運行,就致使 docker 前臺沒有運行的應用,
這樣的容器後臺啓動後,會當即自殺由於他以爲他沒事可作了.
因此,最佳的解決方案是,將你要運行的程序之前臺進程的形式運行
二、查看容器日誌

docker ligs -f -t tail 容器ID

含義:
  • -t 是加入時間戳
  • -f 跟隨最新的日誌打印
  • --tail 數字 顯示最後多少條

三、查看容器內運行的進程

docker top 容器ID

四、查看容器內部細節

docker inspect 容器ID

五、進行正在運行的容器並以命令行交互

docker exec -it 容器ID bashShell

docker attach 容器ID

attach 直接進入容器啓動命令的終端,不會啓動新的進程
exec 是在容器中打開新的終端,而且能夠啓動新的進程

六、從容器內拷貝文件到主機上

docker cp 容器ID:容器內路徑目標主機路徑

4、總結

命令 含義 含義
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 # 截取容器中止時的退出狀態值

本文做者:魂皓軒 歡迎關注公衆號掃碼關注

本人保留全部權益,轉載請註明出處。 歡迎有故事、有想法的朋友和我分享,可發送至 e-mail: lwqforit@163.com

相關文章
相關標籤/搜索