{"id":229,"date":"2020-01-26T11:39:06","date_gmt":"2020-01-26T11:39:06","guid":{"rendered":"http:\/\/tonysbit.blog\/?p=229"},"modified":"2020-01-26T11:39:06","modified_gmt":"2020-01-26T11:39:06","slug":"how-to-access-the-docker-host-from-a-docker-container","status":"publish","type":"post","link":"https:\/\/tonysbit.blog\/?p=229","title":{"rendered":"How to access the Docker host from a Docker container"},"content":{"rendered":"\n
<\/p>\n\n\n\n
There are situations where from a Docker container, you need to access services on the host machine. An example of this use-case is trying to test pdf-generation using a website hosted in your IDE environment from a container running on the same host pdf-bot<\/a>.<\/p>\n<\/div>\n\n\n\n From Docker 18.04 on-wards there is a convenient internal DNS Entry ( You can ping the host from within a container by running<\/p>\n To test this feature using this guide you will need<\/p>\n This will return you the container id, in my case it is a77209ae9b0f11c80ce488eda8631a03e8444af94167fd6a96df5ee2e600da1f<\/p>\n<\/div>\n\n\n\n e.g. Note: you do not need to use full container id, you can use first 3 characters<\/strong><\/p>\n<\/div>\n\n\n\n From within the container run the following commands:<\/p>\n Get package lists – Install net-tools – Install DNS utilities – Install iputils-ping – <\/p>\n\n\n\n There is a dns service<\/a> running on the containers network default gateway (eth01) that allows you to resolve to the internal IP address used by the host. The DNS name to resolve the host is Ping the host to establish that you have connectivity. You will also be able to see the host IP Address that is resolved.<\/p>\n note: you should use this internal DNS address instead of IP as the IP address of the host may change.<\/strong><\/p>\n<\/div>\n\n\n\n Services on the host should be advertising on either 0.0.0.0 or localhost to be accessible.<\/p>\n e.g. To access a service on the host running on localhost:4200 you can run the following command from within the host.<\/p>\n Note that if you use <\/p>\n\n\n\n Stack overflow discussing solution here<\/a><\/p>\n<\/li>\nSummary<\/h1>\n
host.docker.internal<\/code>) accessible from your containers that will resolve to the internal network address of your host from your Docker container’s network.<\/p>\n
ping host.docker.internal<\/code><\/p>\n<\/div>\n\n\n\n
Proof<\/h1>\n
\n
Steps<\/h2>\n
Step 1: Run the Docker container using the command<\/h3>\n
docker run -t -d ubuntu<\/code><\/p>\n<\/div>\n\n\n\n
<\/figure>\n\n\n\n
Step 2. Access the container by running<\/h3>\n
docker exec -it <container id> \/bin\/bash<\/code><\/p>\n
docker exec -it a77 \/bin\/bash<\/code>.<\/p>\n
Step 3. Set up the container<\/h3>\n
apt-get update<\/code><\/p>\n
apt-get install net-tools<\/code><\/p>\n
apt-get install dnsutils<\/code><\/p>\n
apt-get install iputils-ping<\/code><\/p>\n<\/div>\n\n\n\n
<\/figure>\n\n\n\n
Step 4. Using the DNS Service<\/h3>\n
host.docker.internal<\/code>.<\/p>\n<\/div>\n\n\n\n
<\/figure>\n\n\n\n
Step 5. Pinging the host<\/h3>\n
ping host.docker.internal<\/code><\/p>\n
\n
curl 192.168.65.2:4200<\/code><\/p>\n
host.docker.internal<\/code> some web servers will throw "Invalid Host header" errors in which case you either need to disable host header check on your web server or use the IP Address instead of the host name<\/strong><\/p>\n<\/div>\n\n\n\n
References<\/h2>\n
\n