这篇文章主要介绍“怎么查看到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是否正常运行”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注天达云网站,小编会继续努力为大家带来更多实用的文章!