Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can the docker container communicate host machine?

Host machine ip:192.168.123.2 Gateway:192.168.123.1

Adapter is enp3s0, so I create a docker network in host:

docker network create -d macvlan --subnet 192.168.123.0/24 --gateway 192.168.123.1 -o parent=enp3s0 -o macvlan_mode=bridge macnet

Run a container set IP

docker run --net macnet --ip 192.168.123.102 --name hd3  -it  ubuntu bash

Container and Host machine ping Gateway 192.168.123.1 are all work.
But doesn't work between the container and the host.

Container:
ping Gateway is OK
ping Host isn't OK

Host:
ping Gateway is OK
ping Container isn't OK

Why and How can I communicate between the docker container and host machine?

like image 867
Winson.Wu Avatar asked May 19 '18 12:05

Winson.Wu


People also ask

Can Docker containers communicate host?

docker run --network="host" Alternatively you can run a docker container with network settings set to host . Such a container will share the network stack with the docker host and from the container point of view, localhost (or 127.0.

What is the default networking between the Docker container and the host machine?

By default, Docker provides two network drivers for you, the bridge and the overlay drivers. You can also write a network driver plugin so that you can create your own drivers but that is an advanced task. Inspecting the network is an easy way to find out the container's IP address.

How does container network communication work?

Container networks run on software platforms, such as Docker, that can be run on the cloud. Container's efficiency comes from the fact they are self-contained. They use the bare minimum of software and resources to run and use a router to direct traffic instead of a switch.

What is a host machine in Docker?

In Docker, the host is a machine responsible for running one or more containers. Docker network host, also known as Docker host networking, is a networking mode in which a Docker container shares its network namespace with the host machine.


1 Answers

If you have a recent enough docker (18.03 and more), see, as mentioned here, if you can use:

host.docker.internal

That was first documented for MacOS as host, but should work on Windows host too.

like image 55
VonC Avatar answered Oct 18 '22 02:10

VonC