Say I have a network called "mynet" and I want to start a container with an IP address bound to 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
.
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)