Search code examples
jsonubuntudockersystemddocker-api

Docker - Enable Remote HTTP API with SystemD and "daemon.json"


Disclaimer:

On a old machine with Ubuntu 14.04 with Upstart as init system I have enabled the HTTP API by defining DOCKER_OPTS on /etc/default/docker. It works.

$ docker version
Client:
 Version:      1.11.2
 (...)

Server:
 Version:      1.11.2
 (...)

Problem:

This does solution does not work on a recent machine with Ubuntu 16.04 with SystemD.

As stated on the top of the recent file installed /etc/default/docker:

# Docker Upstart and SysVinit configuration file

#
# THIS FILE DOES NOT APPLY TO SYSTEMD
#
#   Please see the documentation for "systemd drop-ins":
#   https://docs.docker.com/engine/articles/systemd/
#
(...)

As I checked this information on the Docker documentation page for SystemD I need to fill a daemon.json file but as stated on the reference there are some properties self-explanatory but others could be under-explained.

That being said, I'm looking for help to convert this:

DOCKER_OPTS="-H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock -G myuser --debug"

to the daemon.jsonobject?


Notes

PS1: I'm aware that the daemon.json have a debug: true as default.

PS2: Probably the group: "myuser" it will work like this or with an array of strings.

PS3: My main concern is to use SOCK and HTTP simultaneous.


EDIT (8/08/2017) After reading the accepted answer, check the @white_gecko answer for more input on the matter.


Solution

  • With a lot of fragmented documentation it was difficult to solve this.

    My first solution was to create the daemon.json with

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

    This does not worked this error docker[5586]: unable to configure the Docker daemon with file /etc/docker/daemon.json after tried to restart the daemon with service docker restart. Note: There was more on the error that I failed to copy.

    But what this error meant it at the start the daemon it a conflict with a flag and configurations on daemon.json.

    When I looked into it with service docker status this it was the parent process: ExecStart=/usr/bin/docker daemon -H fd://.

    What it was strange because is different with configurations on /etc/init.d/docker which I thought that were the service configurations. The strange part it was that the file on init.d does contain any reference to daemon argument neither -H fd://.

    After some research and a lot of searches of the system directories, I find out these directory (with help on the discussion on this issue docker github issue #22339).

    Solution

    Edited the ExecStart from /lib/systemd/system/docker.service with this new value: /usr/bin/docker daemon

    And created the /etc/docker/daemon.json with

    {
      "hosts": [
        "fd://",
        "tcp://127.0.0.1:2376"
      ]
    }
    

    Finally restarted the service with service docker start and now I get the "green light" on service docker status.

    Tested the new configurations with:

    $ docker run hello-world
    
    Hello from Docker!
    (...)
    

    And,

    $ curl http://127.0.0.1:2376/v1.23/info
    [JSON]
    

    I hope that this will help someone with a similar problem as mine! :)