簡(jiǎn)述docker一些常用的操作
- 查看到docker是否正常運(yùn)行
docker info
wangande@ubuntu:~$ sudo docker info
Containers: 82
Images: 37
Server Version: 1.9.1
Storage Driver: aufs
Root Dir: /var/lib/docker/aufs
Backing Filesystem: extfs
Dirs: 201
Dirperm1 Supported: false
Execution Driver: native-0.2
Logging Driver: json-file
Kernel Version: 3.13.0-32-generic
Operating System: Ubuntu 14.04.1 LTS
CPUs: 1
Total Memory: 986.8 MiB
Name: ubuntu
ID: SZML:DAOQ:6BT5:4QJP:KQSX:NBXL:KBQG:HUF5:CVKQ:LIVI:V55U:24BH
返回所有docker所有容器和鏡像的數(shù)量悯仙,docker使用的執(zhí)行驅(qū)動(dòng)和存儲(chǔ)驅(qū)動(dòng)儿咱,以及docker的基本配置docker 是一個(gè)客戶端——服務(wù)器框架卦羡,它有一個(gè)docker程序乍丈,既能作為客戶端鲤妥,也能作為服務(wù)器端庶橱。作為客戶端時(shí),docker程序向Docker守護(hù)進(jìn)程發(fā)送請(qǐng)求(如請(qǐng)求返回守護(hù)進(jìn)程自身的信息)童擎,然后再對(duì)請(qǐng)求結(jié)果進(jìn)行處理
- 列出鏡像
docker images
wangande@wangande-MS-7808:~$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
andrew-site v1.0.0 4e51341b8ec2 4 months ago 628 MB
my v2 f8fc45357f55 4 months ago 504.1 MB
鏡像從倉(cāng)庫(kù)下載下來(lái)滴劲,鏡像保存在倉(cāng)庫(kù)中,倉(cāng)庫(kù)存在于Registry顾复,默認(rèn)的Registry是由docker公司運(yùn)營(yíng)的Registry服務(wù)班挖,即docker hub
本地鏡像保存在docker宿主機(jī)的/var/lib/docker目錄下,每個(gè)鏡像都保存在docker所采用的存儲(chǔ)驅(qū)動(dòng)下芯砸,如aufs或devicemapper萧芙,在/var/lib/docker/containers目錄下可以看到所有的容器
- 拉取鏡像
docker pull ${鏡像名}
docker會(huì)從鏡像倉(cāng)庫(kù)中搜索相關(guān)鏡像,如果找到假丧,則下載相關(guān)鏡像
- 查找鏡像
docker search ${鏡像名}
wangande@ubuntu:~$ sudo docker search ubuntu
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
ubuntu Ubuntu is a Debian-based Linux operating s... 4629 [OK]
ubuntu-upstart Upstart is an event-based replacement for ... 66 [OK]
rastasheep/ubuntu-sshd Dockerized SSH service, built on top of of... 37 [OK]
torusware/speedus-ubuntu Always updated official Ubuntu docker imag... 27 [OK]
ubuntu-debootstrap debootstrap --variant=minbase --components... 25 [OK]
nickistre/ubuntu-lamp LAMP server on Ubuntu 8 [OK]
nuagebec/ubuntu Simple always updated Ubuntu docker images... 8 [OK]
nickistre/ubuntu-lamp-wordpress LAMP on Ubuntu with wp-cli installed 6 [OK]
nimmis/ubuntu This is a docker images different LTS vers... 5 [OK]
maxexcloo/ubuntu Base image built on Ubuntu with init, Supe... 2 [OK]
darksheer/ubuntu Base Ubuntu Image -- Updated hourly 1 [OK]
admiringworm/ubuntu Base ubuntu images based on the official u... 1 [OK]
jordi/ubuntu Ubuntu Base Image 1 [OK]
datenbetrieb/ubuntu custom flavor of the official ubuntu base ... 0 [OK]
lynxtp/ubuntu https://github.com/lynxtp/docker-ubuntu 0 [OK]
dorapro/ubuntu ubuntu image 0 [OK]
webhippie/ubuntu Docker images for ubuntu 0 [OK]
croscon/ubuntu Crosconized Ubuntu 0 [OK]
life360/ubuntu Ubuntu is a Debian-based Linux operating s... 0 [OK]
esycat/ubuntu Ubuntu LTS 0 [OK]
widerplan/ubuntu Our basic Ubuntu images. 0 [OK]
teamrock/ubuntu TeamRock's Ubuntu image configured with AW... 0 [OK]
ustclug/ubuntu ubuntu image for docker with USTC mirror 0 [OK]
konstruktoid/ubuntu Ubuntu base image 0 [OK]
smartentry/ubuntu ubuntu with smartentry 0 [OK]
wangande@ubuntu:~$
在docker hub上查找所有的ubuntu鏡像
返回信息:
倉(cāng)庫(kù)名
鏡像描述
用戶評(píng)價(jià)(Stars)——反應(yīng)出一個(gè)鏡像的受歡迎程度
是否官方(Official)
自動(dòng)構(gòu)建(Automated)——表示這個(gè)鏡像是由Docker Hub的自動(dòng)構(gòu)建的
- 運(yùn)行容器
docker run -i -t ${鏡像名}: ${tag} /bin/bash
1双揪、告訴docker執(zhí)行docker run命令
2、-i保證容器中的STDIN是開(kāi)啟的(持久化輸入是shell的半邊天)
3虎谢、-t告訴容器要為其分配一個(gè)偽TTY終端(shell的另外半邊天)
注:可以使用docker help run 或者man docker run 查看相關(guān)標(biāo)志參數(shù)
4盟榴、基于andrew-site:v1.0.0鏡像創(chuàng)建容器
5、docker在新容器中運(yùn)行/bin/bash命令婴噩,啟動(dòng)一個(gè)Bash shell
過(guò)程:
(1)docker首先檢查本地是否存在andrew-site:v1.0.0鏡像擎场,如果本地沒(méi)有該鏡像的話羽德,那么docker就會(huì)連接到Registry,查看是否存在該鏡像
(2)docker一旦找到該鏡像迅办,就會(huì)下載該鏡像并將其保存到本地宿主機(jī)中
(3)隨后宅静,docker在文件系統(tǒng)內(nèi)部有這個(gè)鏡像創(chuàng)建一個(gè)新容器,該容器擁有自己的網(wǎng)絡(luò)站欺,IP地址姨夹,以及一個(gè)用來(lái)和宿主機(jī)通信的橋接網(wǎng)絡(luò)接口
(4)最后告訴docker在新容器中運(yùn)行/bin/bash命令,啟動(dòng)一個(gè)Bash shell
- 容器命名
docker會(huì)為我們創(chuàng)建的每一個(gè)容器自動(dòng)生成一個(gè)隨機(jī)的名稱矾策,如 adoring_hopper, 如果想為容器指定一個(gè)名稱磷账,可以使用--name標(biāo)志來(lái)實(shí)現(xiàn)
wangande@wangande-MS-7808:~/python-workspace/web-site$ sudo docker run --name name_test -i -t andrew-site:v1.0.0 /bin/bash
root@6eab7fdaab3e:/andrewSite#
上述命令會(huì)創(chuàng)建一個(gè)名為name_test 的容器,合法的命名包含以下字符:小寫字母a-z贾虽,大寫字母A-Z逃糟,數(shù)字0-9,下劃線蓬豁、圓點(diǎn)绰咽、橫線
正則[a-zA-Z0-9_.-]
容器的命名必須是唯一的。如果我們創(chuàng)建2個(gè)名稱相同的容器地粪,則命名將會(huì)失敗
- 重新啟動(dòng)已停止的容器
docker start ${容器名稱}/${容器iD}
wangande@wangande-MS-7808:~/python-workspace/web-site$ sudo docker start name_test
name_test
也可以使用
docker restart ${容器名稱}/${容器iD}
wangande@wangande-MS-7808:~/python-workspace/web-site$ sudo docker restart name_test
name_test
- 附著到容器上
docker attach ${容器名稱}/${容器iD}
wangande@wangande-MS-7808:~/python-workspace/web-site$ sudo docker attach name_test
root@6eab7fdaab3e:/andrewSite#
- 創(chuàng)建守護(hù)進(jìn)程式的容器
docker run -d ${鏡像名}: ${tag}
wangande@wangande-MS-7808:~/python-workspace/web-site$ sudo docker run --name daemon_test -d andrew-site:v1.0.0
991c0927c9b1cb1fb51e34a1ae0cd364dbbadd47d9a5687c7db7363c5170d777
wangande@wangande-MS-7808:~/python-workspace/web-site$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
991c0927c9b1 andrew-site:v1.0.0 "/bin/sh -c 'python /" 7 seconds ago Up 6 seconds 80/tcp daemon_test
-d 將容器保持在后臺(tái)運(yùn)行(前提容器里程序不能退出)
- 查看容器運(yùn)行日志
docker logs ${容器名稱}/${容器iD}
wangande@wangande-MS-7808:~/python-workspace/web-site$ sudo docker logs daemon_test
[E 160906 07:40:16 services_start:33] current server address: 172.17.0.2:80
[I 160906 07:40:16 locale:140] Supported locales: ['en_US', 'ja_JP', 'zh_CN']
docker 會(huì)輸出最后幾條日志項(xiàng)并返回取募,也可以使用-f參數(shù)監(jiān)控docker日志,與tail -f 命令相似
docker logs -f ${容器名稱}/${容器iD}
wangande@wangande-MS-7808:~/python-workspace/web-site$ sudo docker logs -f daemon_test
[E 160906 07:40:16 services_start:33] current server address: 172.17.0.2:80
[I 160906 07:40:16 locale:140] Supported locales: ['en_US', 'ja_JP', 'zh_CN']
也可以跟蹤容器日志的某一片段蟆技,只需要在tail 后面加入-f --lines
如:
(1)docker logs --tail 10 -f ${容器名稱}/${容器iD} 獲取日志的最后十行
wangande@wangande-MS-7808:~/python-workspace/web-site$ sudo docker logs --tail 10 -f daemon_test
[I 160906 07:40:16 locale:140] Supported locales: ['en_US', 'ja_JP', 'zh_CN']
(2)docker logs --tail 0 -f ${容器名稱}/${容器iD} 跟蹤容器的最新日志
wangande@wangande-MS-7808:~/python-workspace/web-site$ sudo docker logs --tail 0 -f daemon_test
為了調(diào)試更簡(jiǎn)單玩敏,我們還可以使用-t標(biāo)志為每條日志項(xiàng)加上時(shí)間戳
docker logs -ft ${容器名稱}/${容器iD}
wangande@wangande-MS-7808:~/python-workspace/web-site$ sudo docker logs -ft daemon_test
2016-09-06T07:40:16.712357005Z [E 160906 07:40:16 services_start:33] current server address: 172.17.0.2:80
2016-09-06T07:40:16.731046798Z [I 160906 07:40:16 locale:140] Supported locales: ['en_US', 'ja_JP', 'zh_CN']
- 查看容器內(nèi)的進(jìn)程
docker top ${容器名稱}/${容器iD}
wangande@wangande-MS-7808:~/python-workspace/web-site$ sudo docker top daemon_test
UID PID PPID C STIME TTY TIME CMD
root 13815 1508 0 15:40 ? 00:00:00 /bin/sh -c python /andrewSite/manage.py --port=80
root 13825 13815 0 15:40 ? 00:00:00 python /andrewSite/manage.py --port=80
- 在容器內(nèi)部運(yùn)行進(jìn)程
在容器內(nèi)部運(yùn)行后臺(tái)進(jìn)程
docker exec -d ${容器名稱}/${容器iD} touch /tmp/new_file
wangande@wangande-MS-7808:~/python-workspace/web-site$ sudo docker exec -d daemon_test touch /tmp/new_file
wangande@wangande-MS-7808:~/python-workspace/web-site$
在容器內(nèi)部運(yùn)行交互命令
docker exec -i -t ${容器名稱}/${容器iD} /bin/bash
wangande@wangande-MS-7808:~/python-workspace/web-site$ sudo docker exec -i -t daemon_test /bin/bash
root@991c0927c9b1:/andrewSite#
- 停止守護(hù)式容器
docker stop ${容器名稱}/${容器iD}
wangande@wangande-MS-7808:~/python-workspace/web-site$ sudo docker stop daemon_test
daemon_test
注:docker stop 命令會(huì)向容器進(jìn)程發(fā)送SIGTERM信號(hào),如果想快速停止可以使用docker kill發(fā)送SIGKILL信號(hào)
- 查看容器
docker ps (顯示正在運(yùn)行的docker)
wangande@wangande-MS-7808:~/python-workspace/web-site$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
991c0927c9b1 andrew-site:v1.0.0 "/bin/sh -c 'python /" 9 minutes ago Up 2 seconds 80/tcp daemon_test
docker ps -a (顯示所有docker付魔,包括已退出)
wangande@wangande-MS-7808:~/python-workspace/web-site$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
991c0927c9b1 andrew-site:v1.0.0 "/bin/sh -c 'python /" 9 minutes ago Up 18 seconds 80/tcp daemon_test
6eab7fdaab3e andrew-site:v1.0.0 "/bin/bash" 25 minutes ago Exited (0) 15 minutes ago name_test
1e700c32e95c i71:5000/isee-video-yixun:v4.1.5.30 "/bin/bash" 10 weeks ago Exited (0) 9 weeks ago desperate_mccarthy
9166129e6105 andrew-site:v1.0.0 "/bin/sh -c 'python /" 11 weeks ago Exited (137) 11 weeks ago daemon1
229d18a74ec5 andrew-site:v1.0.0 "-d -p 20000:80" 11 weeks ago Created daemon
5affe32960c7 andrew-site:v1.0.0 "/bin/bash" 12 weeks ago Exited (0) 12 weeks ago my_name_test
5dc39a01d48b i71:5000/isee-file "/bin/bash" 12 weeks ago Exited (0) 12 weeks ago adoring_hopper
c30c288cccc2 i71:5000/isee-file "/bin/bash" 12 weeks ago Exited (0) 12 weeks ago evil_lichterman
73b7a25539f0 i71:5000/isee-resize "ImageCompress /bin/b" 12 weeks ago Exited (1) 12 weeks ago backstabbing_albattani
04549918fcb4 andrew-site:v1.0.0 "/bin/bash" 3 months ago Exited (0) 12 weeks ago serene_lalande
c89e85466d2f andrew-site:v1.0.0 "/bin/sh -c 'python /" 3 months ago Exited (1) 3 months ago clever_kalam
fbe4e3780598 andrew-site:v1.0.0 "/bin/bash" 3 months ago Exited (130) 3 months ago test-site
0e1a8ff5c52c andrew-site:v1.0.0 "/bin/bash" 4 months ago Exited (0) 4 months ago big_dijkstra
da0b188806a8 andrew-site:v1.0.0 "/bin/bash" 4 months ago Exited (130) 4 months ago jovial_lamarr
98d97845c3d7 i71:5000/isee-sms-yixun:v4.1.4.55 "/bin/sh -c 'python /" 4 months ago Exited (137) 4 months ago stoic_rosalind
7b6b75db6d8d andrew-site:v1.0.0 "/bin/sh -c 'python /" 4 months ago Exited (137) 4 months ago elated_pasteur
6a3ee17b8f5b andrew-site:v1.0.0 "/bin/sh -c 'python /" 4 months ago Created goofy_thompson
98d6fec1de29 andrew-site:v1.0.0 "/bin/sh -c 'python /" 4 months ago Created nostalgic_swanson
c96bce8edcae andrew-site:v1.0.0 "/bin/sh -c 'python /" 4 months ago Created gloomy_goldberg
edbb85621f6e andrew-site:v1.0.0 "/bin/sh -c 'python /" 4 months ago Created stupefied_heisenberg
314d7a7d7fac andrew-site:v1.0.0 "/bin/sh -c 'python /" 4 months ago Exited (137) 4 months ago serene_pike
27cb093780b1 andrew-site:v1.0.0 "/bin/sh -c 'p
docker ps -l (顯示最后一個(gè))
wangande@wangande-MS-7808:~/python-workspace/web-site$ sudo docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
991c0927c9b1 andrew-site:v1.0.0 "/bin/sh -c 'python /" 9 minutes ago Up 39 seconds 80/tcp daemon_test
docker ps -n x(顯示最后x個(gè)容器)
wangande@wangande-MS-7808:~/python-workspace/web-site$ sudo docker ps -n 5
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
991c0927c9b1 andrew-site:v1.0.0 "/bin/sh -c 'python /" 10 minutes ago Up About a minute 80/tcp daemon_test
6eab7fdaab3e andrew-site:v1.0.0 "/bin/bash" 26 minutes ago Exited (0) 16 minutes ago name_test
1e700c32e95c i71:5000/isee-video-yixun:v4.1.5.30 "/bin/bash" 10 weeks ago Exited (0) 9 weeks ago desperate_mccarthy
9166129e6105 andrew-site:v1.0.0 "/bin/sh -c 'python /" 11 weeks ago Exited (137) 11 weeks ago daemon1
229d18a74ec5 andrew-site:v1.0.0 "-d -p 20000:80" 11 weeks ago Created daemon
wangande@wangande-MS-7808:~/python-workspace/web-site$
- 自動(dòng)重啟docker
由于某種錯(cuò)誤導(dǎo)致容器停止運(yùn)行聊品,可以通過(guò)--restart 標(biāo)志,讓docker自動(dòng)重啟几苍。
--restart 標(biāo)志會(huì)檢查容器的退出代碼翻屈,并根據(jù)此來(lái)決定是否重啟容器,默認(rèn)docker不會(huì)重啟容器
sudo docker run --restart=always --name daemon_test andrew-site:v1.0.0
--restart 被設(shè)置為always,無(wú)論退出代碼是什么都會(huì)重啟
還可以將標(biāo)志設(shè)置為on-failure 這樣當(dāng)容器退出代碼為非0時(shí)妻坝,才會(huì)自動(dòng)重啟
on-failure還可以接受可選的重啟次數(shù)如:--restart=on-failure:5
- 深入容器
除了通過(guò)docker ps 伸眶,還可以通過(guò)docker inspect獲取更多的容器信息
docker inspect ${容器名稱}/${容器iD}
wangande@wangande-MS-7808:~/python-workspace/web-site$ sudo docker inspect daemon_test
[
{
"Id": "991c0927c9b1cb1fb51e34a1ae0cd364dbbadd47d9a5687c7db7363c5170d777",
"Created": "2016-09-06T07:40:16.363206933Z",
"Path": "/bin/sh",
"Args": [
"-c",
"python /andrewSite/manage.py --port=80"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 14068,
"ExitCode": 0,
"Error": "",
"StartedAt": "2016-09-06T07:49:33.647550223Z",
"FinishedAt": "2016-09-06T07:48:47.666181288Z"
},
"Image": "4e51341b8ec2db42cf656162884972c58f07ea29cdcbda10696db11984c65191",
..........................
}
]
通過(guò)-f 或者--format選定查看結(jié)果
docker inspect -f='{{.State.Running}}' ${容器名稱}/${容器iD}
wangande@wangande-MS-7808:~/python-workspace/web-site$ sudo docker inspect -f='{{.State.Running}}' daemon_test
true
返回容器的運(yùn)行狀態(tài)
docker inspect --format '{{.Name}} {{.State.Running}}' ${容器名稱}/${容器iD} ${容器名稱}/${容器iD}
wangande@wangande-MS-7808:~/python-workspace/web-site$ sudo docker inspect --format '{{.Name}} {{.State.Running}}' daemon_test name_test
/daemon_test true
/name_test false
同時(shí)指定多個(gè)容器,并顯示每個(gè)容器的輸出結(jié)果
- 刪除容器
如果容器不使用我們可以使用docker rm命令刪除它們
docker rm ${容器名稱}/${容器iD}
wangande@wangande-MS-7808:~/python-workspace/web-site$ sudo docker rm 9166129e6105
9166129e6105
注:運(yùn)行中的容器無(wú)法刪除刽宪,必須使用docker stop 或者docker kill命令停止容器
wangande@wangande-MS-7808:~/python-workspace/web-site$ sudo docker rm 991c0927c9b1
Error response from daemon: Conflict, You cannot remove a running container. Stop the container before attempting removal or use -f
Error: failed to remove containers: [991c0927c9b1]
wangande@wangande-MS-7808:~/python-workspace/web-site$ sudo docker stop 991c0927c9b1
991c0927c9b1
wangande@wangande-MS-7808:~/python-workspace/web-site$
wangande@wangande-MS-7808:~/python-workspace/web-site$
wangande@wangande-MS-7808:~/python-workspace/web-site$ sudo docker rm 991c0927c9b1
991c0927c9b1
目前還沒(méi)有一次刪除所有容器的命令厘贼,不過(guò)可以通過(guò)如下代碼刪除
sudo docker rm `docker ps -a -q`
-a 列出所有容器
-q 表示只返回容器的ID而不返回容器的其它信息
這樣我們就可以得到容器的列表,并傳給docker rm命令圣拄,達(dá)到刪除所有容器目的