Hi! Could you explain me how to run container on a specific IP?
If I run from console I execute this command:
docker run -d --name iperf_srv --network mynet --ip 10.195.10.10 astotal/iperf3 -s
So how can reach the same result with python API?
Container's entrypoint is iperf, so I cant run container and then connect the network.
Is there way to run container with IP at once instead of create and connect network to it?
I found solution with a few inter steps
srv = doc.containers.create('astotal/iperf3', '-s', detach=True, name='iperf_server')
doc.networks.get("uplink").connect(srv, ipv4_address="10.195.10.11")
srv.start()
Thanks a lot!
@AStotal I tried your solution but got NotFound: 404 Client Error: Not Found ("network uplink not found"). Can you imagine why?
You need to create the network first.
Ah. Thought it's probably created implicitly. Do I have to create the network with subnet configuration (imap_pool)?
@fkromer Yes you do. E.g:
import docker
doc = docker.from_env()
ipam_pool = docker.types.IPAMPool(
subnet='10.2.0.0/24',
iprange='10.2.0.0/24',
gateway='10.2.0.2',
aux_addresses={
'host_system': '10.2.0.1',
}
)
ipam_config = docker.types.IPAMConfig(
pool_configs=[ipam_pool])
# uplink
doc.networks.create('uplink',
driver='macvlan',
options={'parent': 'eth2.3200'},
ipam=ipam_config,
)
I have tried the way above, the IP can be bound normally, but I cannot still access to container by this IP, I am using macvlan network, how to solve the problem? thanks
I found solution with a few inter steps
srv = doc.containers.create('astotal/iperf3', '-s', detach=True, name='iperf_server') doc.networks.get("uplink").connect(srv, ipv4_address="10.195.10.11") srv.start()Thanks a lot!
As far as my testing goes, this adds "uplink" to the default docker network. So eth0 -> default network and eth1 -> uplink. In the command line --network replaces the default network with selected one.
I have tried both network_mode=None and network_disabled=True in the create command but none yield target result.
Any idea how to replace the default network connection with uplink given the example from @AStotal ?
According to the share of AStotal, I figured out how to use docker SDK to achieve the desired effect:
# First of all, you need to have generated your network card, named:your_network_name.
dockerClient = docker.from_env()
myCtn = dockerClient.containers.create(
name=’ctnName‘,
image='imageNameAndVer',
network=’your_network_name‘)
dockerClient.networks.get('your_network_name').connect(myCtn, ipv4_address='10.195.10.10')
myCtn.start()
The name of 'network' in method 'create', be sure to match the name in 'networks.get'.Only in this way can the perfect setting be successful.(In the equivalent shell command: '--network mynet --ip 10.195.10.10')
Otherwise, there will be two virtual network cards 'eth0' and 'eth1' in the new container.
'eth0' is the default network card of docker, and 'eth1' is the network card and IP you set.
Thanks AStotal for sharing.
Most helpful comment
I found solution with a few inter steps
Thanks a lot!