Assign External Network Public or Private IP Address To Docker Container Without Port Binding
Assign External Network Public or Private IP Address To Docker Container Without Port Binding
Using the default docker0 Bridge and the port mapping works for most of the scenarios, but not all the
scenarios, for example, you want to put all the docker containers in a flat network to provide full-access
between the containers on different docker hosts. There are several ways to configure the docker multi-
host networking.
In this post will cover one of these ways: using Linux bridge to directly bridge the docker containers to
the external network.
net.ipv4.ip_forward = 1
Note- We are setting up a Linux router/gateway or maybe a VPN server (pptp or ipsec) or just a plain
dial-in server then we will need to enable forwarding.
[root@lb-01 ~]# sysctl –p
net.ipv4.ip_forward = 1 Output
3- Macvlan
For this we are using macvlan based connectivity with macvlan bridge mode.
MACVLAN creates multiple virtual network interfaces with different MAC addresses. This way if your
system has multiple IP addresses with MAC addresses then we can create multiple virtual network
interfaces each having their own IP address and MAC address.
MACVLAN doesn’t need to learn(identify) mac addresses of the systems within the network to distribute
traffic as it know’s every mac address, this makes it fast and easy to setup than bridge type networking.
Advantages of MACVLAN-
IPTables aren’t affected.
No port binding.
Easy to setup.
Faster than bridge networking.
4- First confirming that your Linux distribution has support for macvlan interfaces.
5- Verify Network Interfaces And Create virtual interfaces with different MAC addresses
Now we are creating virtual interfaces (eno1.1 for local ip and eno1.2 for public ip) with different mac
address (define any mac address) for macvlan bridge connection..
[root@linux-lb1 ~]# ip link add link eno1 address 08:00:27:f9:d6:c1 eno1.1 type macvlan
[root@linux-lb1 ~]# ip link add link eno1 address 08:00:27:f9:d6:c3 eno1.2 type macvlan
DEVICE=eno1.1
BOOTPROTO=none
ONBOOT=yes
NM_CONTROLLED=no
TYPE=macvlan
[root@lb-01 network-scripts]# vim ifcfg-eno1.2
DEVICE=eno1.2
BOOTPROTO=none
ONBOOT=yes
NM_CONTROLLED=no
TYPE=macvlan
In my Scenario-
[root@lb-01 ~]# docker run --name Local-IP-Con --net=Macvlan-Local --ip 172.16.10.24 -tid
ubuntu:14.04
Verify Docker Container and IP Address accessible form outside or not.
Example-2 Create a macvlan network and assign public ip address to container (Follow above setup )
Note- In This Example we r creating nginx container and verify access from anywhere.
[root@lb-01 ~]# docker run --name Public-IP-Con --net=Macvlan-Public --ip 59.180.229.222 -tid nginx
You can verify by hitting ‘59.180.229.222’ where you will be welcomed with nginx page.
Successfully Assigned The External Public Ip and Local Ip Address to Docker Container..!!
Reference- Ashutosh
https://github.com/docker/libnetwork/blob/master/docs/macvlan.md