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

1.3k Views Asked by At

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

There are 2 best solutions below

1
On

When services are running, you can attache them for debugging with

docker exec -ti some-service.1.lz3j7mqb1ux4jp5cwe87gpiok sh

you need to figure out the service name with docker ps.

Then you can try to ping another service. But this will only work when both services are attached to the same overlay network which is not ingress.

docker network create -d overlay dev
docker service create --network dev --name a ...
docker service create --network dev --name b ...

Then you can ping a from b and ping b from a.

0
On

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