9

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.

markzz
  • 1,185
  • 11
  • 23

1 Answers1

6

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)
anhlc
  • 13,839
  • 4
  • 33
  • 40
  • Adding this alone doesn't seem to help. https://gist.github.com/markzz/3f25719a37f85bc851aa10e3b9c0ee5f – markzz Oct 16 '17 at 03:51
  • This would help on how make a change to a python installed package: https://stackoverflow.com/questions/23075397/python-how-to-edit-an-installed-package – anhlc Oct 16 '17 at 04:18
  • You are right. It doesn't work that way. I have updated my solution. – anhlc Oct 16 '17 at 05:10