connect to docker daemon from inside docker container

和自甴很熟 提交于 2020-05-29 02:39:16

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/46916796/connect-to-docker-daemon-from-inside-docker-container

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!