docker状态
docker info
该命令会返回所有容器和镜像的数量、docker使用的驱动程序和存储驱动,以及docker的基本配置信息
创建/运行第一个容器
使用docker run
命令创建容器,此命令提供了容器的创建到启动的功能
root@doushuo-virtual-machine:~# docker run -i -t ubuntu /bin/bash
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
ae79f2514705: Pull complete
6f01dc62e444: Download complete
5ad56d5fc149: Pull complete
170e558760e8: Pull complete
395460e233f5: Pull complete
6f01dc62e444: Pull complete
Digest: sha256:506e2d5852de1d7c90d538c5332bd3cc33b9cbd26f6ca653875899c505c82687
Status: Downloaded newer image for ubuntu:latest
root@82a9aa3f8f4d:/#
首先,docker会检查本地是否存在ubuntu镜像,如果没有就会连接官方维护的Docker Hub Registry,查找是否有此镜像,如果有就会下载并保存在本地宿主机上。
随后,docker在文件系统内部用这个镜像创建了一个新容器,它拥有自己的网络、IP地址,以及一个来和宿主机进行通信的桥接网络接口。
- docker run:创建并启动一个容器
- -i :保证容器中的STDIN是开启的,即是前台实时交互的,而不是后台运行
- -t :指示docker要为创建的容器分配一个可交互的伪tty终端
- ubuntu:指示基于什么镜像来创建容器。Ubuntu镜像是一个常备镜像,也称为基础镜像,由docker公司提供,保存在Docker Hub Registry上。
我们可以以这个ubuntu镜像为基础,在此上构建属于自己的镜像。到目前为止,仅仅构建了一个ubuntu容器,并没有增加任何东西。 - /bin/bash:启动了一个Bash shell,代码块中前后的shell不是一个
使用第一个容器
这是一个完整的ubuntu系统,可以用它来做任何事情。容器ID:82a9aa3f8f4d
查看容器主机名
root@82a9aa3f8f4d:~# hostname
82a9aa3f8f4d
检查容器的/etc/hosts文件
root@82a9aa3f8f4d:~# cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2 82a9aa3f8f4d
docker已经在hosts文件中为该容器的IP地址添加了一条主机配置项
查看容器网络接口
root@82a9aa3f8f4d:/# ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:ac:11:00:02
inet addr:172.17.0.2 Bcast:0.0.0.0 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:15165 errors:0 dropped:0 overruns:0 frame:0
TX packets:11515 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:46810862 (46.8 MB) TX bytes:636176 (636.1 KB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
查看进程
root@82a9aa3f8f4d:/# ps -aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.3 18332 3232 ? Ss 13:22 0:00 /bin/bash
root 791 0.0 0.2 34428 2792 ? R+ 13:48 0:00 ps -aux
安装软件包
apt-get update
apt-get install vim
退出容器
root@82a9aa3f8f4d:/# exit
exit
root@doushuo-virtual-machine:~#
返回宿主机,但是容器也随机停止运行了
只有在指定/bin/bash命令处于运行状态的时候,容器才会相应的处于运行状态,但是容器依然存在:
root@doushuo-virtual-machine:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
root@doushuo-virtual-machine:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
82a9aa3f8f4d ubuntu "/bin/bash" About an hour ago Exited (0) About a minute ago determined_hamilton
指定容器可以通过短ID、长ID、容器名称来指定
容器命名
docker run --name test -i -t ubuntu /bin/bash
这样会创建一个名为test的docker容器,以Ubuntu为镜像
重新启动一个容器
退出容器之后,必须要用docker ps -a
才能查看到所有容器,下面来启动一个已经存在的容器:
docker start [docker_name]
docker start [docker_ID]
也可以选择重新启动已经运行的容器:
docker restart [docker_name/docker_ID]
附着到容器上
使用start
或者restart
命令仅仅是启动了容器,并没有与之进行交互。
使用attach
命令重新附着到一个容器的会话上:
root@doushuo-virtual-machine:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d3efdb7fc09c ubuntu "/bin/bash" 14 seconds ago Exited (0) 6 seconds ago doushuo
82a9aa3f8f4d ubuntu "/bin/bash" 5 weeks ago Exited (0) 5 weeks ago determined_hamilton
root@doushuo-virtual-machine:~# docker start 82a9a
82a9a
root@doushuo-virtual-machine:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
82a9aa3f8f4d ubuntu "/bin/bash" 5 weeks ago Up 6 seconds determined_hamilton
root@doushuo-virtual-machine:~# docker attach deter
Error: No such container: deter
root@doushuo-virtual-machine:~# docker attach determined_hamilton
root@82a9aa3f8f4d:/#
创建守护式容器
- 交互式容器,可以进行交互操作等
- 守护式容器,没有交互式会话,非常适合运行服务和应用程序
通常情况下都是使用守护式容器
root@doushuo-virtual-machine:~# docker run --name daemon_dave -d ubuntu /bin/bash -c "while true; do echo hello world; sleep 1;done"
a74c40ecf2bf0059739f5db1d3aa7a6b745d88d517f8683ec4bbd6557a459574
root@doushuo-virtual-machine:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a74c40ecf2bf ubuntu "/bin/bash -c 'whi..." 4 seconds ago Up 4 seconds daemon_dave
root@doushuo-virtual-machine:~#
这里使用了-d
选项,指定容器于后台运行,所有创建之后仅仅在命令行返回了一个docker_ID而不是交互界面。
获取容器日志
只用docker logs
获取容器运行的信息
root@doushuo-virtual-machine:~# docker logs daemon_dave
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
root@doushuo-virtual-machine:~#
还可以加上一些参数:
- -f 动态追踪日志,类似于
tail -f
- -t 加上时间戳查看日志
- --tail n 查看最新的n条日志,如果为0则只查看最新的日志
root@doushuo-virtual-machine:~# docker logs daemon_dave
hello world
hello world
hello world
hello world
hello world
hello world
hello world
^C
root@doushuo-virtual-machine:~#
root@doushuo-virtual-machine:~# docker logs --tail 0 -ft daemon_dave
2017-10-30T08:17:50.523079296Z hello world
2017-10-30T08:17:51.527672075Z hello world
2017-10-30T08:17:52.529261165Z hello world
2017-10-30T08:17:53.531698074Z hello world
2017-10-30T08:17:54.537658244Z hello world
^C
root@doushuo-virtual-machine:~#
docker日志驱动
自docker1.6开始,可以控制docker守护进程和容器的日志驱动了。通过--log-driver
选项来实现,可以在启动守护进程或者执行docker run
时使用
- json-file 默认选项
- syslog 将禁用docker logs命令;将对应容器的日志重定向到syslog中,在启动守护进程时指定该选项,会将所有容器的日志重定向到syslog中
- none 禁用所有容器中的日志
root@doushuo-virtual-machine:~# docker run --log-driver="syslog" --name log_syslog -d ubuntu /bin/bash -c "while; do echo hello world ;sleep 3; done"
c97d942c5b24a5540b0313a7b1a5c272f1a1adbc743b676c0c4a981f9164474d
root@doushuo-virtual-machine:~#
root@doushuo-virtual-machine:~# docker logs log_syslog
Error response from daemon: configured logging driver does not support reading
root@doushuo-virtual-machine:~#
root@doushuo-virtual-machine:~# docker run --log-driver="none" --name log_none -d ubuntu /bin/sh -c "while; do echo hello world; sleep 1; done"
31c2219a7adaf238b737cafb45e78c016e124849c78dfd68a132cba7d35ffa7c
root@doushuo-virtual-machine:~# docker logs log_none
Error response from daemon: configured logging driver does not support reading
root@doushuo-virtual-machine:~#
查看容器内的进程
使用docker top
命令:
root@doushuo-virtual-machine:~# docker start daemon_dave
daemon_dave
root@doushuo-virtual-machine:~# docker top daemon_dave
UID PID PPID C STIME TTY TIME CMD
root 14634 14617 0 16:52 ? 00:00:00 /bin/bash -c while true; do echo hello world; sleep 1;done
root 14664 14634 0 16:52 ? 00:00:00 sleep 1
docker统计信息
docker1.5.0中引入了docker stats
命令,可以查看一个或者多个容器的状态:
root@doushuo-virtual-machine:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
31c2219a7ada ubuntu "/bin/sh -c 'while..." 15 minutes ago Exited (2) 15 minutes ago log_none
c97d942c5b24 ubuntu "/bin/bash -c 'whi..." 17 minutes ago Exited (1) 17 minutes ago log_syslog
a74c40ecf2bf ubuntu "/bin/bash -c 'whi..." About an hour ago Up 10 minutes daemon_dave
d3efdb7fc09c ubuntu "/bin/bash" About an hour ago Exited (0) About an hour ago doushuo
82a9aa3f8f4d ubuntu "/bin/bash" 5 weeks ago Exited (0) About an hour ago determined_hamilton
root@doushuo-virtual-machine:~# docker stats log_none doushuo daemon_dave
CONTAINER CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
log_none 0.00% 0B / 0B 0.00% 0B / 0B 0B / 0B 0
doushuo 0.00% 0B / 0B 0.00% 0B / 0B 0B / 0B 0
daemon_dave 0.06% 444KiB / 976.8MiB 0.04% 4.74kB / 0B 0B / 0B 0
在容器内部运行进程
docker1.3之后,可以通过docker exec
命令在容器内部额外启动新的进程:
- 后台任务
- 交互式任务
后台任务:
root@doushuo-virtual-machine:~# docker exec -d daemon_dave touch /home/doushuo.conf
-d
选项指后台执行,此例会在创建一个空文件:/home/doushuo.conf
交互式任务:
root@doushuo-virtual-machine:~# docker exec -t -i daemon_dave /bin/bash
root@a74c40ecf2bf:/# ps -aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.3 18032 3008 ? Ss 08:52 0:00 /bin/bash -c while true; do echo hello world; sleep 1;done
root 1270 0.0 0.3 18244 3220 ? Ss 09:13 0:00 /bin/bash
root 1289 0.0 0.0 4384 684 ? S 09:14 0:00 sleep 1
root 1290 0.0 0.2 34428 2744 ? R+ 09:14 0:00 ps -aux
root@a74c40ecf2bf:/#
类似于运行交互式容器,-t
和-i
标志为容器创建了tty伪终端并捕捉STDIN。此例会在daemon_dave容器中创建一个新的bash会话,从而进行交互式操作。
停止守护式容器
使用docker stop
命令来停止守护式(后台)容器:
docker stop [docker_name/docker_ID]
docker stop
命令会向容器发送SIGTERM信号,使用docker kill
命令可以快速停止容器,相当于发送SIGKILL信号。
自动重启容器
如果出于某种错误而导致容器停止运行,可以通过--restart
标志,让docker自动重启该容器。--restart
标志会自动检查容器退出代码,由此来决定是否重启容器,默认不会重启机器。此标志是docker1.2.0引入的选项。
docker run --restart=always --name daemon_dave -d ubuntu /bin/sh -c "while; do echo hello world ;sleep 3; done"
无论退出代码是什么,都会重启容器
docker run --restart=on-failure --name daemon_dave -d ubuntu /bin/sh -c "while; do echo hello world ;sleep 3; done"
只有当容器退出代码为非0值的时候,才会重启容器
docker run --restart=on-failure:5 --name daemon_dave -d ubuntu /bin/sh -c "while; do echo hello world ;sleep 3; done"
退出代码非0值,尝试重启容器,最多重启5次。
深入容器
使用docker inspect
命令来获取更多的容器信息:
root@doushuo-virtual-machine:~# docker inspect daemon_dave
[
{
"Id": "a74c40ecf2bf0059739f5db1d3aa7a6b745d88d517f8683ec4bbd6557a459574",
"Created": "2017-10-30T08:10:45.238282111Z",
"Path": "/bin/bash",
"Args": [
"-c",
"while true; do echo hello world; sleep 1;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 14634,
"ExitCode": 0,
"Error": "",
"StartedAt": "2017-10-30T08:52:46.317099001Z",
"FinishedAt": "2017-10-30T08:33:25.94086388Z"
},
"Image": "sha256:747cb2d60bbecbda48aff14a8be5c8b913ca69318a6067e57c697f8a78dda06e",
"ResolvConfPath": "/var/lib/docker/containers/a74c40ecf2bf0059739f5db1d3aa7a6b745d88d517f8683ec4bbd6557a459574/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/a74c40ecf2bf0059739f5db1d3aa7a6b745d88d517f8683ec4bbd6557a459574/hostname",
"HostsPath": "/var/lib/docker/containers/a74c40ecf2bf0059739f5db1d3aa7a6b745d88d517f8683ec4bbd6557a459574/hosts",
"LogPath": "/var/lib/docker/containers/a74c40ecf2bf0059739f5db1d3aa7a6b745d88d517f8683ec4bbd6557a459574/a74c40ecf2bf0059739f5db1d3aa7a6b745d88d517f8683ec4bbd6557a459574-json.log",
"Name": "/daemon_dave",
"RestartCount": 0,
"Driver": "aufs",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "docker-default",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": null,
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DiskQuota": 0,
"KernelMemory": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": -1,
"OomKillDisable": false,
"PidsLimit": 0,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0
},
"GraphDriver": {
"Data": null,
"Name": "aufs"
},
"Mounts": [],
"Config": {
"Hostname": "a74c40ecf2bf",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/bash",
"-c",
"while true; do echo hello world; sleep 1;done"
],
"Image": "ubuntu",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "3305cf8f47e8e04015f4276d7ff58b02ee2224981dc932a0a0ec4590e5277c39",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/3305cf8f47e8",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "6c937be3f6e16fb4036316bb508ccb67434ea1a1913c4fc18e56d00872635eea",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "1aaf32710fa4bd4b51a599ebbbe7a6a2827d8fcd46157a3fc4a3f962ff23e511",
"EndpointID": "6c937be3f6e16fb4036316bb508ccb67434ea1a1913c4fc18e56d00872635eea",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02"
}
}
}
}
]
root@doushuo-virtual-machine:~#
此外还可以用-f
或者--format
标志来标定查看结果:
root@doushuo-virtual-machine:~# docker inspect -f '{{.NetworkSettings.IPAddress}}' daemon_dave
172.17.0.2
root@doushuo-virtual-machine:~#
可以指定多个容器,并标定多个输出结果:
root@doushuo-virtual-machine:~# docker inspect -f '{{.Name}} {{.Args}}' daemon_dave doushuo
/daemon_dave [-c while true; do echo hello world; sleep 1;done]
/doushuo []
root@doushuo-virtual-machine:~#
使用此命令可以查看容器的大部分配置信息,包括名称、命令、网络配置以及这个容器相关文件的具体路径等。
删除容器
如果容器不再使用,使用docker rm
命令来删除:
root@doushuo-virtual-machine:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
31c2219a7ada ubuntu "/bin/sh -c 'while..." 3 hours ago Exited (2) 3 hours ago log_none
c97d942c5b24 ubuntu "/bin/bash -c 'whi..." 3 hours ago Exited (1) 3 hours ago log_syslog
a74c40ecf2bf ubuntu "/bin/bash -c 'whi..." 4 hours ago Up 3 hours daemon_dave
d3efdb7fc09c ubuntu "/bin/bash" 4 hours ago Up 2 hours doushuo
82a9aa3f8f4d ubuntu "/bin/bash" 5 weeks ago Exited (0) 4 hours ago determined_hamilton
root@doushuo-virtual-machine:~# docker rm log_none
log_none
root@doushuo-virtual-machine:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c97d942c5b24 ubuntu "/bin/bash -c 'whi..." 3 hours ago Exited (1) 3 hours ago log_syslog
a74c40ecf2bf ubuntu "/bin/bash -c 'whi..." 4 hours ago Up 3 hours daemon_dave
d3efdb7fc09c ubuntu "/bin/bash" 4 hours ago Up 2 hours doushuo
82a9aa3f8f4d ubuntu "/bin/bash" 5 weeks ago Exited (0) 4 hours ago determined_hamilton
root@doushuo-virtual-machine:~#
注意:自docker1.6.2开始,可以通过docker rm -f
删除所有包括正在运行的容器;在这之前,如果要删除正在运行的容器,要先使用docker kill
或者docker stop
命令停止容器,再进行删除操作。
删除所有容器:
root@doushuo-virtual-machine:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c97d942c5b24 ubuntu "/bin/bash -c 'whi..." 3 hours ago Exited (1) 3 hours ago log_syslog
a74c40ecf2bf ubuntu "/bin/bash -c 'whi..." 4 hours ago Up 3 hours daemon_dave
d3efdb7fc09c ubuntu "/bin/bash" 4 hours ago Up 2 hours doushuo
82a9aa3f8f4d ubuntu "/bin/bash" 5 weeks ago Exited (0) 4 hours ago determined_hamilton
root@doushuo-virtual-machine:~# docker ps -a -q
c97d942c5b24
a74c40ecf2bf
d3efdb7fc09c
82a9aa3f8f4d
root@doushuo-virtual-machine:~# docker rm `docker ps -a -q`
c97d942c5b24
82a9aa3f8f4d
Error response from daemon: You cannot remove a running container a74c40ecf2bf0059739f5db1d3aa7a6b745d88d517f8683ec4bbd6557a459574. Stop the container before attempting removal or force remove
Error response from daemon: You cannot remove a running container d3efdb7fc09cfd37e29c50249a1370701e0b06cd9ccee06dfce362adacb1576a. Stop the container before attempting removal or force remove
root@doushuo-virtual-machine:~#
docker ps -a
列出所有容器,-q
标志仅显示容器的ID;传递给docker rm
意为删除所有容器。有两个正在运行的容器不能删除,加上-f
标志删除所有容器:
root@doushuo-virtual-machine:~# docker rm -f `docker ps -a -q`
a74c40ecf2bf
d3efdb7fc09c
root@doushuo-virtual-machine:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
root@doushuo-virtual-machine:~#
网友评论