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
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
Learn more On Kubernetes -
- Setup kubernetes on Ubuntu
- Setup Kubernetes on CentOs
- Setup HA Kubernetes Cluster with Kubespray
- Setup HA Kubernetes with Minikube
- Setup Kubernetes Dashboard for local kubernetes cluster
- Setup Kubernetes Dashboard On GCP(Google Cloud Platform)
- How to use Persistent Volume and Persistent Volume Claims in Kubernetes
- Deploy Spring Boot Microservice on local Kubernetes cluster
- Deploy Spring Boot Microservice on Cloud Platform(GCP)
- Setting up Ingress controller NGINX along with HAproxy inside Kubernetes cluster
- CI/CD Kubernetes | Setting up CI/CD Jenkins pipeline for kubernetes
- kubectl export YAML | Get YAML for deployed kubernetes resources(service, deployment, PV, PVC....)
- How to setup kubernetes jenkins pipeline on AWS?
- Implementing Kubernetes liveness, Readiness and Startup probes with Spring Boot Microservice Application?
- How to fix kubernetes pods getting recreated?
- How to delete all kubernetes PODS?
- How to use Kubernetes secrets?
- Share kubernetes secrets between namespaces?
- How to Delete PV(Persistent Volume) and PVC(Persistent Volume Claim) stuck in terminating state?
- Delete Kubernetes POD stuck in terminating state?
Posts in this Series
- How to push Docker Images to AWS ECR(Elastic Container registry)?
- How to Copy Docker images from one host to another host?
- What is persistent storage in docker and how to manage it
- Docker Installation on MacOS, Linux and Windows
- Docker - ADD, Update, Export Environment variable
- How to fix-Docker docker failed to compute cache key not found
- How to fix docker driver failed programming external connectivity on endpoint webserver?
- How to fix docker error executable file not found in $PATH?
- How to expose port on live containers?
- How to expose multiple ports with Docker?
- How to restart single docker container within multiple docker container?
- How to edit file within Docker container or edit a file after I shell into a Docker container?
- How to fix Error starting docker service Unit not found?
- How to remove old, unused images of Docker?
- How to fix docker error invalid reference format error?
- How to fix requested access to the resource is denied?
- How to fix Docker error cannot delete docker container conflict unable to remove repository reference?
- How to fix docker error no space left on device?
- How to connect localhost from docker container?
- Docker COPY vs Docker ADD?
- 6 Ways to fix - Got permission denied while trying to connect to the Docker daemon socket?
- 6 Ways to fix – Docker COPY failed: stat no source files were specified