How to access Docker daemon through tcp-socker?

2019-02-20 08:14发布

问题:

I have added

DOCKER_OPTS="-H tcp://0.0.0.0:2375"

to /etc/default/docker to make the Docker API accessible on my host machine (I'm running Docker in Virtualbox on an Ubuntu VM). However, when I try to run any Docker commands now, I just get this error message:

Cannot connect to the Docker daemon. Is the docker daemon running on this host?

I have tried sudo service docker restart, and restarted the machine, but nothing has worked. Any idea what the problem is?

回答1:

To use the daemon through the tcp socket the option -H tcp://0.0.0.0:2375 should be added to the command docker (both for the daemon and run).

To access the daemon with its default unix socket make sure that the Docker daemon is also started with the option -H=unix:///var/run/docker.sock.


Note that using the tcp is dangerous if you do not trust the network you are in. Here is the doc from the man page:

   -H, --host=[unix:///var/run/docker.sock]: tcp://[host]:[port][path] to bind or unix://[/path/to/socket] to use.
     The socket(s) to bind to in daemon mode specified using one or more
     tcp://host:port/path, unix:///path/to/socket, fd://* or fd://socketfd.
     If the tcp port is not specified, then it will default to either 2375 when
     --tls is off, or 2376 when --tls is on, or --tlsverify is specified.


标签: docker