0

I created a network with driver: ipvlan and several subnets with specific gateways in it. (about 20)

Now i want to create containers with specific IP's, so that they get assign to the right subnet of the ipvlan network only!

     dockernetwork = client.networks.get('ipvlan172')
     print "create container with ip "+targetIP
     client.containers.run('myalpine',auto_remove=True,command='ping 192.168.11.11',detach=True,name=targetName)
     dockernetwork.connect(container,ipv4_address=targetIP)
     container.start()

What happens here is, that client.containers.run give the container an ip of the docker bridge network and the default gateway to the docker bridge. Then dockernetwork.connect gives the container's interface a second ip (target ip).

If i add the parameter network='ipvlan172' to the command:

client.containers.run('myalpine',network='ipvlan172',auto_remove=True,command='ping 192.168.11.11',detach=True,name=targetName)

The created container does not get an ip of the docker bridge network, which i intended, but the targetIP of the command

dockernetwork.connect(container,ipv4_address=targetIP)

will be ignored. So that the created Container has an IP of a random subnet of the ipvlan network

One way to solve this, is to create a script which will be executed after the start of the container and deletes the dockerhost ip and changes the default gateway.

Are there other solutions?

Thanks!

z0nderling
  • 31
  • 8

1 Answers1

0

Okay i found out what you need to do in this case.

First u create the container without parameter network=ipvlanXX, so that the container gets a connection to the docker bridge.

Then u connect the container to the network of your choice, in my case ipvlan172.

dockernetwork.connect(container,ipv4_address=targetIP)

Afterwards you disconnect the container of the docker bridge network.

network_dockerbridge.disconnect(container,force=True)

And finally start the container.

container.start()

The created container has now the correct ip and uses the gateway of the correct subnet of the ipvlan network.

It would be nice to have an ipv4/ipv6 parameter in the run/create implementation of the dockerpy modul.

z0nderling
  • 31
  • 8