Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Communicate to Docker host from Docker container

Tags:

docker

I’m experimenting with docker networking, and I can’t seem to find a way to accomplish the following:

Start a simple netcat server in the host:

nc -l -p 5555

and then communicate to this server from within a docker container, e.g.

# grab the docker network interface ip
hostip=$(ip route | awk '/docker0/ { print $NF }')
# pass in the docker network interface as a host and try a curl command
docker run --add-host=docker:"${hostip}" --rm -it hiromasaono/curl curl docker:5555

The curl request just hangs and the host netcat server does not receive a request.

If I instead start docker with --net=host option, it works:

docker run --net=host --rm -it hiromasaono/curl curl 127.0.0.1:5555

and the netcat server receives

GET / HTTP/1.1
User-Agent: curl/7.35.0
Host: 127.0.0.1:5555
Accept: */*

How can I communicate to the simple host netcat server from within the docker container without using --net=host (the default is --net=bridge)?

(fyi: I'm running docker server/client 1.11.2)

Potentailly Relevant Resources I've Studied in search of an answer:

  • Document how to connect to docker host from container (github issue)
  • How to connect to Docker host from container (github issue)
  • Allow Docker Container to Connect to a Local Postgres DB
  • Docker Container Networking Documentation
like image 336
C. Reed Avatar asked Aug 13 '16 20:08

C. Reed


People also ask

Can Docker containers communicate host?

docker run --network="host" Alternatively you can run a docker container with network settings set to host . Such a container will share the network stack with the docker host and from the container point of view, localhost (or 127.0.

How do I connect to Docker host?

Accessing the Host With the Default Bridge Mode You just need to reference it by its Docker network IP, instead of localhost or 127.0. 0.1 . Your host's Docker IP will be shown on the inet line. Connect to this IP address from within your containers to successfully access the services running on your host.

How do Docker containers communicate with each other?

If you are running more than one container, you can let your containers communicate with each other by attaching them to the same network. Docker creates virtual networks which let your containers talk to each other. In a network, a container has an IP address, and optionally a hostname.


Video Answer


1 Answers

As far as I understand, based on two comments[1][2], the following unofficial hack seems to be working for me as of today (2016.10.27):

In the Dockerfile for the app which needs to connect to the Docker host, I added the following lines:

...
# netstat
RUN apt-get update && apt-get install net-tools -y
...
CMD (netstat -nr | grep '^0\.0\.0\.0' | awk '{print $2" dockerhost"}' >> /etc/hosts) && \
        ...old CMD line...
...

This seems to make the Docker host available to me from within the container as dockerhost.


Note to future readers: see also https://github.com/docker/docker/issues/23177 — as of writing, this issue is still open, so there's a non-zero chance it may become official at some point in future and supersede any workarounds; though it may also become closed and dismissed as happened to its various predecessors.

like image 182
akavel Avatar answered Oct 12 '22 12:10

akavel