17

Im trying configure the docker daemon so i can connect to it from inside the docker containers i start..

So i changed /etc/docker/daemon.json to

{
   "hosts": ["unix:///var/run/docker.sock", "tcp://0.0.0.0:2375"]
}

So that i connect to it through the docker bridge.. However when i restart docker i get

netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         
State       PID/Program name
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      3728/mysqld     
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      24253/redis-server 
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      3756/nginx      
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      3634/sshd       
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      3756/nginx      
tcp6       0      0 :::8010                 :::*                    LISTEN      4230/apache2    
tcp6       0      0 :::9200                 :::*                    LISTEN      26824/java      
tcp6       0      0 :::9300                 :::*                    LISTEN      26824/java      
tcp6       0      0 :::22                   :::*                    LISTEN      3634/sshd       
tcp6       0      0 :::2375                 :::*                    LISTEN      1955/dockerd    

So first i though the issue was the fact that it was listening on ipv6 not ipv4. and according to Make docker use IPv4 for port binding It should all still work but it doesnt.. When i try

telnet 172.17.0.1(docker host) 2375

it fails to connect while

telnet 172.17.0.1(docker host) 80

works. How can i connect to docker running on the host machine? Im running on Ubuntu 14.04.5 docker Version: 17.06.2-ce

tgogos
  • 23,218
  • 20
  • 96
  • 128
darthShana
  • 367
  • 1
  • 4
  • 16
  • You are using bridge IP and not HOST IP. What is the output of `ifconfig` ? – Tarun Lalwani Oct 24 '17 at 17:56
  • / # ifconfig eth0 Link encap:Ethernet HWaddr 02:42:AC:11:00:02 inet addr:172.17.0.2 Bcast:0.0.0.0 Mask:255.255.0.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:14 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:1108 (1.0 KiB) TX bytes:0 (0.0 B) – darthShana Oct 25 '17 at 18:53
  • i though the container ip would be 172.17.0.2 and the default route would be the host ip 172.17.0.1.. and i could connect to ports running on the host using its ip 172.17.0.1.. I can connect to other service which are listening on 0.0.0.0 as apposed to ::: – darthShana Oct 25 '17 at 18:56

2 Answers2

22

You can start your containers mounting the host docker socket into your containers.

docker run -v /var/run/docker.sock:/var/run/docker.sock ...

With this setup, Docker clients inside the containers will be using the Docker daemon from the host. Your containers will be able to build, run, push etc. using daemon running in host. Please note that with these setup everything is happening on the host, so if you start new containers they will be “sibling” containers.

EDIT

If you are using the bridge network, you can connect to any service running on host machine using host IP address.

For example, I have mysqld running on my host with IP 10.0.0.1 and from a container I can do

mysql -u user -p -h 10.0.0.1

The trick is to find out the host IP address from containers.

In Docker for Mac (I am running version 17.07.0) is as simple as connecting to the special host "docker.for.mac.localhost"

Another option is to add an alias IP to your loopback interface

sudo ifconfig lo0 alias 192.168.1.1

And then when running containers add a host for this alias IP

docker run --rm -ti --add-host host-machine:192.168.1.1 mysql:5.7 bash

With this setup, inside container you should be able to do

mysql -u user -p -h host-machine
Luciano Afranllie
  • 4,053
  • 25
  • 23
  • ok cool.. this is an option.. but in general i would like to be able to connect to ports running on the host machine. things like redis etc.. – darthShana Oct 25 '17 at 18:58
  • @darthShana, please see my edit and let me know if that is what you want. If that is correct then please edit your question to specify that you want to connect using TCP/IP. – Luciano Afranllie Oct 26 '17 at 18:18
  • Hi @luciano-afranllia i tried mounting the docker socket as a volume as you suggested but i get Caused by: java.lang.UnsatisfiedLinkError: /tmp/libjunixsocket-native-2.0.46148114015950108127.so: libstdc++.so.6: cannot open shared object file: No such file or directory – darthShana Nov 02 '17 at 18:45
  • It seems that you don't have /var/run/docker.sock in your host. Check if docker daemon is running on host, with local socket enabled and configured at that location. – Luciano Afranllie Nov 03 '17 at 16:33
  • i do have that file.. And i am able to use the socket on the host machine.. Just not from a docker container after ive mounted the file as a volume – darthShana Nov 05 '17 at 03:06
  • ok turns out i was running based on alpine which doesnt have the required drivers to connet to the docker socket – darthShana Nov 05 '17 at 18:24
  • what about this ip address: 172.17.0.1 ? – Dani May 26 '20 at 12:21
  • 1
    I'd also like how it works with the default Docker IP 172.17.0.1. Some people saying, any service running on the host e.g. with localhost:1234 should be available from inside a Docker container through host.docker.internal:1234. But it doesn't work, it fails with "connection refused". – Moongazer Mar 01 '22 at 20:48
0

This answer may be a bit late, but it's better late than never as we never can tell who may be experiencing similar problem. I just fixed it be disabling the unnecessary ufw rule blocking the internal communication.

Example:

sudo ufw allow from <IP address or range> to any port  [desired port]

sudo ufw allow from 172.16.0.0/12 to any port  3421.

As for me, I disabled the UFW service totally using the command below.

sudo ufw disable
MisterNox
  • 1,445
  • 2
  • 8
  • 22
jemmy655
  • 1
  • 1