Golang Docker SDK 修改容器 HostConfig 的方法
在 Golang Docker SDK 中,ContainerUpdate 方法确实不支持直接修改 HostConfig。不过,可以通过使用 ContainerUpdateOptions 的 HostConfigPath 字段来指定一个 JSON 文件,该文件包含要应用于容器的新 HostConfig。在这个 JSON 文件中,可以指定要修改的 HostConfig 的任何字段。例如,以下是一个示例 JSON 文件:
{
'Binds': [
'/host/path:/container/path'
],
'PortBindings': {
'80/tcp': [
{
'HostPort': '8080'
}
]
},
'RestartPolicy': {
'Name': 'always'
}
}
然后,将该 JSON 文件的路径传递给 ContainerUpdateOptions 的 HostConfigPath 字段,如下所示:
opts := types.ContainerUpdateOptions{
HostConfigPath: '/path/to/new/hostconfig.json',
}
err := cli.ContainerUpdate(ctx, containerID, opts)
这将使用指定的 JSON 文件中的新 HostConfig 更新容器。注意,这种方法需要您手动编写 JSON 文件,以确保它包含您希望修改的所有 HostConfig 字段。
原文地址: https://www.cveoy.top/t/topic/kFKN 著作权归作者所有。请勿转载和采集!