Connecting to the Host :
Docker usually containerize the services that form our stack and use inter-container networking to communicate between them. Sometimes we might need a container to talk to a service on our host that has not been containerized. Lets see how to access localhost from within a Docker container.
host.docker.internal too via the --add-host flag for docker run commands are used in Linux. Start your containers with this flag to expose the host string:
$docker run -d --add-host host.docker.internal:host-gateway my-container:latest
Connecting to the Host Network:
Containers are launched with the host network by adding the --network=host flag:
Get the IP Address of your local machine,
ip addr show eth0
$docker run -itd --network=host my-container:latest ip addr show eth0
Once executed the the above command to run the docker container both container and host machine have the same IP Address and connecting with the same network.
Maybe your container can reference localhost or 127.0.0.1 directly.
Comments (0)