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 -


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  netmask  broadcast
4eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
5        inet  netmask  broadcast

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 ( 56 data bytes
3PING localhost ( 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



Docker Network


Other articles on Devops -

  1. Part-1 : Setup kubernetes on Ubuntu
  2. Part-2 : Setup Kubernetes on CentOs
  3. Part-3 : Setup HA Kubernetes Cluster with Kubespray
  4. Part-4 : Setup HA Kubernetes with Minikube
  5. Part-5 : Setup Kubernetes Dashboard for local kubernetes cluster
  6. Part-6 : Setup Kubernetes Dashboard On GCP(Google Cloud Platform)
  7. Part-7 : How to use Persistent Volume and Persistent Volume Claims in Kubernetes
  8. Part-8 : Deploy Spring Boot Microservice on local Kubernetes cluster
  9. Part-9 : Deploy Spring Boot Microservice on Cloud Platform(GCP)
  10. Part-10 : Setting up Ingress controller NGINX along with HAproxy inside Kubernetes cluster
  11. Part-11 : CI/CD Kubernetes | Setting up CI/CD Jenkins pipeline for kubernetes
  12. Part-12 : kubectl export YAML | Get YAML for deployed kubernetes resources(service, deployment, PV, PVC….)