New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support host.docker.internal DNS name to host #264

Open
Mahoney opened this Issue Mar 31, 2018 · 66 comments

Comments

Projects
None yet
@Mahoney
Copy link

Mahoney commented Mar 31, 2018

  • This is a bug report
  • This is a feature request
  • I searched existing issues before opening this one

Expected behavior

As in docker-for-mac and docker-for-windows, inside a container, the DNS name host.docker.internal resolves to an IP address allowing network access to the host (roughly the output of ip -4 route list match 0/0 | cut -d' ' -f3 inside the same container).

Actual behavior

host.docker.internal resolves to nothing

Steps to reproduce the behavior

Execute docker run --rm alpine nslookup host.docker.internal

See it returns nslookup: can't resolve 'host.docker.internal': Name does not resolve

Output of docker version:

Client:
 Version:	18.03.0-ce
 API version:	1.37
 Go version:	go1.9.4
 Git commit:	0520e24
 Built:	Wed Mar 21 23:10:09 2018
 OS/Arch:	linux/amd64
 Experimental:	false
 Orchestrator:	swarm

Server:
 Engine:
  Version:	18.03.0-ce
  API version:	1.37 (minimum version 1.12)
  Go version:	go1.9.4
  Git commit:	0520e24
  Built:	Wed Mar 21 23:08:36 2018
  OS/Arch:	linux/amd64
  Experimental:	false

Output of docker info:

Containers: 0
 Running: 0
 Paused: 0
 Stopped: 0
Images: 1
Server Version: 18.03.0-ce
Storage Driver: overlay2
 Backing Filesystem: extfs
 Supports d_type: true
 Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: bridge host macvlan null overlay
 Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: cfd04396dc68220d1cecbe686a6cc3aa5ce3667c
runc version: 4fc53a81fb7c994640722ac585fa9ca548971871
init version: 949e6fa
Security Options:
 apparmor
 seccomp
  Profile: default
Kernel Version: 4.13.0-37-generic
Operating System: Ubuntu 17.10
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 1.947GiB
Name: rob-VirtualBox
ID: 3L2C:BTV3:TQO2:4SAG:XVW5:744G:MPWQ:62FK:56DP:KH3Z:EQ7Z:TBR5
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
 127.0.0.0/8
Live Restore Enabled: false

WARNING: No swap limit support

Additional environment details (AWS, VirtualBox, physical, etc.)

Running Ubuntu in VirtualBox 5.2.8 on OS/X 10.13.4

@bscheshirwork

This comment has been minimized.

Copy link

bscheshirwork commented Apr 2, 2018

same situation. I want to use this in docker-compose.yml for replace remote_host

    environment:
      XDEBUG_CONFIG: "remote_host=192.168.0.83 remote_port=9001 var_display_max_data=1024 var_display_max_depth=5"
$ docker-compose -f ~/projects/docker-yii2-app-advanced/docker-run/docker-compose.yml run --rm --entrypoint nslookup php "host.docker.internal"
Creating network "dockerrun_default" with the default driver
Creating dockerrun_mysql_1 ... done
Creating dockerrun_db_1    ... done
nslookup: can't resolve '(null)': Name does not resolve

nslookup: can't resolve 'host.docker.internal': Name does not resolve
...
Kernel Version: 4.4.0-116-generic
Operating System: Ubuntu 16.04.4 LTS
@bscheshirwork

This comment has been minimized.

Copy link

bscheshirwork commented Apr 2, 2018

https://docs.docker.com/docker-for-mac/networking/#use-cases-and-workarounds

I want to connect from a container to a service on the host

The host has a changing IP address (or none if you have no network access). From 18.03 onwards our recommendation is to connect to the special DNS name host.docker.internal, which resolves to the internal IP address used by the host.

The gateway is also reachable as gateway.docker.internal.

@JuxhinDB

This comment has been minimized.

Copy link

JuxhinDB commented Apr 11, 2018

Same situation, when taking a closer look at /etc/hosts I notice the following:

172.17.0.2 f85e063d8c3e

Which suggests that it's just setting it to a random ID rather than host.docker.internal which is what I need.

@Zomis

This comment has been minimized.

Copy link

Zomis commented Apr 15, 2018

When I tried to find out how to connect to host from Docker I found this question/answer on Stack Overflow: https://stackoverflow.com/a/43541732/1310566

I was not aware at the time that it only applied to macOS and Windows (it was just recently edited)

@atolia

This comment has been minimized.

Copy link

atolia commented May 1, 2018

While this feature not on linux I use

web:
  image: httpd:2.4
  volumes:
    - ......
  entrypoint: 
  - /bin/sh
  - -c 
  - ip -4 route list match 0/0 | awk '{print $$3" host.docker.internal"}' >> /etc/hosts && httpd-foreground 
@bscheshirwork

This comment has been minimized.

Copy link

bscheshirwork commented May 2, 2018

@atolia
This look like work with docker-compose exec web and don't work with docker-compose run --rm --entrypoint /bin/bash web

@hernandev

This comment has been minimized.

Copy link

hernandev commented May 8, 2018

Mimic on that can be done with:

echo -e "`/sbin/ip route|awk '/default/ { print $3 }'`\tdocker.host.internal" | sudo tee -a /etc/hosts > /dev/null

Notice @atolia already provided a similar solution, but he is not considering non-privileged default USER, this one will work for non-root images with passwordless sudo available, for images where default user is root, just remove the sudo part.

This command will make docker.host.internal available regardless of the Docker version OR execution mode. I'm using this on entrypoint files.

@acuthbert

This comment has been minimized.

Copy link

acuthbert commented May 15, 2018

Firstly - docker is an amazing tool and so thank you for all who work tirelessly on it! As a leader of a large mixed team of linux and mac engineers this has been one of the biggest "why did we leave vagrant" questions I get hammered about when we called time on vagrant. It is very frustrating that this exists on mac and windows and not linux. We need connect back for xdebug and for letting selenium running in a container to access local urls for acceptance testing. This difference is bloating our build scripts with more and more fragility so it would be great if this was standardised. Is it not a worry if the same version of docker engineer on the 3 platforms can deviate in feature set?

@FX-HAO

This comment has been minimized.

Copy link

FX-HAO commented May 16, 2018

docker.host.internal is still unavailable on my mac. And I can't connect to my host with 172.17.0.1.
My docker version:

Client:
 Version:      18.03.1-ce
 API version:  1.37
 Go version:   go1.9.5
 Git commit:   9ee9f40
 Built:        Thu Apr 26 07:13:02 2018
 OS/Arch:      darwin/amd64
 Experimental: false
 Orchestrator: swarm

Server:
 Engine:
  Version:      18.03.1-ce
  API version:  1.37 (minimum version 1.12)
  Go version:   go1.9.5
  Git commit:   9ee9f40
  Built:        Thu Apr 26 07:22:38 2018
  OS/Arch:      linux/amd64
  Experimental: true
@chaospixel

This comment has been minimized.

Copy link

chaospixel commented May 16, 2018

In case you missed it: docker.for.mac.host.internal and docker.for.mac.localhost do work - but only on docker for mac...

@FX-HAO

This comment has been minimized.

Copy link

FX-HAO commented May 16, 2018

From 18.03 onwards our recommendation is to connect to the special DNS name host.docker.internal

Older aliases are deprecated in favor of this one. And I tried them, not working.

@mnd999

This comment has been minimized.

Copy link

mnd999 commented May 16, 2018

Not working here either (unsurprisingly).

@mnd999

This comment has been minimized.

Copy link

mnd999 commented May 16, 2018

@JuxhinDB it's not a random number, it's the container id. Still not useful though.

@jcoutch

This comment has been minimized.

Copy link

jcoutch commented May 18, 2018

I'm running a microsoft/dotnet-framework container on a Windows host (v18.03.1-cd-win65 17513), and host.docker.internal does not work.

@kunal-bhatia

This comment has been minimized.

Copy link

kunal-bhatia commented May 22, 2018

Any idea, when will linux support for connecting to special DNS will be fixed?

@bscheshirwork

This comment has been minimized.

Copy link

bscheshirwork commented May 22, 2018

@kunalbhatia87 wait 4 resolve this issue? 😆

@rfay

This comment has been minimized.

Copy link

rfay commented May 23, 2018

There are several comments (and workarounds) here that mistakenly use "docker.host.internal", which I don't think was ever supported. The hostname we want to be supported is the one that's supported in Docker for Windows and Docker for Mac, "host.docker.internal"

@rfay

This comment has been minimized.

Copy link

rfay commented May 23, 2018

@Mahoney I think you should check "This is a bug report" in the OP. This is a bug. Docker team, please acknowledge it, thanks!

@Mahoney

This comment has been minimized.

Copy link

Mahoney commented May 23, 2018

@rfay I'm not aware of this ever being a documented feature of Linux docker - as far as I can see it's only documented for docker-for-mac and docker-for-windows, and only as a recent change in each case. I couldn't find any discussion around the choice or anything to suggest it had been agreed as something all versions of Docker should implement, though it would make sense to me if it were.

So as far as I can see "feature request" rather than "bug" is the correct categorisation.

@brunosaboia

This comment has been minimized.

Copy link

brunosaboia commented May 31, 2018

@Mahoney while I do agree that this is a feature request, it's an important one. In the end, you want your dev team to use a shared config file all across — the underlying OS should be indifferent.

For me, running Linux on a Mac-based team, it's very bad to have to either create and ignore the changes to a config file, or to have to create hosts entries in each VM to mimic Docker for Mac behavior. I think it's simpler to just have an additional entry on the Docker networking so that the host is always reachable using the same hostname.

@jtreminio

This comment has been minimized.

Copy link

jtreminio commented Jun 1, 2018

@brunosaboia What I do while waiting for this to be resolved is create Linux-specific config files and mount them via docker-compose.override.yml.

Not the perfect workaround, but it is the best solution right now.

@mrpink76

This comment has been minimized.

Copy link

mrpink76 commented Jun 14, 2018

I'm running a windows container on a windows host:
microsoft/dotnet:2.1-aspnetcore-runtime-nanoserver-1709

host.docker.internal does not work to connect to a service on the host.

@Mahoney

This comment has been minimized.

Copy link

Mahoney commented Jun 14, 2018

Issues with docker for windows should be raised in the docker/for-win repo after an appropriate search such as https://github.com/docker/for-win/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+host.docker.internal

@atrauzzi

This comment has been minimized.

Copy link

atrauzzi commented Jun 14, 2018

Yeah, this definitely needs to be implemented so that we can have consistency.

Really, containers should always have had something like this anyway.

@nyetwurk

This comment has been minimized.

Copy link

nyetwurk commented Oct 14, 2018

The way docker does DNS by default is fundamentally broken. It should always use dnsmasq and the host's DNS configuration by default (and not use 8.8.8.8). Each container should (by default) resolve DNS by querying the host, which should forward the request to its own resolver, and provide resolution for all of .docker.internal, including host.docker.internal. All other accessible containers should also resolve in .docker.internal

@atrauzzi

This comment has been minimized.

Copy link

atrauzzi commented Oct 15, 2018

Totally agree.

@perlun

This comment has been minimized.

Copy link

perlun commented Oct 19, 2018

The workaround I've used for now: use 172.17.0.1 as the "host IP address". This works unless people start changing the IP address of the docker0 interface, but it's clearly much less elegant than a host.docker.internal DNS name.

(Caveat: this uses the bridge default Docker network which is apparently deprecated and not recommended for production use according to this web page. However, for development environments it can be pretty fine.)

soedar added a commit to moexmen/dockerfiles that referenced this issue Oct 23, 2018

Update domain name used for forwarding (#24)
Refer to docker/for-linux#264 for an explanation of what's available for windows and mac, as well as issues with the same domain name on Linux.
@juanmirocks

This comment has been minimized.

Copy link

juanmirocks commented Nov 1, 2018

@perlun that IP works for me in most environments... but sometimes it doesn't. Definitely not a solution for production :-(

@lucasbasquerotto

This comment has been minimized.

Copy link

lucasbasquerotto commented Nov 1, 2018

@juanmirocks You can use ip -4 addr show docker0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}' (because the docker0 network should have an ip that connects to the host; you can run ip -4 addr show docker0 to inspect it).

Then you could pass the result value to some variable and use it.

For example, if you use docker-compose inside a container (https://hub.docker.com/r/docker/compose/), you can change it's script to use the variable.

1) Download the run script:

sudo curl -L --fail https://github.com/docker/compose/releases/download/1.22.0/run.sh -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

You can see the original script in the official repo: https://github.com/docker/compose/blob/1.22.0/script/run/run.sh

2) Change it to use the IP mentioned previously (basically these 2 lines of code instead of the last line in the original file):

DOCKER_HOST_IP=$(ip -4 addr show docker0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}')
exec docker run --rm $DOCKER_RUN_OPTIONS $DOCKER_ADDR $COMPOSE_OPTIONS $VOLUMES -w "$(pwd)" --env DOCKER_HOST_IP=$DOCKER_HOST_IP $IMAGE "$@"

Full script: https://github.com/lucasbasquerotto/docker-scripts/blob/1.0.0/docker-compose.sh

3) Use it in any compose file:

services:
  myservice:
    image: alpine
    extra_hosts:
      - "host.docker.internal:$DOCKER_HOST_IP"

The above is a docker compose use case, but it can be adapted to other scenarios.

@qoomon

This comment has been minimized.

Copy link

qoomon commented Nov 1, 2018

Especially for docker compose scenario I really recommend this docker container to solve the problem https://github.com/qoomon/docker-host

@hasnat

This comment has been minimized.

Copy link

hasnat commented Nov 1, 2018

I'd use apline so we're not depending on host
docker run --rm alpine ip route | awk 'NR==1 {print $3}'

@tomasfejfar tomasfejfar referenced this issue Nov 7, 2018

Merged

Vojta init #1

wongherlung pushed a commit to moexmen/dockerfiles that referenced this issue Dec 3, 2018

Update domain name used for forwarding (#24)
Refer to docker/for-linux#264 for an explanation of what's available for windows and mac, as well as issues with the same domain name on Linux.

wongherlung pushed a commit to moexmen/dockerfiles that referenced this issue Dec 3, 2018

Update domain name used for forwarding (#24)
Refer to docker/for-linux#264 for an explanation of what's available for windows and mac, as well as issues with the same domain name on Linux.
@seth100

This comment has been minimized.

Copy link

seth100 commented Jan 4, 2019

Hi,
I'm using docker-maven-plugin in my java maven project and I would need to reach the machine host from inside the docker container (I have a REST client calling a localhost:8081/... url).
What's the option for me?
Thanks

@qoomon

This comment has been minimized.

Copy link

qoomon commented Jan 4, 2019

@seth100

This comment has been minimized.

Copy link

seth100 commented Jan 4, 2019

@qoomon ok thanks. But how can I connect that container to mine one and what will it be the url of my endpoint from localhost:8081... to ...?

@qoomon

This comment has been minimized.

Copy link

qoomon commented Jan 4, 2019

@seth100 have a look at the README of https://github.com/qoomon/docker-host
your domain to access the docker host will look like dockerhost:8081

@qoomon

This comment has been minimized.

Copy link

qoomon commented Jan 4, 2019

@seth100 I've improved the documentation with some better examples and description, hopefully this will answer all your questions. Feel free to open a support issue at https://github.com/qoomon/docker-host

clemens-tolboom added a commit to clemens-tolboom/docker4drupal that referenced this issue Jan 8, 2019

Linux still required XDEBUG connect to 172.17.0.1
According to discussions in docker/for-linux#264 we still need the bridge IP on Linux.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment