question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Run container on specific IP

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10

github_iconTop GitHub Comments

4reactions
AStotalcommented, Apr 25, 2018

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!

1reaction
OldLi-12commented, Jan 14, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Assign static IP to Docker container - Stack Overflow
This command will run a Docker container with a random IP like 172.17.0.5, but I need to assign a specific IP to the...
Read more >
Binding a container to a specific IP address - Learning Docker
Binding a container to a specific IP address ... Here, the IP address must be a valid IP address on the Docker host....
Read more >
How to Assign a Static IP to a Docker Container - How-To Geek
Static IP addresses don't change when containers or services are stopped and started, making them useful for permanent networking.
Read more >
Container networking - Docker Documentation
When you connect an existing container to a different network using docker network connect , you can use the --ip or --ip6 flags...
Read more >
Manually Specifying the IP Address of a Container
You can specify an IP address and a network explicitly, using Classless Inter-Domain Routing or CIDR notation. For example, we can launch a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found