Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

connect to docker daemon from inside docker container

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

like image 520
darthShana Avatar asked Oct 24 '17 17:10

darthShana


1 Answers

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
like image 131
Luciano Afranllie Avatar answered Nov 10 '22 17:11

Luciano Afranllie