Communicate with a Docker host from a Docker container

Im experimenting with a docker network and I cannot find a way to accomplish the following:

Start a simple netcat server in the host:

nc -l -p 5555

and then exchange data with this server from the docker container, for example

# 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 the request.

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

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

and netcat server gets

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

How can I connect to a simple host netcat server from a docker container without using --net=host(by default this --net=bridge)?

(fyi: I am running docker / client 1.11.2)

Potentially Relevant Resources I studied searching for an answer:

+4
2

, [1] [2], , , (2016.10.27):

Dockerfile , Docker, :

...
# 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...
...

, Docker dockerhost.


: . https://github.com/docker/docker/issues/23177 — , , - ; , .

0

, , .

  • Netcat (, , , Docker). . netcat -l -p 5555, netstat -a -l -n | grep 5555, 0.0.0.0:5555 *:5555. 127.0.0.1:5555, , , Docker.
  • docker, 127.0.0.1. docker run --net=host --rm -it hiromasaono/curl ping docker. pinging 127.0.0.1, , : hostip=$(ip route | awk '/docker0/ { print $NF }'). 127.0.0.1, , .
-1

Source: https://habr.com/ru/post/1651178/


All Articles