Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to Docker API from another machine?

I'm trying to use the Docker API to connect to docker daemon from another machine. I am able to do this command successfully:

docker -H=tcp://127.0.0.1:4243 images 

But NOT when I use the real IP address:

docker -H=tcp://192.168.2.123:4243 images 2013/08/04 01:35:53 dial tcp 192.168.2.123:4243: connection refused 

Why can't I connect when using a non-local IP?

I'm using a Vagrant VM with the following in Vagrantfile: config.vm.network :private_network, ip: "192.168.2.123"

The following is iptables:

# Generated by iptables-save v1.4.12 on Sun Aug  4 01:24:46 2013 *filter :INPUT ACCEPT [1974:252013] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [1511:932565] -A INPUT -p tcp -m tcp --dport 4243 -j ACCEPT COMMIT # Completed on Sun Aug  4 01:24:46 2013 # Generated by iptables-save v1.4.12 on Sun Aug  4 01:24:46 2013 *nat :PREROUTING ACCEPT [118:8562] :INPUT ACCEPT [91:6204] :OUTPUT ACCEPT [102:7211] :POSTROUTING ACCEPT [102:7211] :DOCKER - [0:0] -A PREROUTING -m addrtype --dst-type LOCAL -j DOCKER -A OUTPUT ! -d 127.0.0.0/8 -m addrtype --dst-type LOCAL -j DOCKER -A POSTROUTING -s 172.16.42.0/24 ! -d 172.16.42.0/24 -j MASQUERADE 
like image 833
Victor Lyuboslavsky Avatar asked Aug 04 '13 01:08

Victor Lyuboslavsky


People also ask

What is my Docker API URL?

It depends on your host, but look for /etc/default/docker or /var/lib/boot2docker/profile (for Docker Machine hosts using a boot2docker VM). Then get the IP address of the machine hosting your Docker daemon. (With a Docker Machine created host, that would be: docker-machine ip <yourmachine> .)

How do I access Docker from outside?

To make a port available to services outside of Docker, or to Docker containers which are not connected to the container's network, use the --publish or -p flag. This creates a firewall rule which maps a container port to a port on the Docker host to the outside world.


1 Answers

Came across a similar issue, one thing I don't see mentioned here is you need to start docker to listen to both the network and a unix socket. All regular docker (command-line) commands on the host assume the socket.

sudo docker -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock -d & 

will start docker listening to any ip address on your host, as well as the typical unix socket.

like image 178
Andy D Avatar answered Sep 26 '22 01:09

Andy D