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.
I struggled to get that functionality, and I will share my experience and what I did to get exactly what you need.
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 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 is eth0
). 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.
Warning: It is highly recommended not to do these steps remotely except you have a physical access to your server! You may lose your connection to your server if you were not careful.
Install bridges managing utility:
sudo apt install bridge-utils
The system will not be able to create the bridge without
bridge-utils
package.
To create persistence bridge, edit interfaces
file:
sudo vim /etc/network/interfaces
Add the follwing configuration to the end of the file (adapt them to suit your needs):
auto br0
iface br0 inet static
bridge_ports eth0
address 192.168.1.10
netmask 255.255.255.0
broadcast 192.168.1.255
gateway 192.168.1.1
Now remove Docker's default bridge docker0, as we don't need it:
sudo systemctl stop docker
sudo ip link set dev docker0 down
sudo brctl delbr docker0
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:
sudo vim /etc/systemd/multi-user.target.wants/docker.service
Adapt the file to look like this:
[Service]
ExecStart=/usr/bin/dockerd -H fd:// --bridge=br0 --fixed-cidr=192.168.1.32/27 --default-gateway=192.168.1.1
Now tell the system about the changes on that file:
sudo systemctl daemon-reload
Reboot the system:
sudo reboot
Now check your bridge, it should be there!
ip addr
Now create your container like bellow, this will lead to give your container a fix IP:
docker run --name myContainer \
-it --restart always --memory 100M \
--network bridge --cap-add NET_ADMIN \
--hostname client1.noureldin.local \
--add-host "client1.noureldin.local client1":192.168.1.123 \
mnoureldin/general-purpose:latest /bin/bash -c " \
ip addr flush dev eth0; \
ip addr add 192.168.1.123/24 brd + dev eth0; \
ip route add default via 192.168.1.1 dev eth0; \
/bin/bash"
The important part related to your network requirements is:
--network bridge --cap-add NET_ADMIN \
ip addr flush dev eth0; \
ip addr add 192.168.1.123/24 brd + dev eth0; \
ip route add default via 192.168.1.1 dev eth0; \
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 by ip
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 not ip
command), just intall the mentioned packages and then restart the container and everything should be exactly as you want!
Hope that helps.