sarunas-zilinskas / docker-compose-macvlan

Docker-compose macvlan example - container using different IP address than host.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Why the null network?

haneef95 opened this issue · comments

Hi,

Any specific reason as to why a null network has to be defined in addition to the macvlan network?

Thanks,

Hello haneef95,

This is an old concept on which I worked more than a year before so I can't recall all exact details what is what and how is how.

In regards about the question itself, I really didn't do much investigation as to why it behaves like this but in my experience it didn't work without the null network. And what is even worse it not only just didn't work but it broke the entire networking and I was not able to SSH into host anymore so had to physically get to the machine to fix this up.
Maybe it was some sort of the bug in docker networking stack or maybe something else can't say for sure but this method worked for me wonders so I kept it like this.
Also as you raised this question it got me wondering if null + macvlan network is mandatory instead of having only the macvlan so I am planning to do some tinkering with it and see if it works without null network.

Thanks for that.

I've since got it working, like so (a single macvlan network):

version: "2.3"

services:
  playground:
    image: ubuntu:latest
    #container_name: default-busybox
    #command: ['sh'] # not needed, as that's the default.
    stdin_open: true # docker run -i # Uncomment this and tty options for testing.
    tty: true        # docker run -t # Without these options, busybox automatically exits with nothing for docker attach to attach to.
    networks:
      mcv1:
        ipv4_address: 192.168.0.35
    hostname: default-busybox

networks:
  mcv1:
    name: mcv1
    driver: macvlan
    driver_opts:
      parent: eth1 #your ethernet interface
    ipam: # https://docs.docker.com/compose/compose-file/compose-file-v2/#ipam
      config:
        - subnet: 192.168.0.0/24 # I use the same subnet as my LAN router.
          gateway: 192.168.0.1
          ip_range: 192.168.0.32/27 # Outside of the DHCP Leasable Range, to avoid conflicts.
          # Total : 32 - 63 (32 IPs).
          # Note: This parameter is used by IPAM to auto allocate IP Addresses.
          # Hence, the below app-specific allocations are for static allocations only.
          # If this parameter is not set, it may result in IP Address collisions between Docker IPAM and DHCP.
          # ip_range isn't a required parameter however!
          #aux_addresses: # Optional, not specified.
          #  host: 192.168.1.101


# Source: https://dev.to/fredlab/make-docker-containers-available-both-on-your-local-network-with-macvlan-and-on-the-web-with-traefik-2hj1

^ baseconfig

services:
  myapp:
    networks:
      mcv1_projectname:
        ipv4_address: 192.168.0.33

networks:
  mcv1_projectname:
    external:
      name: mcv1

      # Source: https://dockstarter.com/advanced/macvlan/

additional apps in different a different yaml file once the first one is executed.

I guess, this could be made better by using depends_on and etc...

Note 1: if the docker host isn a VM, the MTU might need to be lowered on the interface(s) to avoid net/http tls handshake errors.
Note 2: The eth1 adapter in the docker host may need to be allowed to spoof mac addresses, depending on the host/network configurations

I did some testing without the duplicate block (null driver) and surprisingly it works just fine! Just like it worked for you.
Also good notes!
About MTU size, never tried running docker in VM but will keep that in mind if would have to for some reason in the future, thanks! :)

Yh, especially Azure VM/NETs, as they fragment at 1400 (yuk! it seems, hope they have a good reason!)