常用命令及其詳解
- 幫助命令
-
docker version
:版本信息 -
docker info
:查看docker詳細(xì)信息 -
docker --help
:查看命令幫助信息
- 鏡像命令
-
docker images
:列出本地所有的鏡像(含中間印象層)
參數(shù) | 說明 |
---|---|
REPOSITORY | 表示鏡像的倉(cāng)庫(kù)源 |
TAG | 鏡像的標(biāo)簽 |
IMAGE ID | 鏡像ID |
CREATED | 鏡像創(chuàng)建時(shí)間 |
SIZE | 鏡像大小 |
同一倉(cāng)庫(kù)源可以有多個(gè) TAG,代表這個(gè)倉(cāng)庫(kù)源的不同個(gè)版本,我們使用 REPOSITORY:TAG 來定義不同的鏡像熬的。
如果你不指定一個(gè)鏡像的版本標(biāo)簽刹孔,例如你只使用 ubuntu碟嘴,docker 將默認(rèn)使用 ubuntu:latest 鏡像
docker images [OPTIONS]
常用OPTIONS說明:
-a
:列出本地的所有鏡像鞋既,包括中間印象層-q
:只顯示鏡像的ID--digests
:顯示鏡像的摘要信息--no-trunc
:顯示完整的鏡像信息
-
docker search [某鏡像的名字]
:查找某個(gè)鏡像
注意:即使本地鏡像源配置的是阿里云贮配,但是這個(gè)search也是去docker hub上去查找谍倦,只不過在pull的時(shí)候從阿里云上pull。
docker search [OPTIONS] [某鏡像的名字]
常用OPTIONS說明:
-s
:列出收藏?cái)?shù)(stars)不少于指定值的鏡像--automated
:只列出automated build類型的鏡像--no-trunc
:顯示完整的鏡像信息
注意:在使用-s
時(shí)泪勒,會(huì)出現(xiàn)Flag --stars has been deprecated, use --filter=stars=3 instead昼蛀。也就是說-s已經(jīng)被啟用了宴猾,可以使用docker search --filter stars=x [某個(gè)鏡像的名字]
來代替。同樣--automated
可以使用--filter=is-automated=true
來代替叼旋。
-
docker pull [鏡像名字][:TAG]
:下載某個(gè)鏡像
注意:docker pull tomcat 相當(dāng)于是docker pull tomcat:latest仇哆,不加后面的TAG的話默認(rèn)是latest
-
docker rmi [某個(gè)鏡像的名字/ID]
:刪除單個(gè)某個(gè)鏡像
注意:如果守護(hù)進(jìn)程 daemon正在使用該鏡像,就會(huì)出現(xiàn)以下錯(cuò)誤:
這種情況下夫植,可以使用docker rmi -f [某個(gè)鏡像的名字/ID]
來強(qiáng)制刪除讹剔。
刪除多個(gè):
docker rmi -f [鏡像名1][:TAG] [鏡像名2][:TAG]
刪除全部(組合命令):docker rmi -f $(docker images -qa)
說明:$()
是shell腳本中的變量返回值的賦值
-
docker commit
:提交容器副本,使之成為一個(gè)新的鏡像
此命令就是把一個(gè)鏡像做修改之后使用副本重新提交成一個(gè)新的鏡像详民。相當(dāng)于改裝延欠。具體的使用方式為docker commit -m="提交的描述信息" -a="作者" [容器ID] [要?jiǎng)?chuàng)建的目標(biāo)鏡像名]:[標(biāo)簽名]
- 容器命令
-
docker run + [鏡像名稱]
根據(jù)下面打印的信息,可以看出沈跨,運(yùn)行docker run
首先會(huì)在本地containers中找hello-world容器由捎,如果沒有,再在本地找hello-world的鏡像(images)饿凛,如果還是沒有狞玛,就去遠(yuǎn)程鏡像源(遠(yuǎn)程倉(cāng)庫(kù))去pull。把鏡像pull到本地之后再生產(chǎn)容器去運(yùn)行涧窒。當(dāng)?shù)诙卧龠\(yùn)行該命令時(shí)为居,直接運(yùn)行本地的hello-world容器。
接下來杀狡,運(yùn)行docker pull centos
獲取centos的鏡像蒙畴,下面部分全部使用該鏡像演示容器命令。
-
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
:新建并啟動(dòng)容器
如果本地有呜象,就直接啟動(dòng)膳凝;如果本地沒有,先pull再run恭陡。
相關(guān)OPTIONS說明:
--name="容器新名字"
: 為容器指定一個(gè)名稱蹬音,相當(dāng)于別名-d
: 后臺(tái)運(yùn)行容器,并返回容器ID休玩,也即啟動(dòng)守護(hù)式容器-i
:以交互模式運(yùn)行容器著淆,通常與 -t 同時(shí)使用-t
:為容器重新分配一個(gè)偽輸入終端,通常與 -i 同時(shí)使用-P
: 隨機(jī)端口映射-p
: 指定端口映射拴疤,有以下四種格式
??????ip:hostPort:containerPort
??????ip::containerPort
??????hostPort:containerPort
??????containerPort
命令參數(shù)說明:
docker run -it centos
是啟動(dòng)交互式容器永部,運(yùn)行之后可以看到下圖中shell的交互由docker變成了centos。
docker run -d
啟動(dòng)后centos后再用docker ps -a 發(fā)現(xiàn)容器已經(jīng)退出很重要的要說明的一點(diǎn):Docker容器后臺(tái)運(yùn)行,就必須有一個(gè)前臺(tái)進(jìn)程呐矾。容器運(yùn)行的命令如果不是那些一直掛起的命令(比如運(yùn)行top苔埋,tail),就是會(huì)自動(dòng)退出的蜒犯。
-
docker ps
:列出所有當(dāng)前正在運(yùn)行的容器
上圖沒有指定name熟悉组橄,那么docker會(huì)隨機(jī)分配給其一個(gè)name荞膘,linux下輸入ps是查看那些進(jìn)程在運(yùn)行,輸入docker ps是查看那些容器在運(yùn)行玉工。
相關(guān)OPTIONS說明(常用):
-a
:列出當(dāng)前所有正在運(yùn)行的容器+歷史上運(yùn)行過的-l
:顯示最近創(chuàng)建的容器-n
:顯示最近n個(gè)創(chuàng)建的容器-q
:靜默模式羽资,只顯示容器編號(hào)--no-trunc
:不截?cái)噍敵?/li>
-
exit
:容器停止退出
ctrl + P +Q
:容器不停止退出
當(dāng)停止不退出時(shí),使用docker ps可以查看到centos容器正在運(yùn)行
docker start [容器ID/容器名]
:?jiǎn)?dòng)容器docker restart [容器ID/容器名]
:重新啟動(dòng)容器docker stop[容器ID/容器名]
:停止容器(正常關(guān)機(jī))-
docker rm[容器ID]
:刪除已停止的容器
使用docker ps -n 3查看運(yùn)行過的近三個(gè)容器:
可以看到centos正在運(yùn)行遵班,還有其他兩個(gè)已經(jīng)停止削罩。下面使用docker rm 刪除其中一個(gè)已停止的容器:
可以看到,ID為b78aeba89c42 的容器已經(jīng)被刪除费奸。 docker rm -f $(docker ps -a -q)
:一次性刪除多個(gè)容器
docker ps -a -q | xargs docker rm
:一次性刪除多個(gè)容器(Linux管道)docker logs -f -t -tail [tail numbers] [容器ID]
:顯示容器的日志
相關(guān)OPTIONS說明(常用):
-t
:顯示時(shí)加入時(shí)間戳-f
:跟隨最新的日志打印-tail [numbers]
:顯示最后的多少條
-
docker top [容器ID]
:從查看容器內(nèi)運(yùn)行的進(jìn)程
-
docker inspect [容器ID]
:從查看容器內(nèi)部細(xì)節(jié) -
docker attach [容器ID]
:重新進(jìn)入正在運(yùn)行的容器中弥激,并以命令行進(jìn)行交互,配合上面啟動(dòng)容器后ctrl + P + Q退出后重新進(jìn)入愿阐。
docker exec -it [容器ID] [shell command]
:可以不進(jìn)入容器微服,直接在宿主機(jī)上運(yùn)行容器里的shell command,并返回結(jié)果缨历。
可以看到以蕴,在宿主機(jī)上執(zhí)行centos 里面的ls命令,返回centos的文件列表之后交互的shell還是宿主機(jī)辛孵。
兩種命令的區(qū)別:
attach是直接進(jìn)入容器啟動(dòng)終端命令丛肮,不會(huì)啟動(dòng)新的進(jìn)程,而exec是在容器中打開新的終端魄缚,并可以啟動(dòng)新的進(jìn)程宝与。也就是說,使用docker attach [容器ID]
進(jìn)入容器之后再使用exit退出冶匹,容器就關(guān)閉了习劫。而使用docker exec -it [容器ID] /bin/bash
以bash的方式進(jìn)入容器之后使用exit退出關(guān)閉的只是一個(gè)進(jìn)程,容器還在啟動(dòng)嚼隘。
-
docker cp [容器ID]:[容器內(nèi)的路徑] [目的主機(jī)的路徑]
:從容器中拷貝文件到主機(jī)
命令匯總(陽(yáng)哥太貼心啦)
attach Attach to a running container # 當(dāng)前 shell 下 attach 連接指定運(yùn)行鏡像
build Build an image from a Dockerfile # 通過 Dockerfile 定制鏡像
commit Create a new image from a container changes # 提交當(dāng)前容器為新的鏡像
cp Copy files/folders from the containers filesystem to the host path #從容器中拷貝指定文件或者目錄到宿主機(jī)中
create Create a new container # 創(chuàng)建一個(gè)新的容器诽里,同 run,但不啟動(dòng)容器
diff Inspect changes on a container's filesystem # 查看 docker 容器變化
events Get real time events from the server # 從 docker 服務(wù)獲取容器實(shí)時(shí)事件
exec Run a command in an existing container # 在已存在的容器上運(yùn)行命令
export Stream the contents of a container as a tar archive # 導(dǎo)出容器的內(nèi)容流作為一個(gè) tar 歸檔文件[對(duì)應(yīng) import ]
history Show the history of an image # 展示一個(gè)鏡像形成歷史
images List images # 列出系統(tǒng)當(dāng)前鏡像
import Create a new filesystem image from the contents of a tarball # 從tar包中的內(nèi)容創(chuàng)建一個(gè)新的文件系統(tǒng)映像[對(duì)應(yīng)export]
info Display system-wide information # 顯示系統(tǒng)相關(guān)信息
inspect Return low-level information on a container # 查看容器詳細(xì)信息
kill Kill a running container # kill 指定 docker 容器
load Load an image from a tar archive # 從一個(gè) tar 包中加載一個(gè)鏡像[對(duì)應(yīng) save]
login Register or Login to the docker registry server # 注冊(cè)或者登陸一個(gè) docker 源服務(wù)器
logout Log out from a Docker registry server # 從當(dāng)前 Docker registry 退出
logs Fetch the logs of a container # 輸出當(dāng)前容器日志信息
port Lookup the public-facing port which is NAT-ed to PRIVATE_PORT # 查看映射端口對(duì)應(yīng)的容器內(nèi)部源端口
pause Pause all processes within a container # 暫停容器
ps List containers # 列出容器列表
pull Pull an image or a repository from the docker registry server # 從docker鏡像源服務(wù)器拉取指定鏡像或者庫(kù)鏡像
push Push an image or a repository to the docker registry server # 推送指定鏡像或者庫(kù)鏡像至docker源服務(wù)器
restart Restart a running container # 重啟運(yùn)行的容器
rm Remove one or more containers # 移除一個(gè)或者多個(gè)容器
rmi Remove one or more images # 移除一個(gè)或多個(gè)鏡像[無容器使用該鏡像才可刪除飞蛹,否則需刪除相關(guān)容器才可繼續(xù)或 -f 強(qiáng)制刪除]
run Run a command in a new container # 創(chuàng)建一個(gè)新的容器并運(yùn)行一個(gè)命令
save Save an image to a tar archive # 保存一個(gè)鏡像為一個(gè) tar 包[對(duì)應(yīng) load]
search Search for an image on the Docker Hub # 在 docker hub 中搜索鏡像
start Start a stopped containers # 啟動(dòng)容器
stop Stop a running containers # 停止容器
tag Tag an image into a repository # 給源中鏡像打標(biāo)簽
top Lookup the running processes of a container # 查看容器中運(yùn)行的進(jìn)程信息
unpause Unpause a paused container # 取消暫停容器
version Show the docker version information # 查看 docker 版本號(hào)
wait Block until a container stops, then print its exit code # 截取容器停止時(shí)的退出狀態(tài)值