I’m writing a python script that checks if a container exists and gets the host interface, if it does not exists, I’m using pylxd to create a container and then get the host interface, code attached below. First time I run the script , if the container is not present, a container is created but I get a key error telling me that host interface key is not present, but if I run the script the second time the host interface is printed. Is this a python behavior? I couldn’t find any resources since I just don’t get on what to search.
container_exists = client.instances.exists("kali2")
if(container_exists):
container_config = client.instances.get("kali2").config
host_interface = container_config["volatile.eth0.host_name"]
else:
print("Container does not exist,Creating a container")
config = {'name': 'kali2', 'source': {'type': 'image',
'mode': 'pull', 'server': "https://images.linuxcontainers.org", 'protocol': 'simplestreams', 'alias': "kali/current/amd64"}, 'profiles': ['default']}
instance = client.instances.create(config, wait=True)
client.instances.get("kali2").start()
container_config = client.instances.get("kali2").config
host_interface = container_config["volatile.eth0.host_name"]