gpt4 book ai didi

python - 需要检查容器是否在运行

转载 作者:行者123 更新时间:2023-12-02 19:00:14 27 4
gpt4 key购买 nike

我的目的是检查给定的容器是否正在运行。为了执行此操作,请使用docker-py SDK。

cli = docker.APIClient()
cli.inspect_container('festive_bell').items()

我得到的输出如下
[(u 'Platform', u 'linux'), (u 'State', {
u 'Status': u 'running',
u 'Pid': 21095,
u 'OOMKilled': False,
u 'Dead': False,
u 'Paused': False,
u 'Running': True,
u 'FinishedAt': u '2020-04-07T05:44:04.870560348Z',
u 'Restarting': False,
u 'Error': u '',
u 'StartedAt': u '2020-04-07T06:01:38.336911997Z',
u 'ExitCode': 0
}), (u 'Config', {
u 'Tty': False,
u 'Cmd': [u '/usr/sbin/apache2', u '-D', u 'FOREGROUND'],
u 'Volumes': None,
u 'Domainname': u '',
u 'WorkingDir': u '',
u 'Image': u 'clever',
u 'Hostname': u 'ce631de52650',
u 'StdinOnce': False,
u 'ArgsEscaped': True,
u 'Labels': {},
u 'AttachStdin': False,
u 'User': u '',
u 'Env': [u 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin', u 'APACHE_RUN_USER=www-data', u 'APACHE_RUN_GROUP=www-data', u 'APACHE_LOG_DIR=/var/log/apache2', u 'APACHE_PID_FILE=/var/run/apache2/apache2.pid', u 'APACHE_RUN_DIR=/var/run/apache2', u 'APACHE_LOCK_DIR=/var/lock/apache2'],
u 'ExposedPorts': {
u '80/tcp': {}
},
u 'OnBuild': None,
u 'AttachStderr': True,
u 'Entrypoint': None,
u 'AttachStdout': True,
u 'OpenStdin': False
}), (u 'ResolvConfPath', u '/var/snap/docker/common/var-lib-docker/containers/ce631de52650bd33aba64de9e68140910782c31a1909c3e1c94c68ca77706166/resolv.conf'), (u 'HostsPath', u '/var/snap/docker/common/var-lib-docker/containers/ce631de52650bd33aba64de9e68140910782c31a1909c3e1c94c68ca77706166/hosts'), (u 'Args', [u '-D', u 'FOREGROUND']), (u 'Driver', u 'overlay2'), (u 'Path', u '/usr/sbin/apache2'), (u 'HostnamePath', u '/var/snap/docker/common/var-lib-docker/containers/ce631de52650bd33aba64de9e68140910782c31a1909c3e1c94c68ca77706166/hostname'), (u 'RestartCount', 0), (u 'Name', u '/festive_bell'), (u 'Created', u '2020-04-07T05:24:21.897139852Z'), (u 'ExecIDs', None), (u 'GraphDriver', {
u 'Data': {
u 'MergedDir': u '/var/snap/docker/common/var-lib-docker/overlay2/c3af8ddc069aa1c926535d78b0d5c1e07f177536acb45368cc8444f4d502da97/merged',
u 'WorkDir': u '/var/snap/docker/common/var-lib-docker/overlay2/c3af8ddc069aa1c926535d78b0d5c1e07f177536acb45368cc8444f4d502da97/work',
u 'LowerDir': u '/var/snap/docker/common/var-lib-docker/overlay2/c3af8ddc069aa1c926535d78b0d5c1e07f177536acb45368cc8444f4d502da97-init/diff:/var/snap/docker/common/var-lib-docker/overlay2/8138af7982723c810f02f1b707acd275d9261c62d1e6f4c67d6e3589cf83b63f/diff:/var/snap/docker/common/var-lib-docker/overlay2/14fb3837a32f4f3dd05b270c5c811994f4e033625f9dc8f94cfd9f10cbff9966/diff:/var/snap/docker/common/var-lib-docker/overlay2/a23e9c1df228dc61c6c98ab47dd39729b3e9d554a24109727f6aa8d7cefa9df7/diff:/var/snap/docker/common/var-lib-docker/overlay2/94b00ce80624bc73432ff46ed34360add6307440ae9cb83b8719d8049f9cb41a/diff:/var/snap/docker/common/var-lib-docker/overlay2/ac84626aeb7897672ba3ba127c3349b2a1f89e86fff2c471845638b6979ab63b/diff:/var/snap/docker/common/var-lib-docker/overlay2/4812aae566b997946c099442700d04921177dc3a76404cbabe18cc19c0e355b2/diff:/var/snap/docker/common/var-lib-docker/overlay2/38b9a57fff02beffc436c62583b2e0303a28ffecc58a41d45070989cbfa21632/diff:/var/snap/docker/common/var-lib-docker/overlay2/386f2a9643abd098b7a0696efa94c02abcbaeea392e2e80a3370a420735967bd/diff',
u 'UpperDir': u '/var/snap/docker/common/var-lib-docker/overlay2/c3af8ddc069aa1c926535d78b0d5c1e07f177536acb45368cc8444f4d502da97/diff'
},
u 'Name': u 'overlay2'
}), (u 'Mounts', []), (u 'ProcessLabel', u ''), (u 'NetworkSettings', {
u 'Bridge': u '',
u 'Networks': {
u 'bridge': {
u 'NetworkID': u 'c5e2bf0a28cd19b4bf5ab5bb502b81a1643b4557dd2597b0cb33cefc30207142',
u 'MacAddress': u '02:42:ac:11:00:02',
u 'GlobalIPv6PrefixLen': 0,
u 'Links': None,
u 'GlobalIPv6Address': u '',
u 'IPv6Gateway': u '',
u 'DriverOpts': None,
u 'IPAMConfig': None,
u 'EndpointID': u 'e52af7f500ff3656706ee4f6025fc07d6f8ab05d6291e3f962f903291382ac87',
u 'IPPrefixLen': 16,
u 'IPAddress': u '172.17.0.2',
u 'Gateway': u '172.17.0.1',
u 'Aliases': None
}
},
u 'SecondaryIPv6Addresses': None,
u 'LinkLocalIPv6Address': u '',
u 'HairpinMode': False,
u 'IPv6Gateway': u '',
u 'SecondaryIPAddresses': None,
u 'SandboxID': u '20eb8b028235cdefbca4567b1514eb36938467d3ccd3e5b36bcd3a587e98f10e',
u 'MacAddress': u '02:42:ac:11:00:02',
u 'GlobalIPv6Address': u '',
u 'Gateway': u '172.17.0.1',
u 'LinkLocalIPv6PrefixLen': 0,
u 'EndpointID': u 'e52af7f500ff3656706ee4f6025fc07d6f8ab05d6291e3f962f903291382ac87',
u 'SandboxKey': u '/var/snap/docker/423/run/docker/netns/20eb8b028235',
u 'GlobalIPv6PrefixLen': 0,
u 'IPPrefixLen': 16,
u 'IPAddress': u '172.17.0.2',
u 'Ports': {
u '80/tcp': [{
u 'HostPort': u '9091',
u 'HostIp': u '0.0.0.0'
}]
}
}), (u 'AppArmorProfile', u 'docker-default'), (u 'Image', u 'sha256:dbce8a6db15151a8d96fbdd7abe60a9e0e5d628f33d7cc9923856316b365ac4c'), (u 'LogPath', u '/var/snap/docker/common/var-lib-docker/containers/ce631de52650bd33aba64de9e68140910782c31a1909c3e1c94c68ca77706166/ce631de52650bd33aba64de9e68140910782c31a1909c3e1c94c68ca77706166-json.log'), (u 'HostConfig', {
u 'CpuPeriod': 0,
u 'MemorySwappiness': None,
u 'ContainerIDFile': u '',
u 'KernelMemory': 0,
u 'Memory': 0,
u 'CpuQuota': 0,
u 'UsernsMode': u '',
u 'AutoRemove': False,
u 'BlkioDeviceReadIOps': None,
u 'Dns': [],
u 'ExtraHosts': None,
u 'PidsLimit': 0,
u 'DnsSearch': [],
u 'Privileged': False,
u 'IOMaximumIOps': 0,
u 'CpuPercent': 0,
u 'NanoCpus': 0,
u 'Ulimits': None,
u 'CpusetCpus': u '',
u 'DiskQuota': 0,
u 'CgroupParent': u '',
u 'BlkioWeight': 0,
u 'MemorySwap': 0,
u 'RestartPolicy': {
u 'MaximumRetryCount': 0,
u 'Name': u 'no'
},
u 'OomScoreAdj': 0,
u 'BlkioDeviceReadBps': None,
u 'VolumeDriver': u '',
u 'ReadonlyRootfs': False,
u 'CpuShares': 0,
u 'PublishAllPorts': False,
u 'MemoryReservation': 0,
u 'BlkioWeightDevice': [],
u 'ConsoleSize': [0, 0],
u 'NetworkMode': u 'default',
u 'BlkioDeviceWriteBps': None,
u 'Isolation': u '',
u 'GroupAdd': None,
u 'ReadonlyPaths': [u '/proc/bus', u '/proc/fs', u '/proc/irq', u '/proc/sys', u '/proc/sysrq-trigger'],
u 'CpuRealtimeRuntime': 0,
u 'Devices': [],
u 'BlkioDeviceWriteIOps': None,
u 'Binds': None,
u 'CpusetMems': u '',
u 'Cgroup': u '',
u 'UTSMode': u '',
u 'PidMode': u '',
u 'Runtime': u 'runc',
u 'VolumesFrom': None,
u 'CapDrop': None,
u 'DnsOptions': [],
u 'ShmSize': 67108864,
u 'Links': None,
u 'CpuRealtimePeriod': 0,
u 'IpcMode': u 'shareable',
u 'MaskedPaths': [u '/proc/asound', u '/proc/acpi', u '/proc/kcore', u '/proc/keys', u '/proc/latency_stats', u '/proc/timer_list', u '/proc/timer_stats', u '/proc/sched_debug', u '/proc/scsi', u '/sys/firmware'],
u 'PortBindings': {
u '80/tcp': [{
u 'HostPort': u '9091',
u 'HostIp': u ''
}]
},
u 'SecurityOpt': None,
u 'CapAdd': None,
u 'CpuCount': 0,
u 'DeviceCgroupRules': None,
u 'OomKillDisable': False,
u 'LogConfig': {
u 'Config': {},
u 'Type': u 'json-file'
},
u 'IOMaximumBandwidth': 0
}), (u 'Id', u 'ce631de52650bd33aba64de9e68140910782c31a1909c3e1c94c68ca77706166'), (u 'MountLabel', u '')]

我需要从字典中获取“正在运行”字段的值。用于检查docker是否正在运行。我想知道如何找回它们
def checkContainerStatus(containers):
container_status = {}
active=[]
inactive=[]
not_found=[]
if containers:
for container in containers:
inspect_dict = cli.inspect_container('festive_bell')
state = inspect_dict['State']
is_running = state['Status'] == 'running'
if is_running:
active.append(container)
else:
inactive.append(container)
container_status= {'active':active,'inactive':inactive,'not_found':not_found }
print(container_status)

是否可以对以上代码执行字典理解

最佳答案

您想要由State给出的inspect字段,该字段应类似于:

"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 10268,
"ExitCode": 0,
"Error": "",
"StartedAt": "2020-04-07T06:56:19.940744112Z",
"FinishedAt": "0001-01-01T00:00:00Z"
}

您可以使用 StatusRunning检查容器是否正在运行:
cli = docker.APIClient()
inspect_dict = cli.inspect_container('festive_bell')
state = inspect_dict['State']

is_running = state['Status'] == 'running'
# or
is_running = state['Running'] == 'true'

if is_running:
print("My container is running!")

关于python - 需要检查容器是否在运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61074074/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com