Am not well versed with Unix networking, adding virtual interfaces etc, trying to learn it now. We are trying to dockerize our application.
My requirement is : To assign an ip to a docker container which is accessible from an external application/browser.
The container ip should be pingable from a different computer in the same network basically.I don't want to use port forwarding.
I want to access a docker container just like we access a VM using an ip address.[ Without the port mapping, -p flag. If i run any server like Apache or Tomcat inside the container, it should be accessible using the container ip and port. For example: http://container_ip:8443]
Is this possible in docker?Running ifconfig on my Unix box(RHEL 7.1) shows docker0, ens,lo and veth interfaces. There is no eth0. Kind of confused on this.
My current preferred approach with this is to use either the macvlan or ipvlan Docker network driver. I prefer macvlan as each container can have its own MAC address but some things like VMware don’t like having multiple Mac addresses for a single virtualized nic and won’t route traffic properly.
Setup is pretty straight forward. First you need to determine a few things.
Next you create a new Docker network like so:
Now when you start containers use
For more information see the docker docs: https://docs.docker.com/engine/userguide/networking/get-started-macvlan
One caveat to this approach is that macvlan/ipvlan dont seem to work very well with Docker for Mac. The HyperKit vm it creates is a bit of a black box. The macvlan/ipvlan approach requires a more controlled network that Docker for Mac doesn't give you. If you are trying to do this with Docker for Mac then I would suggest setting up a Docker Machine. The docs for how to do that are here: https://docs.docker.com/machine/get-started/.
In this scenario, unless you like setting up routing rules on your Mac, you should have the docker machine use a bridged interface that the macvlan/ipvlan network can then be attached to. In my experience the need for a second NIC that is NAT'ed through the MacOS host is unnecessary but you may find something otherwise.
I struggled to get that functionality, and I will share my experience and what I did to get exactly what you need.
The short answer:
You need to create your own bridge, connect your host's physical network interface to that bridge, and as well connect the virtual interfaces of each container you want to behave like a normal bridged vritual machine in your network, and then make the container chooses its own IP address when it starts.
The detailed answer:
Creating Persistence Network BridgeThe
Bridge
, is a device (in our case virtual device), which behaves similar to network swiches (operates mainly on network layer 2), i.e., it can connect two or more network interfaces to be on the same local area network (LAN) if they have the same subnet.You are going to create new persistence bridge
br0
(it will get started automatically on system boot), add your physical network interface into it (in my case it iseth0
). Note that after you add your interface to the bridge, the interface doesn't need IP address anymore, because the bridge will get IP address and can be used instead of your interface, i.e., you can communicate using the bridge as if it were your physical interface and it will forward the in/out data packets to the correct destination. You don't need to assign any hardware (MAC address) to the bridge, it will automatically take the MAC of the first added interface.Install bridges managing utility:
To create persistence bridge, edit
interfaces
file:Add the follwing configuration to the end of the file (adapt them to suit your needs):
Now remove Docker's default bridge docker0, as we don't need it:
Edit Docker's service-start script to use your bridge (br0) instead of Docker's default bridge (docker0), and pass some important bridge parameters:
Ubuntu:
Adapt the file to look like this:
Now tell the system about the changes on that file:
Reboot the system:
Now check your bridge, it should be there!
Now create your container like bellow, this will lead to give your container a fix IP:
The important part related to your network requirements is:
Of course be sure that you installed
iproute2 net-tools iputils-ping
packages in your container to be able to execute the common network commands (giving the fixed ip done byip
command).For the first time you run the container, you may NOT notice any changes in IP address, because your conainer probably doesn't have
iproute2
package (i.e. there is notip
command), just intall the mentioned packages and then restart the container and everything should be exactly as you want!Hope that helps.