docker / docker/docker-py

Cannot easily start a container in a single network with specified ip

Open
#2,563 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
7.2k
Forks
1.7k
Avg merge
13d 8h
Merged PRs (30d)
2

Description

I am using
Python 3.5.2
docker-py 4.2.0
Docker version 18.09.7, build 2d0083d

I am having issues finding a good solution to start a container in a single network with specified ip.

Given the docker run command:
docker run --network es_bridge --ip 172.18.0.10 --name server10 --hostname server10 -d -it my_container

I have the container configuration as:

{
  "labs": [{
    "containers": [
      {
        "name": "server10",
        "hostname": "server10",
        "image": "my_container",
        "detach": true,
        "network": "es_bridge",
        "ip": "172.18.0.10",
        "tty": true,
        "stdin_open": true
      }
    ]
  }]
}

Unfortunately, we cannot provide the ip in the container config. We need to use network.connect. So, I came up with the following after some google:

ip = config["ip"]
del config["ip"]
c = client.containers.create(**config)
client.networks.get(config["network"]).connect(c, ipv4_address=ip)
c.start()

This option has a problem, it silently ignores the connect request and binds to config["network"] using an auto ip.

Then, I tried not to provide the network to the create command.

ip = config["ip"]
del config["ip"]
net = config["network"]
del config["network"]
c = client.containers.create(**config)
client.networks.get(net).connect(c, ipv4_address=ip)
c.start()

The problem with this solution is that it has the container connected to two networks: bridge, in my case, and the defined network, es_bridge. The container has the correct ip in my network.

Finally, I thought about a hack too avoid the double network issue above:

ip = config["ip"]
del config["ip"]
c = client.containers.create(**config)
client.networks.get(config["network"]).disconnect(c)
client.networks.get(config["network"]).connect(c, ipv4_address=ip)
c.start()

It works as I want, but it is rather counter intuitive and even weird.

What am I missing here?

Thank you,

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the reported behavior with Python 3.5.2, docker-py 4.2.0, and Docker 18.09.7. Inspect client.containers.create and client.networks.get(...).connect, then determine how a container can use only es_bridge with the requested IPv4 address; done means the behavior is supported or the limitation is clearly documented and tested.

Written by the indexing model from the issue text.

Assessment

Tech stack
docker, python
Domain
devops, networking
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.