[英]docker-py: How to bind an IP address to a container
Say I have a network called "mynet" and I want to start a container with an IP address bound to 192.168.23.2. 假设我有一个名为“mynet”的网络,我想启动一个IP地址绑定到192.168.23.2的容器。
The code I'm starting with is: 我开始的代码是:
import docker
c = docker.from_env()
c.containers.run('containername', 'sh some_script.sh', network='mynet')
What do I do from here? 我该怎么办? I'm effectively looking for the equivalent to the --ip
option from docker run
. 我正在寻找与--ip
docker run
相同的--ip
选项。
You need to create a network and connect the container to it: 您需要创建一个网络并将容器连接到它:
container = c.containers.run('containername', 'sh some_script.sh')
ipam_pool = docker.types.IPAMPool(
subnet='192.168.23.0/24',
gateway='192.168.23.1'
)
ipam_config = docker.types.IPAMConfig(
pool_configs=[ipam_pool]
)
mynet= c.network.create(
"network1",
driver="bridge",
ipam=ipam_config
)
ip = {"ipv4_address": "192.168.23.2"}
mynet.connect(container,ip)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.