您好,登錄后才能下訂單哦!
這篇文章主要介紹“怎么查看到docker是否正常運行”,在日常操作中,相信很多人在怎么查看到docker是否正常運行問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”怎么查看到docker是否正常運行”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
####查看到docker是否正常運行 ######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所有容器和鏡像的數量,docker使用的執行驅動和存儲驅動,以及docker的基本配置docker 是一個客戶端——服務器框架,它有一個docker程序,既能作為客戶端,也能作為服務器端。作為客戶端時,docker程序向Docker守護進程發送請求(如請求返回守護進程自身的信息),然后再對請求結果進行處理
####列出鏡像 ######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
鏡像從倉庫下載下來,鏡像保存在倉庫中,倉庫存在于Registry,默認的Registry是由docker公司運營的Registry服務,即docker hub
本地鏡像保存在docker宿主機的/var/lib/docker目錄下,每個鏡像都保存在docker所采用的存儲驅動下,如aufs或devicemapper,在/var/lib/docker/containers目錄下可以看到所有的容器
####拉取鏡像 ######docker pull ${鏡像名}
docker會從鏡像倉庫中搜索相關鏡像,如果找到,則下載相關鏡像
####查找鏡像 ######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鏡像
返回信息
倉庫名
鏡像描述
用戶評價(Stars)——反應出一個鏡像的受歡迎程度
是否官方(Official)
自動構建(Automated)——表示這個鏡像是由Docker Hub的自動構建的
####運行容器 ######docker run -i -t ${鏡像名}: ${tag} /bin/bash
1、告訴docker執行docker run命令
2、-i保證容器中的STDIN是開啟的(持久化輸入是shell的半邊天)
3、-t告訴容器要為其分配一個偽TTY終端(shell的另外半邊天)
注:可以使用docker help run 或者man docker run 查看相關標志參數
4、基于andrew-site:v1.0.0鏡像創建容器
5、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
####在容器內部運行進程 1、在容器內部運行后臺進程 ######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$
2、在容器內部運行交互命令 ######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命令,達到刪除所有容器目的
到此,關于“怎么查看到docker是否正常運行”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。