Configure Docker daemon port to enable Docker APIs

This is an addition to the answer provided by Dan Lowe.

There are two ways in configuring the Docker daemon port:

  1. Configuring at /etc/default/docker file:

    DOCKER_OPTS="-H tcp://127.0.0.1:5000 -H unix:///var/run/docker.sock"
    
  2. Configuring at /etc/docker/daemon.json:

    {
    "debug": true,
    "hosts": ["tcp://127.0.0.1:5000", "unix:///var/run/docker.sock"]
    }
    

Restart the Docker service after configuring the port.

If the Docker default socket is not configured, the similar issue mentioned in the question may occur, i.e., Docker will wait for an infinite period.

Note: But don’t configure in both the configuration files. The following error may occur:

Waiting for /var/run/docker.sock
unable to configure the Docker daemon with file /etc/docker/daemon.json: the following directives are specified both as a flag and in the configuration file: hosts: (from flag: [tcp://127.0.0.1:5000 unix:///var/run/docker.sock], from file: tcp://127.0.0.1:5000)

The reason for adding both the user port[ tcp://127.0.0.1:5000] and default Docker socket[unix:///var/run/docker.sock] is that the user port enables the access to the Docker APIs whereas the default socket enables the CLI. In case the default port[unix:///var/run/docker.sock] is not mentioned in the /etc/default/docker file the following error may occur:

# docker ps
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

This error is not because that the Docker container is not running, but because the default Docker socket is not enabled.

This is applicable for Docker version 17.04, and it may vary with different versions of Docker.

Leave a Comment