本文整理匯總了Python中docker.types方法的典型用法代碼示例。如果您正苦於以下問題:Python docker.types方法的具體用法?Python docker.types怎麽用?Python docker.types使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類docker
的用法示例。
在下文中一共展示了docker.types方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: docker_network
# 需要導入模塊: import docker [as 別名]
# 或者: from docker import types [as 別名]
def docker_network(self) -> Iterator[Network]:
"""
Return a Docker network.
"""
client = docker.from_env(version='auto')
ipam_pool = docker.types.IPAMPool(
subnet='172.28.0.0/16',
iprange='172.28.0.0/24',
gateway='172.28.0.254',
)
# We use the default container prefix so that the
# ``minidcos docker clean`` command cleans this up.
prefix = Docker().container_name_prefix
random = uuid.uuid4()
name = '{prefix}-network-{random}'.format(prefix=prefix, random=random)
network = client.networks.create(
name=name,
driver='bridge',
ipam=docker.types.IPAMConfig(pool_configs=[ipam_pool]),
attachable=False,
)
try:
yield network
finally:
network.remove()
示例2: ensure_local_net
# 需要導入模塊: import docker [as 別名]
# 或者: from docker import types [as 別名]
def ensure_local_net(
network_name: str = DOCKER_STARCRAFT_NETWORK,
subnet_cidr: str = SUBNET_CIDR
) -> None:
"""
Create docker local net if not found.
:raises docker.errors.APIError
"""
logger.info(f"checking whether docker has network {network_name}")
ipam_pool = docker.types.IPAMPool(subnet=subnet_cidr)
ipam_config = docker.types.IPAMConfig(pool_configs=[ipam_pool])
networks = docker_client.networks.list(names=DOCKER_STARCRAFT_NETWORK)
output = networks[0].short_id if networks else None
if not output:
logger.info("network not found, creating ...")
output = docker_client.networks.create(DOCKER_STARCRAFT_NETWORK, ipam=ipam_config).short_id
logger.debug(f"docker network id: {output}")