Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build docker environment migrate from Virtualbox with complex network configuration

Tags:

docker

I have an environment which consist of 3 application, 2 running in Windows and 1 running in Debian OS.
For testing purpose, I already build the environment in only 1 PC (running Windows 10) with VirtualBox VM (for running Debian OS).
Below is detail network configuration:

Host PC (using 2 VirtualBox Host Only Adapter):

Ethernet adapter VirtualBox Host-Only Network:

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::ec0c:3c16:4f85:1a5e%12
   IPv4 Address. . . . . . . . . . . : 192.168.1.11
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   IPv4 Address. . . . . . . . . . . : 192.168.1.12
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   IPv4 Address. . . . . . . . . . . : 192.168.1.13
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   IPv4 Address. . . . . . . . . . . : 192.168.1.14
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   IPv4 Address. . . . . . . . . . . : 192.168.1.15
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   IPv4 Address. . . . . . . . . . . : 192.168.1.21
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   IPv4 Address. . . . . . . . . . . : 192.168.1.22
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   IPv4 Address. . . . . . . . . . . : 192.168.1.23
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   IPv4 Address. . . . . . . . . . . : 192.168.1.24
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   IPv4 Address. . . . . . . . . . . : 192.168.1.25
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   IPv4 Address. . . . . . . . . . . : 192.168.1.26
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   IPv4 Address. . . . . . . . . . . : 192.168.1.31
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   IPv4 Address. . . . . . . . . . . : 192.168.1.32
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   IPv4 Address. . . . . . . . . . . : 192.168.1.33
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   IPv4 Address. . . . . . . . . . . : 192.168.1.34
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   IPv4 Address. . . . . . . . . . . : 192.168.1.123
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . :

Ethernet adapter VirtualBox Host-Only Network #2:

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::5d7:813f:a9a:865d%16
   IPv4 Address. . . . . . . . . . . : 192.168.1.122
   Subnet Mask . . . . . . . . . . . : 255.255.255.248
   Default Gateway . . . . . . . . . :

VirtualBox VM network configuration:
/etc/network/interfaces:

auto lo
iface lo inet loopback

auto eth1
iface eth1 inet static
  address 192.168.1.120
  netmask 255.255.255.0
  gateway 192.168.1.1

auto eth2
allow-hotplug eth2
iface eth2 inet static
  address 192.168.1.10
  netmask 255.255.255.0
  gateway 192.168.1.1

auto eth2:0
iface eth2:0 inet static
  address 192.168.1.20
  netmask 255.255.255.0

auto eth2:1
iface eth2:1 inet static
  address 192.168.1.30
  netmask 255.255.255.0

Ifconfig output:

eth0      Link encap:Ethernet  HWaddr 08:00:27:a8:08:8b  
          inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0
          ...

eth1      Link encap:Ethernet  HWaddr 08:00:27:41:27:73  
          inet addr:192.168.1.120  Bcast:192.168.1.255  Mask:255.255.255.0
          ...

eth2      Link encap:Ethernet  HWaddr 08:00:27:0d:6b:54  
          inet addr:192.168.1.10  Bcast:192.168.1.255  Mask:255.255.255.0
          ...

eth2:0    Link encap:Ethernet  HWaddr 08:00:27:0d:6b:54  
          inet addr:192.168.1.20  Bcast:192.168.1.255  Mask:255.255.255.0
          ...

eth2:1    Link encap:Ethernet  HWaddr 08:00:27:0d:6b:54  
          inet addr:192.168.1.30  Bcast:192.168.1.255  Mask:255.255.255.0
          ...

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          ...

Below is some flow of network interaction in current environment:

  • App1 run in VM and create multiple socket server:
    -Listening on IP 192.168.1.10, 192.168.1.20, 192.168.1.30 port 1448
    -Listening on IP 192.168.1.120 port 1449
  • App2 run in Host machine and create multiple connection:
    -create socket server in 192.168.1.123 port 1448
    -connect to socket server in VM: 192.168.1.10 port 1448 (using binding ip = 192.168.1.11)
    -connect to socket server in VM: 192.168.1.20 port 1448 (using binding ip=192.168.1.12)
    ...
    -connect to socket server in VM: 192.168.1.120 port 1449
  • App3 run in Host machine and create multiple connection:
    -connect to socket server 192.168.1.123 port 1448 (App2)
    -connect to socket server 192.168.1.120 port 1449 (App1)

The environment is working OK now, but after read about Docker, I plan to replace VirtualBox VM with using Docker for reduce memory usage.
I successed build up an Debian image, setup library, etc...
However, I don't know how to setup network in Docker for having similar function with above VirtualBox VM configuration. Some dificult points:

  • Can we create network interface similar like 'Ethernet adapter VirtualBox Host-Only Network' using Docker?
  • In case we cannot create such that network interface, it is OK to remain these 2 VirtualBox Host Only Adapter in environment. But i'm not sure how to "connect" these network interface into Docker container?
  • It seem impossible to assign static IP address for docker container, is this correct?
    (This doesn't help: Assign static IP to Docker container)
  • It seem also impossible to assign multiple IP address to docker container, is it correct?

So it seem very dificult to build up above environment using Docker.
If anyone have solution that still able to build the environment using Docker, please help point out some direction.
Thank you very much!
like image 427
phibao37 Avatar asked Oct 16 '20 12:10

phibao37


People also ask

How do I make my Docker container accessible from network?

To make a port available to services outside of Docker, or to Docker containers which are not connected to the container's network, use the --publish or -p flag. This creates a firewall rule which maps a container port to a port on the Docker host to the outside world.

Can Docker container have multiple network interfaces?

You can create multiple networks with Docker and add containers to one or more networks. Containers can communicate within networks but not across networks. A container with attachments to multiple networks can connect with all of the containers on all of those networks.

How do I create a Docker bridge network?

Manage a user-defined bridge Use the docker network create command to create a user-defined bridge network. You can specify the subnet, the IP address range, the gateway, and other options. See the docker network create reference or the output of docker network create --help for details.


2 Answers

The first three questions you asked are solved with macvlan network. You'll have containers directly attached to your network, just like VM's. Here's an example:

version: "2.1"

services:
  nginx1:
    image: nginx
    networks:
      network_1:
        ipv4_address: 10.1.1.115
  nginx2:
    image: nginx
    networks:
      network_1:
        ipv4_address: 10.1.1.116


networks:
  network_1:
    driver: macvlan
    driver_opts:
      parent: enp52s0  # Your network interface name
    ipam:
      driver: default
      config:
        - subnet: 10.1.1.0/24
          gateway: 10.1.1.1

In this example I declared a macvlan network named network_1, which attached to the enp52s0 network interface. The two nginx containers use that network and each advertises its own static IP.

Now if you want to assign more than one IP per container things begin to get messy. To assign an additional address you need an additional network, with its own IP range, its own parent network interface, and its own gateway. That is literally another network. Or you can think of some hack, maybe using a proxy container that'll listen on another IP and forward traffic into desired container but it's kinda 'meh'. I'd say that VM overhead does not worth all that trouble unless you are open to redesign connectivity of your application.

like image 87
anemyte Avatar answered Oct 18 '22 03:10

anemyte


In the docker documentation there are articles about "user defined" network bridges, the link can be found here. I think you'll have to create those according to your network architecture and once they are made then you can specify when creating a docker container that it should use one of the user-defined bridges that you created.

As you are using multiple applications and would like to put this in multiple docker containers I would also suggest to look into "docker-compose". With this you can create a YML manifest that will spin up multiple docker containers with their defined configs, networks, specific ports, ... all at once so you can launch/shutdown the whole environment at once.

An example and even more information on ip addresses of docker images can be found in this article.

like image 1
Dieter Maes Avatar answered Oct 18 '22 03:10

Dieter Maes