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