docker-py: 在启动容器之前附加到网络
docker-py: attach to network before starting the container
直到最近,我都可以在启动容器之前将容器连接到网络,如下所示:
params = {
'image': 'busybox',
'name': 'test',
}
result = docker_client.create_container(**params)
docker_client.connect_container_to_network(result['Id'], network_id)
docker_client.start(result['Id'])
但是,现在我总是报错,因为我连接网络时容器还没有启动:
APIError: 500 Server Error: Internal Server Error ("Container 0b1005fc86be565d1a54c44f89f2a60d338b541f8b73805c2a367116bf04a060 is not running")
我可以用命令行客户端重现同样的错误:
% docker create --name test busybox
7aa04b908b2ec45a37f272809ec909116cfae5ae80a13c6596822ca4d9b39a0e
% docker network connect test 7aa04b908b2e
Error response from daemon: Container 7aa04b908b2ec45a37f272809ec909116cfae5ae80a13c6596822ca4d9b39a0e is not running
所以我需要在创建容器的时候直接把容器连上网络:
% docker create --net=test busybox test
在 docker-py
中,如何在创建过程中将容器直接附加到网络?
您可以使用 HostConfig 配置容器所连接的网络:
network_mode
is available since v1.11 and sets the Network mode for the container ('bridge': creates a new network stack for the container on the Docker bridge, 'none': no networking for this container, 'container:[name|id]': reuses another container network stack, 'host': use the host network stack inside the container or any name that identifies an existing Docker network).
例如,您会 运行:
params = {
'image': 'busybox',
'name': 'test',
'host_config': docker_client.create_host_config(network_mode='test')
}
docker_client.create_container(**params)
这会将容器连接到网络 test
。
这样试试:
host_config = client.create_host_config(
network_mode='my-net'
)
networking_config = client.create_networking_config({
'my-net': client.create_endpoint_config(
)
})
container = client.create_container(
image='josepainumkal/vwadaptor:jose_toolUI',
host_config=host_config,
networking_config = networking_config
)
直到最近,我都可以在启动容器之前将容器连接到网络,如下所示:
params = {
'image': 'busybox',
'name': 'test',
}
result = docker_client.create_container(**params)
docker_client.connect_container_to_network(result['Id'], network_id)
docker_client.start(result['Id'])
但是,现在我总是报错,因为我连接网络时容器还没有启动:
APIError: 500 Server Error: Internal Server Error ("Container 0b1005fc86be565d1a54c44f89f2a60d338b541f8b73805c2a367116bf04a060 is not running")
我可以用命令行客户端重现同样的错误:
% docker create --name test busybox
7aa04b908b2ec45a37f272809ec909116cfae5ae80a13c6596822ca4d9b39a0e
% docker network connect test 7aa04b908b2e
Error response from daemon: Container 7aa04b908b2ec45a37f272809ec909116cfae5ae80a13c6596822ca4d9b39a0e is not running
所以我需要在创建容器的时候直接把容器连上网络:
% docker create --net=test busybox test
在 docker-py
中,如何在创建过程中将容器直接附加到网络?
您可以使用 HostConfig 配置容器所连接的网络:
network_mode
is available since v1.11 and sets the Network mode for the container ('bridge': creates a new network stack for the container on the Docker bridge, 'none': no networking for this container, 'container:[name|id]': reuses another container network stack, 'host': use the host network stack inside the container or any name that identifies an existing Docker network).
例如,您会 运行:
params = {
'image': 'busybox',
'name': 'test',
'host_config': docker_client.create_host_config(network_mode='test')
}
docker_client.create_container(**params)
这会将容器连接到网络 test
。
这样试试:
host_config = client.create_host_config(
network_mode='my-net'
)
networking_config = client.create_networking_config({
'my-net': client.create_endpoint_config(
)
})
container = client.create_container(
image='josepainumkal/vwadaptor:jose_toolUI',
host_config=host_config,
networking_config = networking_config
)