Correct way to start docker daemon listening to specific port

15,093

Solution 1

I found a solution to my problem. I specified docker running on IP x and Port y, but docker then only listens to that socket. I had to add another -H flag with the unix socket in order to listen to local requests:

sudo /usr/bin/docker daemon -H tcp://0.0.0.0:5555 -H unix:///var/run/docker.sock

Solution 2

On Ubuntu (16.04 LTS) with docker-ce (17.03.1~ce-0~ubuntu-xenial) do the following to make docker listen to a TCP port instead of sockets.

Add a file /etc/systemd/system/docker.service.d/override.conf with the following content:

[Service]
ExecStart=
ExecStart=/usr/bin/dockerd

Add a file /etc/docker/daemon.json with the following content

{
    "hosts": [
        "tcp://127.0.0.1:2375"
    ] 
}

Reload (systemctl daemon-reload) and restart (systemctl restart docker.service) docker.

For reference: https://github.com/moby/moby/issues/25471

EDIT:

Be careful, so the demon will only listen to that network port ignoring local requests. To make docker listen to both remote and local, edit daemon.json but keep the standard unix socket

{
        "hosts" : [
                "unix:///var/run/docker.sock",
                "tcp://<docker-host-ip-or-localhost>:2375"
        ]
}

Docker daemon connection options docs

Solution 3

On Mac OSX you'd run the Docker Quickstart Terminal App to see:

Machine default already exists in VirtualBox.
Starting machine default...
Started machines may have new IP addresses. You may need to re-run the `docker-machine env` command.
Setting environment variables for machine default...

...

                        ##         .
                  ## ## ##        ==
               ## ## ## ## ##    ===
           /"""""""""""""""""\___/ ===
      ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ /  ===- ~~~
           \______ o           __/
             \    \         __/
              \____\_______/


docker is configured to use the default machine with IP 192.168.99.100
For help getting started, check out the docs at https://docs.docker.com

now

docker ps

should work

On Linux e.g. Ubuntu prepending sudo might be necessary. E.g.

docker ps

will lead to:

Get http:///var/run/docker.sock/v1.20/containers/json: dial unix /var/run/docker.sock: permission denied.
* Are you trying to connect to a TLS-enabled daemon without TLS?
* Is your docker daemon up and running?

but

sudo docker ps

will work.

See https://docs.docker.com/articles/basics/

To check if your docker service is running you can call

 sudo service --status-all 2>&1 | grep docker

and it should show:

[ - ]  docker

Sometimes your docker instllation might be corrupt see: Docker daemon does not start or restart

Solution 4

This thread is quite useful, so I'd like to contribute how to get this to work on Ubuntu 14.04.1 LTS with docker 1.12.6-cs13 (might work with docker-ce versions just as well). The systemd approach (also /etc/sysconfig/docker) is not possible for this Ubuntu vintage, the official docker documentation as of this writing has no post-install guide for making docker daemon listen on network using upstart for Ubuntu 14.04.1 LTS. I found that below approach works rather well:

create /etc/default/docker file with content DOCKER_OPTS = "-H tcp://MACHINE-IP:2375 -H unix:///var/run/docker.sock"

restart docker service sudo service docker restart. After doing this, docker daemon started listening on network and the daemon logs looked clean. Perhaps more experienced docker users can chime in if there is a better way.

Share:
15,093
EllisTheEllice
Author by

EllisTheEllice

Updated on June 06, 2022

Comments

  • EllisTheEllice
    EllisTheEllice almost 2 years

    I´m new to docker and want to start it in daemon mode listening to a specific IP-adress and port. In the documentation it is said that this can be done by writing sudo /usr/bin/docker daemon -H 0.0.0.0:5555. It then says that I can list running containers with this command docker ps. If I try this I get the following message:

    Get http:///var/run/docker.sock/v1.20/containers/json?all=1: dial unix /var/run/docker.sock: no such file or directory.

    • Are you trying to connect to a TLS-enabled daemon without TLS?
    • Is your docker daemon up and running?

    I cannot interact with it. I´ve searched for a solution but with no luck. Any suggestions?

    P.S. How can I run this daemon in background? I tried appending an & but I´m stuck on the ouput till pressing ctrl+c.

    Thanks in advance