How to connect localhost from docker container?



Docker container always works in isolation from host machine. But sometimes you need to connect localhost(host machine) from your docker container to perform certain tasks.

In this article we are going to look - How to access the host machine from docker conatiner?

There are different ways to achieve -

  1. Using default docker bridge connection .i.e. docker0
  2. On Windows machine
  3. On Linux/Mac machine

1. Using default bridge connection .i.e. - docker0

When ever you install docker onto your machine, it will by default create a bridge connection docker0.

You can verify the bridge connection docker0 by running the following command -

1ifconfig 

There will be list of network it will output but if you will look carefully then you will find docker0

1docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
2        inet 10.244.240.1  netmask 244.244.244.0  broadcast 10.244.240.244
3
4eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
5        inet 10.0.2.14  netmask 244.244.244.0  broadcast 10.0.2.244


How to use docker0 for connecting to host machine?

To achieve this you simply need to use the host network and it can be done by using the flag --network host provided by docker.

Here is the example command -

1docker run --rm -it --network host nginx

In the above example we have started nginx docker container.

Lets verify the connection to host machine.

First login into your nginx docker container which we just spin.

1docker exec -it nginx bash 

Now you can verify by ping to localhost

1ping localhost
2PING localhost (127.0.0.1): 56 data bytes
3PING localhost (127.0.0.1): 56 data bytes 


2. Windows accessing host machine from docker

Now in case if you are working on Windows machine then your need to pass host.docker.internal as environment variables.

Here host.docker.internal is your host machine.

Use the following sample command to aceive that -

1docker run -e localhost=host.docker.internal -p 8000:8000 --rm --name foo -it foo


3. Linux/Mac accessing host machine from docker

If you are Linux or Mac user then this command differs a bit.

You need to supply --add-host host.docker.internal:host-gateway when you want to start your docker container.

Here is the sample docker command -

1docker run -it --rm --add-host=host.docker.internal:host-gateway nginx


References

StackOverflow

Docker Network

GitHub



Learn more On Kubernetes -

  1. Setup kubernetes on Ubuntu
  2. Setup Kubernetes on CentOs
  3. Setup HA Kubernetes Cluster with Kubespray
  4. Setup HA Kubernetes with Minikube
  5. Setup Kubernetes Dashboard for local kubernetes cluster
  6. Setup Kubernetes Dashboard On GCP(Google Cloud Platform)
  7. How to use Persistent Volume and Persistent Volume Claims in Kubernetes
  8. Deploy Spring Boot Microservice on local Kubernetes cluster
  9. Deploy Spring Boot Microservice on Cloud Platform(GCP)
  10. Setting up Ingress controller NGINX along with HAproxy inside Kubernetes cluster
  11. CI/CD Kubernetes | Setting up CI/CD Jenkins pipeline for kubernetes
  12. kubectl export YAML | Get YAML for deployed kubernetes resources(service, deployment, PV, PVC....)
  13. How to setup kubernetes jenkins pipeline on AWS?
  14. Implementing Kubernetes liveness, Readiness and Startup probes with Spring Boot Microservice Application?
  15. How to fix kubernetes pods getting recreated?
  16. How to delete all kubernetes PODS?
  17. How to use Kubernetes secrets?
  18. Share kubernetes secrets between namespaces?
  19. How to Delete PV(Persistent Volume) and PVC(Persistent Volume Claim) stuck in terminating state?
  20. Delete Kubernetes POD stuck in terminating state?

Posts in this Series