2019独角兽企业重金招聘Python工程师标准>>>
1. 依附容器
docker attach big_poincare
只能依附前台型容器,后台型容器是无法依附终端的,因为它本身就不接受用户交互输入。
2. 查看容器日志
docker logs -f deamon_logs :滚动查看日志(tail -f)
docker logs --tail=5 deamon_logs :查看最新5行日志(tail -n 5)
docker logs -f --tail=5 deamon_logs : 查看最新五行,并监视最新的日志
docker logs -f -t deamon_logs : 添加-t标志可以打印产生日志的时刻
3. 查看容器进程
docker top deamon_logs
4. 容器内执行命令(不自己总结了,书上写的很清楚)
在容器启动的时候,通常需要指定其需要执行的程序,然而有时候我们需要在容器运行之后中途启动另一个程序。从Docker 1.3开始,我们可以用docker exec命令在容器中运行新的任务,它可以创建两种任务:后台型任务和交互型任务。后台型任务没有用户交互终端,交互型任务具有和用户交互的输入输出终端。
让我们看一个后台型任务的例子:
$ sudo docker exec -d daemon_dave touch /etc/new_config_file
这里-d标志表示要运行一个后台型任务。接着需要指定要运行命令的容器名和要运行的命令。在这个例子里,touch命令会在daemon_dave容器中创建一个new_config_file的文件。通过docker exec创建的后台任务,我们可以执行维护、监视、管理等复杂而有意义的任务。
使用docker exec命令创建交互型任务时,需要加上-t -i标志,示例如下:
$ sudo docker exec -t -i daemon_dave /bin/bash
-t和-i标志的用法与我们创建交互型容器时一样,会创建一个交互终端,并捕捉进程的标准输入和输出。通过该交互终端,我们可以在容器内运行命令和查看信息等。
5. 查看容器信息
以下是我个人电脑上的例子:
docker inspect deamon_logs
[
{
"Id": "870412fbfd3dae8ea4a44b6b00583eb486eb43fded07b6972f9ecc85cc0db710",
"Created": "2016-03-23T08:51:17.723809515Z",
"Path": "/bin/bash",
"Args": [
"-c",
"for((i=0;1;i++));do echo $i;sleep 1;done;"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 26897,
"ExitCode": 0,
"Error": "",
"StartedAt": "2016-03-23T08:51:17.861253283Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:d0e7f81ca65cdd391b6eb3dd3ce2454a575023156cd932ee4a58f188436bc5e0",
"ResolvConfPath": "/mnt/sda1/var/lib/docker/containers/870412fbfd3dae8ea4a44b6b00583eb486eb43fded07b6972f9ecc85cc0db710/resolv.conf",
"HostnamePath": "/mnt/sda1/var/lib/docker/containers/870412fbfd3dae8ea4a44b6b00583eb486eb43fded07b6972f9ecc85cc0db710/hostname",
"HostsPath": "/mnt/sda1/var/lib/docker/containers/870412fbfd3dae8ea4a44b6b00583eb486eb43fded07b6972f9ecc85cc0db710/hosts",
"LogPath": "/mnt/sda1/var/lib/docker/containers/870412fbfd3dae8ea4a44b6b00583eb486eb43fded07b6972f9ecc85cc0db710/870412fbfd3dae8ea4a44b6b00583eb486eb43fded07b6972f9ecc85cc0db710-json.log",
"Name": "/deamon_logs",
"RestartCount": 0,
"Driver": "aufs",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"ShmSize": 67108864,
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": null,
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"KernelMemory": 0,
"Memory": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": -1,
"OomKillDisable": false,
"PidsLimit": 0,
"Ulimits": null
},
"GraphDriver": {
"Name": "aufs",
"Data": null
},
"Mounts": [],
"Config": {
"Hostname": "870412fbfd3d",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": null,
"Cmd": [
"/bin/bash",
"-c",
"for((i=0;1;i++));do echo $i;sleep 1;done;"
],
"Image": "centos",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"build-date": "2016-03-04",
"license": "GPLv2",
"name": "CentOS Base Image",
"vendor": "CentOS"
},
"StopSignal": "SIGTERM"
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "3e90239e760219fa9f0d72d112e8ad0bfb060d6c06b53bb0dc8a95db58714a3f",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/3e90239e7602",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "00a8a8bc2a5e3f6e8621df6e0958f1889dbca71c581ba9baf50b69a960ea898b",
"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": "b0aeedb86f56f2669dc6f4949d099c975a858595b626bd7b80f028ea2318015c",
"EndpointID": "00a8a8bc2a5e3f6e8621df6e0958f1889dbca71c581ba9baf50b69a960ea898b",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02"
}
}
}
}
]
docker inspect --format='{{ .State.Running }}' deamon_logs
参考文档:《Docker开发实践》第二章2.2 《容器内信息获取和命令执行》