####查看到docker是否正常運行 ######docker infopython
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全部容器和鏡像的數量,docker使用的執行驅動和存儲驅動,以及docker的基本配置docker 是一個客戶端——服務器框架,它有一個docker程序,既能做爲客戶端,也能做爲服務器端。做爲客戶端時,docker程序向Docker守護進程發送請求(如請求返回守護進程自身的信息),而後再對請求結果進行處理git
####列出鏡像 ######docker imagesgithub
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
鏡像從倉庫下載下來,鏡像保存在倉庫中,倉庫存在於Registry,默認的Registry是由docker公司運營的Registry服務,即docker hubweb
本地鏡像保存在docker宿主機的/var/lib/docker目錄下,每一個鏡像都保存在docker所採用的存儲驅動下,如aufs或devicemapper,在/var/lib/docker/containers目錄下能夠看到全部的容器docker
####拉取鏡像 ######docker pull ${鏡像名}shell
docker會從鏡像倉庫中搜索相關鏡像,若是找到,則下載相關鏡像json
####查找鏡像 ######docker search ${鏡像名}bootstrap
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鏡像ubuntu
返回信息bash
倉庫名
鏡像描述
用戶評價(Stars)——反應出一個鏡像的受歡迎程度
是否官方(Official)
自動構建(Automated)——表示這個鏡像是由Docker Hub的自動構建的
####運行容器 ######docker run -i -t ${鏡像名}: ${tag} /bin/bash
一、告訴docker執行docker run命令
二、-i保證容器中的STDIN是開啓的(持久化輸入是shell的半邊天)
三、-t告訴容器要爲其分配一個僞TTY終端(shell的另外半邊天)
注:能夠使用docker help run 或者man docker run 查看相關標誌參數
四、基於andrew-site:v1.0.0鏡像建立容器
五、docker在新容器中運行/bin/bash命令,啓動一個Bash shell
過程:
(1)docker首先檢查本地是否存在andrew-site:v1.0.0鏡像,若是本地沒有該鏡像的話,那麼docker就會鏈接到Registry,查看是否存在該鏡像
(2)docker一旦找到該鏡像,就會下載該鏡像並將其保存到本地宿主機中
(3)隨後,docker在文件系統內部有這個鏡像建立一個新容器,該容器擁有本身的網絡,IP地址,以及一個用來和宿主機通訊的橋接網絡接口
(4)最後告訴docker在新容器中運行/bin/bash命令,啓動一個Bash shell
####容器命名 docker會爲咱們建立的每個容器自動生成一個隨機的名稱,如 adoring_hopper 若是想爲容器指定一個名稱,能夠使用--name標誌來實現
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#
上述命令會建立一個名爲name_test 的容器,合法的命名包含如下字符:小寫字母a-z,大寫字母A-Z,數字0-9,下劃線、圓點、橫線
正則[a-zA-Z0-9_.-]
容器的命名必須是惟一的。若是咱們建立2個名稱相同的容器,則命名將會失敗
####從新啓動已中止的容器 ######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#
####建立守護進程式的容器 ######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 將容器保持在後臺運行(前提容器里程序不能退出)
####查看容器運行日誌 ######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 會輸出最後幾條日誌項並返回,也能夠使用-f參數監控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 ``` 爲了調試更簡單,咱們還能夠使用-t標誌爲每條日誌項加上時間戳 ######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']
####查看容器內的進程 ######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
####在容器內部運行進程 一、在容器內部運行後臺進程 ######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$
二、在容器內部運行交互命令 ######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#
####中止守護式容器 ######docker stop ${容器名稱}/${容器iD}
wangande@wangande-MS-7808:~/python-workspace/web-site$ sudo docker stop daemon_test daemon_test
注:docker stop 命令會向容器進程發送SIGTERM信號,若是想快速中止能夠使用docker kill發送SIGKILL信號 ####查看容器 ######docker ps (顯示正在運行的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 (顯示最後一個)
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個容器)
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$
####自動重啓docker 因爲某種錯誤致使容器中止運行,能夠經過--restart 標誌,讓docker自動重啓。 --restart 標誌會檢查容器的退出代碼,並根據此來決定是否重啓容器,默認docker不會重啓容器
sudo docker run --restart=always --name daemon_test andrew-site:v1.0.0
--restart 被設置爲always,不管退出代碼是什麼都會重啓 還能夠將標誌設置爲on-failure 這樣當容器退出代碼爲非0時,纔會自動重啓 on-failure還能夠接受可選的重啓次數如:--restart=on-failure:5 ####深刻容器 除了經過docker ps ,還能夠經過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", .......................... } ]
經過-f 或者--format選定查看結果 ######docker inspect -f='{{.State.Running}}' ${容器名稱}/${容器iD}
wangande@wangande-MS-7808:~/python-workspace/web-site$ sudo docker inspect -f='{{.State.Running}}' daemon_test true
返回容器的運行狀態 ######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
同時指定多個容器,並顯示每一個容器的輸出結果 ####刪除容器 若是容器不使用咱們能夠使用docker rm命令刪除它們 ######docker rm ${容器名稱}/${容器iD}
wangande@wangande-MS-7808:~/python-workspace/web-site$ sudo docker rm 9166129e6105 9166129e6105
注:運行中的容器沒法刪除,必須使用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
目前尚未一次刪除全部容器的命令,不過能夠經過以下代碼刪除
sudo docker rm docker ps -a -q
-a 列出全部容器 -q 表示只返回容器的ID而不返回容器的其它信息 這樣咱們就能夠獲得容器的列表,並傳給docker rm命令,達到刪除全部容器目的