Configuring a static IP address docker containers

Katebright

New Member
Joined
May 16, 2023
Messages
7
Reaction score
1
Credits
68
Hi guys,

I am presently working on my end of course project in linux. I want to deploy 2 haproxy and 2 web servers using docker containers.

My worry is i am unable to setup static IP addresses for my containers and the fact is that they automatically obtain IPs through dhcp which is not adapted to my actual network plan.
Can someone help me on how to go about it please?

I used the following commands to create my network and run my containers:

Docker network create –drive=bridge –subnet 192.168.10.0/24 mylan
# to create mylan which is my network

Docker run - d --name ct-web1 --network mylan ubuntu /bin/bash
Docker run - d --name ct-web1 --network mylan ubuntu /bin/bash

Docker run - d --name ct-haproxy1 --network mylan debian /bin/bash
Docker run - d --name ct-haproxy2 --network mylan debian /bin/bash
# to create my containers and in a specified network


For my router i used the following


Docker run - d --name ct-router --network host debian /bin/bash
Docker network connect mylan ct-router

Are they other instructions that i can use in order to get static IPs ? because when i use the above instructions, my containers will end up taking dynamic addresses even though on the defined network above but i prefer to make it static and fix

Below is an attached structure of my network plan to help you understand

Thank you for your help
 

Attachments

  • network set up.png
    network set up.png
    94 KB · Views: 124
Last edited:


Not going to do your homework for you but here is an example of how to assign an address to a container. I am assigning 192.168.1.1 to various ports. You can also manipulate ports. It would not make sense in this example but if you decided you wanted a website to respond externally to a different port for https say 4444 you could put it together by using something like 192.168.1.1:4444:443. In that case incoming traffic of 4444 would be translated internally to 443. If the text you are using isn't giving you all the answers you need I would go out to hub.docker.com and look at examples. Good luck in your studies

sudo docker run -it -p 192.168.1.1:139:139 -p 192.168.1.1:445:445 -d dperson/samba -p

I would go to hub.docker.com and look around for examples of how others have done things.
 
Hi,

iam not familar with docker but
as possible workaround you can setup a small dhcp server and set mac to ip in your config. For example you can use dnsmasq to do this. Please be patinet with this not that the other people in your system got dhcp adresses from your testsetup :)
 
@Katebright - you haven't added any new input, simply copied what was previously posted.

When you wish to post just use the bottom pane.

Cheers

Chris Turner
wizardfrooz
 
Not going to do your homework for you but here is an example of how to assign an address to a container. I am assigning 192.168.1.1 to various ports. You can also manipulate ports. It would not make sense in this example but if you decided you wanted a website to respond externally to a different port for https say 4444 you could put it together by using something like 192.168.1.1:4444:443. In that case incoming traffic of 4444 would be translated internally to 443. If the text you are using isn't giving you all the answers you need I would go out to hub.docker.com and look at examples. Good luck in your studies

sudo docker run -it -p 192.168.1.1:139:139 -p 192.168.1.1:445:445 -d dperson/samba -p

I would go to hub.docker.com and look around for examples of how others have done things.
If you just want to add an IP you can try something like this


docker run --net mynet123 --ip 172.18.0.22 -it ubuntu bash

So many different ways to do this. Hopefully this will help a bit more
 
If you just want to add an IP you can try something like this


docker run --net mynet123 --ip 172.18.0.22 -it ubuntu bash

So many different ways to do this. Hopefully this will help a bit more
thank you! it works
 

Members online


Top