How can i ping a local docker container from an outside host using macvlan network?

Viewed 1228

objective : assume two hosts A & B. container x at A should be able to be pinged from host B such that this container x is present in the same network as A & B and has its own ip address.

Docker provides two types of network solutions for multi-host networking 1) Overlay network with/without docker swarm 2) Macvlan network. I would like to know for the 2nd type Macvlan networks how can i achieve the objective

2 Answers

Create a macvlan network called my-macvlan-net:

$ docker network create -d macvlan \
  --subnet=172.16.86.0/24 \
  --gateway=172.16.86.1 \
  -o parent=eth0 \
  my-macvlan-net

Start an example alpine container and attach it to the my-macvlan-net network:

$ docker run --rm -dit \
  --network my-macvlan-net \
  --name my-macvlan-alpine \
  alpine:latest \
  ash

Check container network settings:

$ docker exec my-macvlan-alpine ip addr show eth0

9: eth0@tunl0: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP
link/ether 02:42:ac:10:56:02 brd ff:ff:ff:ff:ff:ff
inet 172.16.86.2/24 brd 172.16.86.255 scope global eth0
   valid_lft forever preferred_lft forever

$ docker exec my-macvlan-alpine ip route

default via 172.16.86.1 dev eth0
172.16.86.0/24 dev eth0 scope link  src 172.16.86.2

Networking using a macvlan network

Related