0

I started using the Docker API in python and ran into a time execution issue. When running the following code, it takes 15 seconds to build a single network.

def createDockerNetwork():
nb_nw = 20
logging.info("*** Creating bridges: ")
for i in range(0,nb_nw):
    logging.info("   - bridge: nw_"+str(i))
    base = '10.'+str(i/255)+'.'+str(i%255)
    sbnet = base + '.0/24'
    iprange = base + '.0/24'
    gw = base + '.254'
    options = dict([('com.docker.network.bridge.enable_icc','true'), ('com.docker.network.bridge.name','br_'+str(i))])
    ipam_config = docker.types.IPAMPool(subnet=sbnet, iprange=iprange, gateway=gw)
    try:
        client.networks.create('nw_'+str(i), driver='bridge', options=options, ipam = ipam_config)
    except docker.errors.APIError as error:
        logging.error(" Error while creating bridge no:"+str(i)+" : \n  ---> {0}".format(error))
return 0

2018-09-05 09:49:05,352 - root - INFO - *** Creating bridges:

2018-09-05 09:49:05,352 nw_0

2018-09-05 09:49:20,420 nw_1

2018-09-05 09:49:35,503 nw_2

2018-09-05 09:49:50,557 nw_3

2018-09-05 09:50:05,616 nw_4

...

2018-09-05 09:53:51,693 nw_19

The weird thing is that when I run the same command directly inside a terminal using :

docker network create nw_simu -d bridge --opt com.docker.network.bridge.enable_icc=true --opt com.docker.network.bridge.name=nw_1 --subnet=10.0.0.0/24 --ip-range=10.0.0.0/24 --gateway=10.0.0.254

it runs instantly. So my question is :

Did some of you fine people have the same issue using this API ? Is there something I can do to speed up the process ? (I'm already implementing routines in order to run this in parallelized fashion)

Any tips appreciated !

Versions : Python 2.7.15rc1, Docker 18.06.1-ce, Ubuntu 18.04.1 LTS

Community
  • 1
  • 1

1 Answers1

0

As it turns out, I did a mistake in my code, the correct way of doing it is as follows :

ipam_pool = docker.types.IPAMPool(subnet=sbnet, iprange=iprange, gateway=gw)
ipam_config = docker.types.IPAMConfig(pool_configs=[ipam_pool])
try:
client.networks.create('nw_'+str(i), driver='bridge', options=options, ipam = ipam_config)
except ..... as error:

Thus it works correctly