我正在尝试升级容器基础映像,并且需要将网络从旧版本移动到新版本。所以我从旧容器中收集数据,其中
网络配置
creation_data.get('networking_config')
看起来像:
{'EndpointsConfig': {'opt_network_4ba465': {'IPAMConfig': {'IPv4Address': '10.49.0.2'}}}}
启动旧容器
def pop_new_container(data, docker_client=None):
container = docker_api.create_container(
# todo: use config file
image='docker.site.org:5000/coaxis/coaxisopt_daemon:latest',
hostname=data.get('hostname'),
ports=[80],
host_config=docker_api.create_host_config(
port_bindings={80: 80},
restart_policy={"MaximumRetryCount": 0, "Name": "always"}
),
networking_config=create_network_config(data, docker_client)
)
docker_api.start(container=container.get('Id'))
return container
升级
启动新容器(基于旧数据)。
def upgrade_daemon_container(old_container_id):
old_container_data = docker_api.inspect_container(old_container_id)
creation_data = get_upgrade_data(old_container_data)
new_container = docker_api.create_container(
image=creation_data.get('image'),
hostname=creation_data.get('hostname'),
volumes=creation_data.get('volumes'),
host_config=docker_api.create_host_config(
binds=creation_data.get('volumes_bindings'),
port_bindings={80: 80},
restart_policy={"MaximumRetryCount": 0, "Name": "always"}
),
networking_config=creation_data.get('networking_config'),
)
return new_container
问题
检查新容器时,网络配置不完整:
旧容器网络配置
{
"opt_network_823dc9": {
"Aliases": [
"2387c3b6b2c2"
],
"EndpointID": "f78a2f64d0eef09c8b965f9e53ea25e45ae5fc0ec977015982106d5a3e364de4",
"Gateway": "10.49.0.202",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAMConfig": {
"IPv4Address": "10.49.0.2"
},
"IPAddress": "10.49.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"Links": null,
"MacAddress": "02:42:0a:31:00:02",
"NetworkID": "474c3b5faa006bbed65d188143f0f34749f1f9e84b25ee0f9629e35b6f588c8a"
}
}
新的容器网络配置
{
"opt_network_823dc9": {
"Aliases": null,
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAMConfig": {
"IPv4Address": "10.49.0.2"
},
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"Links": null,
"MacAddress": "",
"NetworkID": ""
}
}