对于我的服务,我正在尝试创建一个Python脚本,该脚本从所有正在运行的Docker容器中提取IP地址和端口:
我使用了以下命令:
import os
import json
##1. Find all container ID
container_id = os.popen('docker ps -q').read()
##2. inspect all container
copy_2_json = os.popen('docker container inspect $(docker ps -q)').read()
print(json.loads(copy_2_json))
您能协助我如何进行吗? 我试图将输出保存到文本文件中,但它也无法正常工作。
这是json文件的外观:
[
{
"Id": "bf960500ad8c68d8bbece532bdcb1fed32887ca988216fd6266d7f0b19530a47",
"Created": "2018-09-13T13:27:13.8794826Z",
"Path": "nginx",
"Args": [
"-g",
"daemon off;"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 60064,
"ExitCode": 0,
"Error": "",
"StartedAt": "2018-09-13T13:27:14.7652514Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:c82521676580c4850bb8f0d72e47390a50d60c8ffe44d623ce57be521bca9869",
"ResolvConfPath": "/var/lib/docker/containers/bf960500ad8c68d8bbece532bdcb1fed32887ca988216fd6266d7f0b19530a47/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/bf960500ad8c68d8bbece532bdcb1fed32887ca988216fd6266d7f0b19530a47/hostname",
"HostsPath": "/var/lib/docker/containers/bf960500ad8c68d8bbece532bdcb1fed32887ca988216fd6266d7f0b19530a47/hosts",
"LogPath": "/var/lib/docker/containers/bf960500ad8c68d8bbece532bdcb1fed32887ca988216fd6266d7f0b19530a47/bf960500ad8c68d8bbece532bdcb1fed32887ca988216fd6266d7f0b19530a47-json.log",
"Name": "/nginx",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {
"80/tcp": [
{
"HostIp": "",
"HostPort": "80"
}
]
},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "shareable",
"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": [],
"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": null,
"OomKillDisable": false,
"PidsLimit": 0,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0,
"MaskedPaths": [
"/proc/acpi",
"/proc/kcore",
"/proc/keys",
"/proc/latency_stats",
"/proc/timer_list",
"/proc/timer_stats",
"/proc/sched_debug",
"/proc/scsi",
"/sys/firmware"
],
"ReadonlyPaths": [
"/proc/asound",
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/216d6a31d149a6d05dcfb7ac5e77aa389c42e93196fc3773d8d5cc73e37710e1-init/diff:/var/lib/docker/overlay2/5c59acc979633ac622494ba32369b6dbab5fe8c24b05c9a04acdccebde05f804/diff:/var/lib/docker/overlay2/b5a97ca4f1274cb1947342ff08769d709841469cf91e9dbe61a2c84305dcf951/diff:/var/lib/docker/overlay2/6de13aacf1daf2dcbdf805fe62705175a62504cf0143f334ba4493e86d8e5378/diff",
"MergedDir": "/var/lib/docker/overlay2/216d6a31d149a6d05dcfb7ac5e77aa389c42e93196fc3773d8d5cc73e37710e1/merged",
"UpperDir": "/var/lib/docker/overlay2/216d6a31d149a6d05dcfb7ac5e77aa389c42e93196fc3773d8d5cc73e37710e1/diff",
"WorkDir": "/var/lib/docker/overlay2/216d6a31d149a6d05dcfb7ac5e77aa389c42e93196fc3773d8d5cc73e37710e1/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "bf960500ad8c",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"80/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"NGINX_VERSION=1.15.2-1~stretch",
"NJS_VERSION=1.15.2.0.2.2-1~stretch"
],
"Cmd": [
"nginx",
"-g",
"daemon off;"
],
"ArgsEscaped": true,
"Image": "nginx:latest",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
},
"StopSignal": "SIGTERM"
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "e62d9dc024c4276bd2f5ca00688c6552e2a8bb4d6a382112c84da34acb9951d0",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"80/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "80"
}
]
},
"SandboxKey": "/var/run/docker/netns/e62d9dc024c4",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "d52386c5f54f950ba681ef993eba46338a52588fc2e71c60518eae82385771d3",
"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": "d6c5276cdfe30c701a5f8133a6cbc2244b30cc0d1225990d73c278729f8e5ebf",
"EndpointID": "d52386c5f54f950ba681ef993eba46338a52588fc2e71c60518eae82385771d3",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}
}
}
答案 0 :(得分:1)
这应该可以帮助您。请注意,端口不在我的docker NetworkSettings中,因此我没有添加端口,因为我无法对其进行测试。通常,对于任何字段,您都可以在单个容器上运行docker inspect,查看所需的信息并轻松提取。从json输出中,您可能只需要在{{.IPAddress}}之后添加{{.Port}}。
无论如何,下面的代码示例应能够为您提供所需的一切。如果没有,请告诉我您还有什么要求。在这里,您可以通过称为“输出”的Python字典(变量)来获取信息。
function checkFalseVars($vars){
foreach($vars as $var){
$v = $GLOBALS[$var];
if (!$v){
return $var;
}
}
return false;
}
$value1 = true;
$value2 = false;
$value3 = false;
$vars = array('value1','value2','value3');
if (!$value1 || !$value2 ||!$value3){
return checkFalseVars($vars);
// returns 'value2'
}