在上文Docker – 系統(tǒng)整潔之道 – 0中已經(jīng)對(duì)Docker是什么,安裝Docker以及怎么運(yùn)行一個(gè)簡(jiǎn)單的容器有了初步了解,這篇文章介紹Docker的一些命令和Docker鏡像的使用及操作。
Docker的命令按照使用一個(gè)容器的順序進(jìn)行。
能查看到docker信息,說(shuō)明docker是安裝好的。
由于在上文中已經(jīng)運(yùn)行過(guò)一次該條命令,所以u(píng)buntu的鏡像已經(jīng)下載到了本地,此次運(yùn)行就可以使用該鏡像產(chǎn)生一個(gè)容器,在容器啟動(dòng)后,上過(guò)-it
獲取到命令行,運(yùn)行命令ls
。
使用–name標(biāo)志可以給容器定義一個(gè)名字,如 docker run –name this_is_first_ubunt_container -it ubuntu 就會(huì)創(chuàng)建一個(gè)名字叫做this_is_first_ubunt_container的ubuntu的容器。名字只能使用大小寫字母,數(shù)字,下劃線,原點(diǎn)和橫線,即[a-zA-Z0-9_.-]。
關(guān)于docker run的幫助可以使用docker run --help
獲取。
使用docker ps
命令可以看到當(dāng)前正在運(yùn)行的容器有哪些,并給出了一些相應(yīng)的屬性。給命令增加參數(shù)-a
就可以獲取當(dāng)前所有的容器,包括已經(jīng)停止的,如下。
docker ps -n x,顯示最后x個(gè)容器,不管容器正在運(yùn)行還是停止。
docker start docker start this_is_first_ubunt_container
docker attach this_is_first_ubunt_container
創(chuàng)建一個(gè)有名字的container,停止它,啟動(dòng)它。
~ docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES03248ab5d03b tomcat:latest "catalina.sh run" 2 days ago Exited (0) 2 days ago tomcat4d0cc9a960f1 hello-world "/hello" 4 days ago Exited (0) 4 days ago ~ docker run --name this_is_first_ubunt_container -it ubunturoot@894b1f0fa739:/# whoamirootroot@894b1f0fa739:/# exitexit~ docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES894b1f0fa739 ubuntu "/bin/bash" 21 seconds ago Exited (0) 14 seconds ago this_is_first_ubunt_container03248ab5d03b tomcat:latest "catalina.sh run" 2 days ago Exited (0) 2 days ago tomcat4d0cc9a960f1 hello-world "/hello" 4 days ago Exited (0) 4 days ago small_roentgen~ docker start 894b1f0fa739894b1f0fa739~ docker attach 894b1f0fa739root@894b1f0fa739:/# whoamirootroot@894b1f0fa739:/# exitexit~ docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES894b1f0fa739 ubuntu "/bin/bash" About a minute ago Exited (0) 8 seconds ago this_is_first_ubunt_container03248ab5d03b tomcat:latest "catalina.sh run" 2 days ago Exited (0) 2 days ago tomcat4d0cc9a960f1 hello-world "/hello" 4 days ago Exited (0) 4 days ago small_roentgen~ docker start this_is_first_ubunt_containerthis_is_first_ubunt_container~ docker attach this_is_first_ubunt_containerroot@894b1f0fa739:/# whoamirootroot@894b1f0fa739:/# exitexit上面創(chuàng)建的ubuntu是交互式運(yùn)行的容器(interactive container),也可以創(chuàng)建一個(gè)長(zhǎng)期運(yùn)行的容器–守護(hù)式容器(daemonized container)。
~ docker run --name daemon_ubuntu -d ubuntu /bin/sh -c "while true;do echo hello world;sleep 1;done"e56ae29adaf1d27cf49e05bccda5a7214be458fecc2afb0ff7721f16af8e044c~ docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESe56ae29adaf1 ubuntu "/bin/sh -c 'while tr" About a minute ago Up About a minute daemon_ubuntudocker run –restart=always –name daemon_ubuntu -d ubuntu /bin/sh -c “while true;do echo hello world;sleep 1;done”
–restart=always 無(wú)論容器的退出代碼為何,都自動(dòng)重啟。 –restart=on-failure 當(dāng)容器退出代碼不為0時(shí),自動(dòng)重啟。 –restart=on-failure:5 重啟5次。
運(yùn)行中的Docker容器是無(wú)法刪除的。
# 刪除所有容器docker rm `docker ps -a -q`docker images
linux /var/lib/docker Mac $HOME/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/Docker.qcow2
docker pull ubuntu:16.10 獲取ubuntu倉(cāng)庫(kù)中tag為16.10的鏡像。
docker pull -a ubuntu 獲取ubuntu倉(cāng)庫(kù)中所有鏡像。
docker images ubuntu 列出本地所有ubuntu倉(cāng)庫(kù)的鏡像。
docker run 的時(shí)候如果沒(méi)有指定鏡像的版本,則拉取最新版本進(jìn)行創(chuàng)建。
Docker Hub 中倉(cāng)庫(kù)分為兩種,一種是用戶倉(cāng)庫(kù)(user repository),這種是用戶創(chuàng)建使用的的,命名方式為username/repositoryname,意義為用戶名/倉(cāng)庫(kù)名;一種是頂層倉(cāng)庫(kù)(top-repository),由docker內(nèi)部人員管理。
docker search 查找Docker Hub上公用可用鏡像。
獲取鏡像時(shí),格式其實(shí)可以看做 用戶/倉(cāng)庫(kù):標(biāo)簽。 由于很多倉(cāng)庫(kù)為官網(wǎng)所有,所有很多都變成了 倉(cāng)庫(kù):標(biāo)簽,如上面寫的 ubuntu:16.10,ubutnu倉(cāng)庫(kù)的tag為16.10的鏡像。
構(gòu)建鏡像
docker commit docker build 和 Dockerfile文件
一般來(lái)說(shuō),我們不是真正「創(chuàng)建」了一個(gè)鏡像,而是基于一個(gè)已有的鏡像,構(gòu)建了一個(gè)新的鏡像。
|
新聞熱點(diǎn)
疑難解答
圖片精選