Docker Python API Update method generates TypeError: unhashable type: 'dict'

Docker Python API Update method generates TypeError: unhashable type: 'dict'

Python 的 Docker SDK 记录容器对象 class“容器”支持“更新”方法,在文档中描述如下:


更新(**kwargs)
更新容器的资源配置。

参数:
blkio_weight (int) – 块IO(相对权重),在10到1000之间
cpu_period (int) – Limit CPU CFS (Completely Fair Scheduler) 周期
cpu_quota (int) – 限制 CPU CFS(完全公平调度程序)配额
cpu_shares (int) – CPU 股(相对权重)
cpuset_cpus (str) – CPUs 允许执行
cpuset_mems (str) – 允许执行的 MEM
mem_limit (int or str) – 内存限制
mem_reservation (int or str) – 内存软限制
memswap_limit (int or str) – 总内存(内存 + swap),-1 禁用 swap
kernel_memory (int or str) – 内核内存限制

restart_policy (dict) – 重启策略字典


reference in docker python api doc

尝试使用时:

client = docker.from_env()
for container in client.containers.list():
    restart_policy = {'restart_policy',{'MaximumRetryCount': 4, 'Name': 'on-failure'}}
    container.update(restart_policy)

我遇到错误:

restart_policy = {'restart_policy',{'MaximumRetryCount': 4, 'Name': 'on-failure'}}
TypeError: unhashable type: 'dict'

请提供有关如何制定请求的指导!

您应该将该参数作为命名参数传递给函数调用:

container.update(restart_policy={'Name': 'on-failure', 'MaximumRetryCount': 4})

如果出于某种原因需要将其作为单独的字典,则需要使用 **kwargs 语法传递该字典:

kwargs = {
  'restart_policy': {
    'Name': 'on-failure',
    'MaximumRetryCount': 4
  }
}
container.update(**kwargs)

使用逗号而不是冒号,{a, b} 创建 Python set,其中 {'a': b} 创建 dict。没有 **function(args) 将字典作为位置参数传递,其中 function(**args) 扩展字典以传递命名参数。